Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Value.IsXxx #257

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 292 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,295 @@ func (v *Value) NotInList(values ...interface{}) *Value {

return v
}

// IsObject succeeds if the underlying value is an object.
//
// If underlying value is not an object (map[string]interface{}), failure is reported.
//
// Example:
//
// value := NewValue(t, map[string]interface{}{"foo": 123})
// value.IsObject()
func (v *Value) IsObject() *Value {
opChain := v.chain.enter("IsObject()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(map[string]interface{}); !ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is object"),
},
})
}

return v
}

// NotObject succeeds if the underlying value is not an object.
//
// If underlying value is an object (map[string]interface{}), failure is reported.
//
// Example:
//
// value := NewValue(t, map[string]interface{}{"foo": 123})
// value.NotObject()
func (v *Value) NotObject() *Value {
opChain := v.chain.enter("NotObject()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(map[string]interface{}); ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is not object"),
},
})
}

return v
}

// IsArray succeeds if the underlying value is an array.
//
// If underlying value is not an array ([]interface{}), failure is reported.
//
// Example:
//
// value := NewValue(t, []interface{}{"foo", "123"})
// value.IsArray()
func (v *Value) IsArray() *Value {
opChain := v.chain.enter("IsArray()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.([]interface{}); !ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is array"),
},
})
}

return v
}

// NotArray succeeds if the underlying value is not an array.
//
// If underlying value is an array ([]interface{}), failure is reported.
//
// Example:
//
// value := NewValue(t, []interface{}{"foo", "123"})
// value.NotArray()
func (v *Value) NotArray() *Value {
opChain := v.chain.enter("NotArray()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.([]interface{}); ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is not array"),
},
})
}

return v
}

// IsString succeeds if the underlying value is a string.
//
// If underlying value is not a string, failure is reported.
//
// Example:
//
// value := NewValue(t, "foo")
// value.IsString()
func (v *Value) IsString() *Value {
opChain := v.chain.enter("IsString()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(string); !ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is string"),
},
})
}

return v
}

// NotString succeeds if the underlying value is not a string.
//
// If underlying value is a string, failure is reported.
//
// Example:
//
// value := NewValue(t, "foo")
// value.NotString()
func (v *Value) NotString() *Value {
opChain := v.chain.enter("NotString()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(string); ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is not string"),
},
})
}

return v
}

// IsNumber succeeds if the underlying value is a number.
//
// If underlying value is not a number (numeric type convertible to float64),
// failure is reported
//
// Example:
//
// value := NewValue(t, 123)
// value.IsNumber()
func (v *Value) IsNumber() *Value {
opChain := v.chain.enter("IsNumber()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(float64); !ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is number"),
},
})
}

return v
}

// NotNumber succeeds if the underlying value is a not a number.
//
// If underlying value is a number (numeric type convertible to float64),
// failure is reported
//
// Example:
//
// value := NewValue(t, 123)
// value.NotNumber()
func (v *Value) NotNumber() *Value {
opChain := v.chain.enter("NotNumber()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(float64); ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is not number"),
},
})
}

return v
}

// IsBoolean succeeds if the underlying value is a boolean.
//
// If underlying value is not a boolean, failure is reported.
//
// Example:
//
// value := NewValue(t, true)
// value.IsBoolean()
func (v *Value) IsBoolean() *Value {
opChain := v.chain.enter("IsBoolean()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(bool); !ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is boolean"),
},
})
}

return v
}

// NotBoolean succeeds if the underlying value is not a boolean.
//
// If underlying value is a boolean, failure is reported.
//
// Example:
//
// value := NewValue(t, true)
// value.NotBoolean()
func (v *Value) NotBoolean() *Value {
opChain := v.chain.enter("NotBoolean()")
defer opChain.leave()

if opChain.failed() {
return v
}

if _, ok := v.value.(bool); ok {
opChain.fail(AssertionFailure{
Type: AssertValid,
Actual: &AssertionValue{v.value},
Errors: []error{
errors.New("expected: value is not boolean"),
},
})
}

return v
}
Loading