Skip to content

Commit

Permalink
Refactoring to accomodate custom known value checks in ExpectKnownVal…
Browse files Browse the repository at this point in the history
…ue, ExpectKnownOutputValue and ExpectKnownOutputValueAtPath (#243)
  • Loading branch information
bendbennett committed Jan 5, 2024
1 parent ad071ae commit efb0b98
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 544 deletions.
11 changes: 9 additions & 2 deletions knownvalue/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue

import (
"encoding/json"
"fmt"
"strconv"
)
Expand All @@ -19,10 +20,16 @@ type Float64Value struct {
// CheckValue determines whether the passed value is of type float64, and
// contains a matching float64 value.
func (v Float64Value) CheckValue(other any) error {
otherVal, ok := other.(float64)
jsonNum, ok := other.(json.Number)

if !ok {
return fmt.Errorf("expected float64 value for Float64Value check, got: %T", other)
return fmt.Errorf("expected json.Number value for Float64Value check, got: %T", other)
}

otherVal, err := jsonNum.Float64()

if err != nil {
return fmt.Errorf("expected json.Number to be parseable as float64 value for Float64Value check: %s", err)
}

if otherVal != v.value {
Expand Down
15 changes: 8 additions & 7 deletions knownvalue/float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue_test

import (
"encoding/json"
"fmt"
"testing"

Expand All @@ -21,28 +22,28 @@ func TestFloat64Value_CheckValue(t *testing.T) {
expectedError error
}{
"zero-nil": {
expectedError: fmt.Errorf("expected float64 value for Float64Value check, got: <nil>"),
expectedError: fmt.Errorf("expected json.Number value for Float64Value check, got: <nil>"),
},
"zero-other": {
other: 0.0, // checking against the underlying value field zero-value
other: json.Number("0.0"), // checking against the underlying value field zero-value
},
"nil": {
self: knownvalue.Float64ValueExact(1.234),
expectedError: fmt.Errorf("expected float64 value for Float64Value check, got: <nil>"),
expectedError: fmt.Errorf("expected json.Number value for Float64Value check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.Float64ValueExact(1.234),
other: int64(1234),
expectedError: fmt.Errorf("expected float64 value for Float64Value check, got: int64"),
other: json.Number("str"),
expectedError: fmt.Errorf("expected json.Number to be parseable as float64 value for Float64Value check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
},
"not-equal": {
self: knownvalue.Float64ValueExact(1.234),
other: 4.321,
other: json.Number("4.321"),
expectedError: fmt.Errorf("expected value 1.234 for Float64Value check, got: 4.321"),
},
"equal": {
self: knownvalue.Float64ValueExact(1.234),
other: 1.234,
other: json.Number("1.234"),
},
}

Expand Down
11 changes: 9 additions & 2 deletions knownvalue/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue

import (
"encoding/json"
"fmt"
"strconv"
)
Expand All @@ -19,10 +20,16 @@ type Int64Value struct {
// CheckValue determines whether the passed value is of type int64, and
// contains a matching int64 value.
func (v Int64Value) CheckValue(other any) error {
otherVal, ok := other.(int64)
jsonNum, ok := other.(json.Number)

if !ok {
return fmt.Errorf("expected int64 value for Int64Value check, got: %T", other)
return fmt.Errorf("expected json.Number value for Int64Value check, got: %T", other)
}

otherVal, err := jsonNum.Int64()

if err != nil {
return fmt.Errorf("expected json.Number to be parseable as int64 value for Int64Value check: %s", err)
}

if otherVal != v.value {
Expand Down
15 changes: 8 additions & 7 deletions knownvalue/int64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue_test

import (
"encoding/json"
"fmt"
"testing"

Expand All @@ -21,28 +22,28 @@ func TestInt64Value_CheckValue(t *testing.T) {
expectedError error
}{
"zero-nil": {
expectedError: fmt.Errorf("expected int64 value for Int64Value check, got: <nil>"),
expectedError: fmt.Errorf("expected json.Number value for Int64Value check, got: <nil>"),
},
"zero-other": {
other: int64(0), // checking against the underlying value field zero-value
other: json.Number("0"), // checking against the underlying value field zero-value
},
"nil": {
self: knownvalue.Int64ValueExact(1234),
expectedError: fmt.Errorf("expected int64 value for Int64Value check, got: <nil>"),
expectedError: fmt.Errorf("expected json.Number value for Int64Value check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.Int64ValueExact(1234),
other: 1.234,
expectedError: fmt.Errorf("expected int64 value for Int64Value check, got: float64"),
other: json.Number("str"),
expectedError: fmt.Errorf("expected json.Number to be parseable as int64 value for Int64Value check: strconv.ParseInt: parsing \"str\": invalid syntax"),
},
"not-equal": {
self: knownvalue.Int64ValueExact(1234),
other: int64(4321),
other: json.Number("4321"),
expectedError: fmt.Errorf("expected value 1234 for Int64Value check, got: 4321"),
},
"equal": {
self: knownvalue.Int64ValueExact(1234),
other: int64(1234),
other: json.Number("1234"),
},
}

Expand Down
27 changes: 23 additions & 4 deletions knownvalue/list_partial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue_test

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -58,7 +59,10 @@ func TestListValuePartial_CheckValue(t *testing.T) {
2: knownvalue.Float64ValueExact(4.56),
3: knownvalue.Float64ValueExact(7.89),
}),
other: []any{1.23, 4.56},
other: []any{
json.Number("1.23"),
json.Number("4.56"),
},
expectedError: fmt.Errorf("missing element index 2 for ListValuePartial check"),
},
"not-equal": {
Expand All @@ -67,7 +71,12 @@ func TestListValuePartial_CheckValue(t *testing.T) {
2: knownvalue.Float64ValueExact(4.56),
3: knownvalue.Float64ValueExact(7.89),
}),
other: []any{1.23, 4.56, 6.54, 5.46},
other: []any{
json.Number("1.23"),
json.Number("4.56"),
json.Number("6.54"),
json.Number("5.46"),
},
expectedError: fmt.Errorf("list element 2: expected value 4.56 for Float64Value check, got: 6.54"),
},
"wrong-order": {
Expand All @@ -76,7 +85,12 @@ func TestListValuePartial_CheckValue(t *testing.T) {
2: knownvalue.Float64ValueExact(4.56),
3: knownvalue.Float64ValueExact(7.89),
}),
other: []any{1.23, 0.00, 7.89, 4.56},
other: []any{
json.Number("1.23"),
json.Number("0.00"),
json.Number("7.89"),
json.Number("4.56"),
},
expectedError: fmt.Errorf("list element 2: expected value 4.56 for Float64Value check, got: 7.89"),
},
"equal": {
Expand All @@ -85,7 +99,12 @@ func TestListValuePartial_CheckValue(t *testing.T) {
2: knownvalue.Float64ValueExact(4.56),
3: knownvalue.Float64ValueExact(7.89),
}),
other: []any{1.23, 0.00, 4.56, 7.89},
other: []any{
json.Number("1.23"),
json.Number("0.00"),
json.Number("4.56"),
json.Number("7.89"),
},
},
}

Expand Down
19 changes: 10 additions & 9 deletions knownvalue/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue_test

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -71,9 +72,9 @@ func TestListValue_CheckValue(t *testing.T) {
knownvalue.Int64ValueExact(789),
}),
other: []any{
int64(123),
int64(456),
int64(654),
json.Number("123"),
json.Number("456"),
json.Number("654"),
},
expectedError: fmt.Errorf("list element index 2: expected value 789 for Int64Value check, got: 654"),
},
Expand All @@ -84,9 +85,9 @@ func TestListValue_CheckValue(t *testing.T) {
knownvalue.Int64ValueExact(789),
}),
other: []any{
int64(123),
int64(789),
int64(456),
json.Number("123"),
json.Number("789"),
json.Number("456"),
},
expectedError: fmt.Errorf("list element index 1: expected value 456 for Int64Value check, got: 789"),
},
Expand All @@ -97,9 +98,9 @@ func TestListValue_CheckValue(t *testing.T) {
knownvalue.Int64ValueExact(789),
}),
other: []any{
int64(123),
int64(456),
int64(789),
json.Number("123"),
json.Number("456"),
json.Number("789"),
},
},
}
Expand Down
29 changes: 15 additions & 14 deletions knownvalue/map_partial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue_test

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -55,8 +56,8 @@ func TestMapValuePartial_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 4.56,
"one": json.Number("1.23"),
"two": json.Number("4.56"),
},
expectedError: fmt.Errorf("missing element three for MapValuePartial check"),
},
Expand All @@ -66,9 +67,9 @@ func TestMapValuePartial_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 4.56,
"three": 6.54,
"one": json.Number("1.23"),
"two": json.Number("4.56"),
"three": json.Number("6.54"),
},
expectedError: fmt.Errorf("three map element: expected value 7.89 for Float64Value check, got: 6.54"),
},
Expand All @@ -78,9 +79,9 @@ func TestMapValuePartial_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 7.89,
"three": 4.56,
"one": json.Number("1.23"),
"two": json.Number("7.89"),
"three": json.Number("4.56"),
},
expectedError: fmt.Errorf("three map element: expected value 7.89 for Float64Value check, got: 4.56"),
},
Expand All @@ -91,9 +92,9 @@ func TestMapValuePartial_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"four": 1.23,
"five": 7.89,
"six": 4.56,
"four": json.Number("1.23"),
"five": json.Number("7.89"),
"six": json.Number("4.56"),
},
expectedError: fmt.Errorf("missing element one for MapValuePartial check"),
},
Expand All @@ -103,9 +104,9 @@ func TestMapValuePartial_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 4.56,
"three": 7.89,
"one": json.Number("1.23"),
"two": json.Number("4.56"),
"three": json.Number("7.89"),
},
},
}
Expand Down
29 changes: 15 additions & 14 deletions knownvalue/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package knownvalue_test

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -59,8 +60,8 @@ func TestMapValue_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 4.56,
"one": json.Number("1.23"),
"two": json.Number("4.56"),
},
expectedError: fmt.Errorf("expected 3 elements for MapValue check, got 2 elements"),
},
Expand All @@ -71,9 +72,9 @@ func TestMapValue_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 4.56,
"three": 6.54,
"one": json.Number("1.23"),
"two": json.Number("4.56"),
"three": json.Number("6.54"),
},
expectedError: fmt.Errorf("three map element: expected value 7.89 for Float64Value check, got: 6.54"),
},
Expand All @@ -84,9 +85,9 @@ func TestMapValue_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 7.89,
"three": 4.56,
"one": json.Number("1.23"),
"two": json.Number("7.89"),
"three": json.Number("4.56"),
},
expectedError: fmt.Errorf("three map element: expected value 7.89 for Float64Value check, got: 4.56"),
},
Expand All @@ -97,9 +98,9 @@ func TestMapValue_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"four": 1.23,
"five": 7.89,
"six": 4.56,
"four": json.Number("1.23"),
"five": json.Number("7.89"),
"six": json.Number("4.56"),
},
expectedError: fmt.Errorf("missing element one for MapValue check"),
},
Expand All @@ -110,9 +111,9 @@ func TestMapValue_CheckValue(t *testing.T) {
"three": knownvalue.Float64ValueExact(7.89),
}),
other: map[string]any{
"one": 1.23,
"two": 4.56,
"three": 7.89,
"one": json.Number("1.23"),
"two": json.Number("4.56"),
"three": json.Number("7.89"),
},
},
}
Expand Down
Loading

0 comments on commit efb0b98

Please sign in to comment.