From e44918aa5a8e6f1473467cbcbe198665d144c068 Mon Sep 17 00:00:00 2001 From: Victor Gaydov Date: Wed, 1 Feb 2023 18:55:03 +0400 Subject: [PATCH] Cleanup Value methods --- value.go | 376 +++++++++++++++++++++++++++---------------------------- 1 file changed, 188 insertions(+), 188 deletions(-) diff --git a/value.go b/value.go index cae9355dc..6bae66489 100644 --- a/value.go +++ b/value.go @@ -21,19 +21,19 @@ type Value struct { // Example: // // value := NewValue(t, map[string]interface{}{"foo": 123}) -// value.Object() +// value.IsObject() // // value := NewValue(t, []interface{}{"foo", 123}) -// value.Array() +// value.IsArray() // // value := NewValue(t, "foo") -// value.String() +// value.IsString() // // value := NewValue(t, 123) -// value.Number() +// value.IsNumber() // // value := NewValue(t, true) -// value.Boolean() +// value.IsBoolean() // // value := NewValue(t, nil) // value.Null() @@ -242,7 +242,7 @@ func (v *Value) Object() *Object { Type: AssertValid, Actual: &AssertionValue{v.value}, Errors: []error{ - errors.New("expected: value is map"), + errors.New("expected: value is object"), }, }) return newObject(opChain, nil) @@ -428,8 +428,8 @@ func (v *Value) Null() *Value { // value := NewValue(t, "") // value.NotNull() // -// value := NewValue(t, make([]interface{}, 0) -// value.Null() +// value := NewValue(t, make([]interface{}, 0)) +// value.NotNull() func (v *Value) NotNull() *Value { opChain := v.chain.enter("NotNull()") defer opChain.leave() @@ -438,7 +438,7 @@ func (v *Value) NotNull() *Value { return v } - if !(v.value != nil) { + if v.value == nil { opChain.fail(AssertionFailure{ Type: AssertNotNil, Actual: &AssertionValue{v.value}, @@ -451,180 +451,6 @@ func (v *Value) NotNull() *Value { return v } -// IsEqual succeeds if value is equal to another value (e.g. map, slice, string, etc). -// Before comparison, both values are converted to canonical form. -// -// Example: -// -// value := NewValue(t, "foo") -// value.IsEqual("foo") -func (v *Value) IsEqual(value interface{}) *Value { - opChain := v.chain.enter("IsEqual()") - defer opChain.leave() - - if opChain.failed() { - return v - } - - expected, ok := canonValue(opChain, value) - if !ok { - return v - } - - if !reflect.DeepEqual(expected, v.value) { - opChain.fail(AssertionFailure{ - Type: AssertEqual, - Actual: &AssertionValue{v.value}, - Expected: &AssertionValue{expected}, - Errors: []error{ - errors.New("expected: values are equal"), - }, - }) - } - - return v -} - -// NotEqual succeeds if value is not equal to another value (e.g. map, slice, -// string, etc). Before comparison, both values are converted to canonical form. -// -// Example: -// -// value := NewValue(t, "foo") -// value.NorEqual("bar") -func (v *Value) NotEqual(value interface{}) *Value { - opChain := v.chain.enter("NotEqual()") - defer opChain.leave() - - if opChain.failed() { - return v - } - - expected, ok := canonValue(opChain, value) - if !ok { - return v - } - - if reflect.DeepEqual(expected, v.value) { - opChain.fail(AssertionFailure{ - Type: AssertNotEqual, - Actual: &AssertionValue{v.value}, - Expected: &AssertionValue{expected}, - Errors: []error{ - errors.New("expected: values are non-equal"), - }, - }) - } - - return v -} - -// Deprecated: use IsEqual instead. -func (v *Value) Equal(value interface{}) *Value { - return v.IsEqual(value) -} - -// InList succeeds if whole value is equal to one of the values from given -// list of values (e.g. map, slice, string, etc). -// Before comparison, all values are converted to canonical form. -// -// Example: -// -// value := NewValue(t, "foo") -// value.InList("foo", map[string]interface{}{"bar": true}) -func (v *Value) InList(values ...interface{}) *Value { - opChain := v.chain.enter("InList()") - defer opChain.leave() - - if opChain.failed() { - return v - } - - if len(values) == 0 { - opChain.fail(AssertionFailure{ - Type: AssertUsage, - Errors: []error{ - errors.New("unexpected empty list argument"), - }, - }) - return v - } - - var isListed bool - for _, val := range values { - expected, ok := canonValue(opChain, val) - if !ok { - return v - } - - if reflect.DeepEqual(expected, v.value) { - isListed = true - break - } - } - - if !isListed { - opChain.fail(AssertionFailure{ - Type: AssertBelongs, - Actual: &AssertionValue{v.value}, - Expected: &AssertionValue{AssertionList(values)}, - Errors: []error{ - errors.New("expected: value is equal to one of the values"), - }, - }) - } - - return v -} - -// NotInList succeeds if the whole value is not equal to any of the values from -// given list of values (e.g. map, slice, string, etc). -// Before comparison, all values are converted to canonical form. -// -// Example: -// -// value := NewValue(t, "foo") -// value.NotInList("bar", map[string]interface{}{"bar": true}) -func (v *Value) NotInList(values ...interface{}) *Value { - opChain := v.chain.enter("NotInList()") - defer opChain.leave() - - if opChain.failed() { - return v - } - - if len(values) == 0 { - opChain.fail(AssertionFailure{ - Type: AssertUsage, - Errors: []error{ - errors.New("unexpected empty list argument"), - }, - }) - return v - } - - for _, val := range values { - expected, ok := canonValue(opChain, val) - if !ok { - return v - } - - if reflect.DeepEqual(expected, v.value) { - opChain.fail(AssertionFailure{ - Type: AssertNotBelongs, - Actual: &AssertionValue{v.value}, - Expected: &AssertionValue{AssertionList(values)}, - Errors: []error{ - errors.New("expected: value is not equal to any of the values"), - }, - }) - return v - } - } - - return v -} - // IsObject succeeds if the underlying value is an object. // // If underlying value is not an object (map[string]interface{}), failure is reported. @@ -660,7 +486,7 @@ func (v *Value) IsObject() *Value { // // Example: // -// value := NewValue(t, map[string]interface{}{"foo": 123}) +// value := NewValue(t, nil) // value.NotObject() func (v *Value) NotObject() *Value { opChain := v.chain.enter("NotObject()") @@ -718,7 +544,7 @@ func (v *Value) IsArray() *Value { // // Example: // -// value := NewValue(t, []interface{}{"foo", "123"}) +// value := NewValue(t, nil) // value.NotArray() func (v *Value) NotArray() *Value { opChain := v.chain.enter("NotArray()") @@ -776,7 +602,7 @@ func (v *Value) IsString() *Value { // // Example: // -// value := NewValue(t, "foo") +// value := NewValue(t, nil) // value.NotString() func (v *Value) NotString() *Value { opChain := v.chain.enter("NotString()") @@ -836,7 +662,7 @@ func (v *Value) IsNumber() *Value { // // Example: // -// value := NewValue(t, 123) +// value := NewValue(t, nil) // value.NotNumber() func (v *Value) NotNumber() *Value { opChain := v.chain.enter("NotNumber()") @@ -894,7 +720,7 @@ func (v *Value) IsBoolean() *Value { // // Example: // -// value := NewValue(t, true) +// value := NewValue(t, nil) // value.NotBoolean() func (v *Value) NotBoolean() *Value { opChain := v.chain.enter("NotBoolean()") @@ -916,3 +742,177 @@ func (v *Value) NotBoolean() *Value { return v } + +// IsEqual succeeds if value is equal to another value (e.g. map, slice, string, etc). +// Before comparison, both values are converted to canonical form. +// +// Example: +// +// value := NewValue(t, "foo") +// value.IsEqual("foo") +func (v *Value) IsEqual(value interface{}) *Value { + opChain := v.chain.enter("IsEqual()") + defer opChain.leave() + + if opChain.failed() { + return v + } + + expected, ok := canonValue(opChain, value) + if !ok { + return v + } + + if !reflect.DeepEqual(expected, v.value) { + opChain.fail(AssertionFailure{ + Type: AssertEqual, + Actual: &AssertionValue{v.value}, + Expected: &AssertionValue{expected}, + Errors: []error{ + errors.New("expected: values are equal"), + }, + }) + } + + return v +} + +// NotEqual succeeds if value is not equal to another value (e.g. map, slice, +// string, etc). Before comparison, both values are converted to canonical form. +// +// Example: +// +// value := NewValue(t, "foo") +// value.NorEqual("bar") +func (v *Value) NotEqual(value interface{}) *Value { + opChain := v.chain.enter("NotEqual()") + defer opChain.leave() + + if opChain.failed() { + return v + } + + expected, ok := canonValue(opChain, value) + if !ok { + return v + } + + if reflect.DeepEqual(expected, v.value) { + opChain.fail(AssertionFailure{ + Type: AssertNotEqual, + Actual: &AssertionValue{v.value}, + Expected: &AssertionValue{expected}, + Errors: []error{ + errors.New("expected: values are non-equal"), + }, + }) + } + + return v +} + +// Deprecated: use IsEqual instead. +func (v *Value) Equal(value interface{}) *Value { + return v.IsEqual(value) +} + +// InList succeeds if whole value is equal to one of the values from given +// list of values (e.g. map, slice, string, etc). +// Before comparison, all values are converted to canonical form. +// +// Example: +// +// value := NewValue(t, "foo") +// value.InList("foo", 123) +func (v *Value) InList(values ...interface{}) *Value { + opChain := v.chain.enter("InList()") + defer opChain.leave() + + if opChain.failed() { + return v + } + + if len(values) == 0 { + opChain.fail(AssertionFailure{ + Type: AssertUsage, + Errors: []error{ + errors.New("unexpected empty list argument"), + }, + }) + return v + } + + var isListed bool + for _, val := range values { + expected, ok := canonValue(opChain, val) + if !ok { + return v + } + + if reflect.DeepEqual(expected, v.value) { + isListed = true + break + } + } + + if !isListed { + opChain.fail(AssertionFailure{ + Type: AssertBelongs, + Actual: &AssertionValue{v.value}, + Expected: &AssertionValue{AssertionList(values)}, + Errors: []error{ + errors.New("expected: value is equal to one of the values"), + }, + }) + } + + return v +} + +// NotInList succeeds if the whole value is not equal to any of the values from +// given list of values (e.g. map, slice, string, etc). +// Before comparison, all values are converted to canonical form. +// +// Example: +// +// value := NewValue(t, "foo") +// value.NotInList("bar", 123) +func (v *Value) NotInList(values ...interface{}) *Value { + opChain := v.chain.enter("NotInList()") + defer opChain.leave() + + if opChain.failed() { + return v + } + + if len(values) == 0 { + opChain.fail(AssertionFailure{ + Type: AssertUsage, + Errors: []error{ + errors.New("unexpected empty list argument"), + }, + }) + return v + } + + for _, val := range values { + expected, ok := canonValue(opChain, val) + if !ok { + return v + } + + if reflect.DeepEqual(expected, v.value) { + opChain.fail(AssertionFailure{ + Type: AssertNotBelongs, + Actual: &AssertionValue{v.value}, + Expected: &AssertionValue{AssertionList(values)}, + Errors: []error{ + errors.New("expected: value is not equal to any of the values"), + }, + }) + return v + } + } + + return v +}