Skip to content

Commit

Permalink
Renaming known value check types (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jan 15, 2024
1 parent 5f4c952 commit aab63ef
Show file tree
Hide file tree
Showing 29 changed files with 748 additions and 748 deletions.
18 changes: 9 additions & 9 deletions knownvalue/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ import (
"strconv"
)

var _ Check = boolValueExact{}
var _ Check = boolExact{}

type boolValueExact struct {
type boolExact struct {
value bool
}

// CheckValue determines whether the passed value is of type bool, and
// contains a matching bool value.
func (v boolValueExact) CheckValue(other any) error {
func (v boolExact) CheckValue(other any) error {
otherVal, ok := other.(bool)

if !ok {
return fmt.Errorf("expected bool value for BoolValueExact check, got: %T", other)
return fmt.Errorf("expected bool value for BoolExact check, got: %T", other)
}

if otherVal != v.value {
return fmt.Errorf("expected value %t for BoolValueExact check, got: %t", v.value, otherVal)
return fmt.Errorf("expected value %t for BoolExact check, got: %t", v.value, otherVal)
}

return nil
}

// String returns the string representation of the bool value.
func (v boolValueExact) String() string {
func (v boolExact) String() string {
return strconv.FormatBool(v.value)
}

// BoolValueExact returns a Check for asserting equality between the
// BoolExact returns a Check for asserting equality between the
// supplied bool and the value passed to the CheckValue method.
func BoolValueExact(value bool) boolValueExact {
return boolValueExact{
func BoolExact(value bool) boolExact {
return boolExact{
value: value,
}
}
22 changes: 11 additions & 11 deletions knownvalue/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ func TestBoolValue_CheckValue(t *testing.T) {
expectedError error
}{
"zero-nil": {
self: knownvalue.BoolValueExact(false),
expectedError: fmt.Errorf("expected bool value for BoolValueExact check, got: <nil>"),
self: knownvalue.BoolExact(false),
expectedError: fmt.Errorf("expected bool value for BoolExact check, got: <nil>"),
},
"zero-other": {
self: knownvalue.BoolValueExact(false),
self: knownvalue.BoolExact(false),
other: false, // checking against the underlying value field zero-value
},
"nil": {
self: knownvalue.BoolValueExact(false),
expectedError: fmt.Errorf("expected bool value for BoolValueExact check, got: <nil>"),
self: knownvalue.BoolExact(false),
expectedError: fmt.Errorf("expected bool value for BoolExact check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.BoolValueExact(true),
self: knownvalue.BoolExact(true),
other: 1.23,
expectedError: fmt.Errorf("expected bool value for BoolValueExact check, got: float64"),
expectedError: fmt.Errorf("expected bool value for BoolExact check, got: float64"),
},
"not-equal": {
self: knownvalue.BoolValueExact(true),
self: knownvalue.BoolExact(true),
other: false,
expectedError: fmt.Errorf("expected value true for BoolValueExact check, got: false"),
expectedError: fmt.Errorf("expected value true for BoolExact check, got: false"),
},
"equal": {
self: knownvalue.BoolValueExact(true),
self: knownvalue.BoolExact(true),
other: true,
},
}
Expand All @@ -66,7 +66,7 @@ func TestBoolValue_CheckValue(t *testing.T) {
func TestBoolValue_String(t *testing.T) {
t.Parallel()

got := knownvalue.BoolValueExact(true).String()
got := knownvalue.BoolExact(true).String()

if diff := cmp.Diff(got, "true"); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
20 changes: 10 additions & 10 deletions knownvalue/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ import (
"strconv"
)

var _ Check = float64ValueExact{}
var _ Check = float64Exact{}

type float64ValueExact struct {
type float64Exact struct {
value float64
}

// CheckValue determines whether the passed value is of type float64, and
// contains a matching float64 value.
func (v float64ValueExact) CheckValue(other any) error {
func (v float64Exact) CheckValue(other any) error {
jsonNum, ok := other.(json.Number)

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

otherVal, err := jsonNum.Float64()

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

if otherVal != v.value {
return fmt.Errorf("expected value %s for Float64ValueExact check, got: %s", v.String(), strconv.FormatFloat(otherVal, 'f', -1, 64))
return fmt.Errorf("expected value %s for Float64Exact check, got: %s", v.String(), strconv.FormatFloat(otherVal, 'f', -1, 64))
}

return nil
}

// String returns the string representation of the float64 value.
func (v float64ValueExact) String() string {
func (v float64Exact) String() string {
return strconv.FormatFloat(v.value, 'f', -1, 64)
}

// Float64ValueExact returns a Check for asserting equality between the
// Float64Exact returns a Check for asserting equality between the
// supplied float64 and the value passed to the CheckValue method.
func Float64ValueExact(value float64) float64ValueExact {
return float64ValueExact{
func Float64Exact(value float64) float64Exact {
return float64Exact{
value: value,
}
}
22 changes: 11 additions & 11 deletions knownvalue/float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ func TestFloat64Value_CheckValue(t *testing.T) {
expectedError error
}{
"zero-nil": {
self: knownvalue.Float64ValueExact(0),
expectedError: fmt.Errorf("expected json.Number value for Float64ValueExact check, got: <nil>"),
self: knownvalue.Float64Exact(0),
expectedError: fmt.Errorf("expected json.Number value for Float64Exact check, got: <nil>"),
},
"zero-other": {
self: knownvalue.Float64ValueExact(0),
self: knownvalue.Float64Exact(0),
other: json.Number("0.0"), // checking against the underlying value field zero-value
},
"nil": {
self: knownvalue.Float64ValueExact(1.234),
expectedError: fmt.Errorf("expected json.Number value for Float64ValueExact check, got: <nil>"),
self: knownvalue.Float64Exact(1.234),
expectedError: fmt.Errorf("expected json.Number value for Float64Exact check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.Float64ValueExact(1.234),
self: knownvalue.Float64Exact(1.234),
other: json.Number("str"),
expectedError: fmt.Errorf("expected json.Number to be parseable as float64 value for Float64ValueExact check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
expectedError: fmt.Errorf("expected json.Number to be parseable as float64 value for Float64Exact check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
},
"not-equal": {
self: knownvalue.Float64ValueExact(1.234),
self: knownvalue.Float64Exact(1.234),
other: json.Number("4.321"),
expectedError: fmt.Errorf("expected value 1.234 for Float64ValueExact check, got: 4.321"),
expectedError: fmt.Errorf("expected value 1.234 for Float64Exact check, got: 4.321"),
},
"equal": {
self: knownvalue.Float64ValueExact(1.234),
self: knownvalue.Float64Exact(1.234),
other: json.Number("1.234"),
},
}
Expand All @@ -67,7 +67,7 @@ func TestFloat64Value_CheckValue(t *testing.T) {
func TestFloat64Value_String(t *testing.T) {
t.Parallel()

got := knownvalue.Float64ValueExact(1.234567890123e+09).String()
got := knownvalue.Float64Exact(1.234567890123e+09).String()

if diff := cmp.Diff(got, "1234567890.123"); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
20 changes: 10 additions & 10 deletions knownvalue/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ import (
"strconv"
)

var _ Check = int64ValueExact{}
var _ Check = int64Exact{}

type int64ValueExact struct {
type int64Exact struct {
value int64
}

// CheckValue determines whether the passed value is of type int64, and
// contains a matching int64 value.
func (v int64ValueExact) CheckValue(other any) error {
func (v int64Exact) CheckValue(other any) error {
jsonNum, ok := other.(json.Number)

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

otherVal, err := jsonNum.Int64()

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

if otherVal != v.value {
return fmt.Errorf("expected value %d for Int64ValueExact check, got: %d", v.value, otherVal)
return fmt.Errorf("expected value %d for Int64Exact check, got: %d", v.value, otherVal)
}

return nil
}

// String returns the string representation of the int64 value.
func (v int64ValueExact) String() string {
func (v int64Exact) String() string {
return strconv.FormatInt(v.value, 10)
}

// Int64ValueExact returns a Check for asserting equality between the
// Int64Exact returns a Check for asserting equality between the
// supplied int64 and the value passed to the CheckValue method.
func Int64ValueExact(value int64) int64ValueExact {
return int64ValueExact{
func Int64Exact(value int64) int64Exact {
return int64Exact{
value: value,
}
}
22 changes: 11 additions & 11 deletions knownvalue/int64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ func TestInt64Value_CheckValue(t *testing.T) {
expectedError error
}{
"zero-nil": {
self: knownvalue.Int64ValueExact(0),
expectedError: fmt.Errorf("expected json.Number value for Int64ValueExact check, got: <nil>"),
self: knownvalue.Int64Exact(0),
expectedError: fmt.Errorf("expected json.Number value for Int64Exact check, got: <nil>"),
},
"zero-other": {
self: knownvalue.Int64ValueExact(0),
self: knownvalue.Int64Exact(0),
other: json.Number("0"), // checking against the underlying value field zero-value
},
"nil": {
self: knownvalue.Int64ValueExact(1234),
expectedError: fmt.Errorf("expected json.Number value for Int64ValueExact check, got: <nil>"),
self: knownvalue.Int64Exact(1234),
expectedError: fmt.Errorf("expected json.Number value for Int64Exact check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.Int64ValueExact(1234),
self: knownvalue.Int64Exact(1234),
other: json.Number("str"),
expectedError: fmt.Errorf("expected json.Number to be parseable as int64 value for Int64ValueExact check: strconv.ParseInt: parsing \"str\": invalid syntax"),
expectedError: fmt.Errorf("expected json.Number to be parseable as int64 value for Int64Exact check: strconv.ParseInt: parsing \"str\": invalid syntax"),
},
"not-equal": {
self: knownvalue.Int64ValueExact(1234),
self: knownvalue.Int64Exact(1234),
other: json.Number("4321"),
expectedError: fmt.Errorf("expected value 1234 for Int64ValueExact check, got: 4321"),
expectedError: fmt.Errorf("expected value 1234 for Int64Exact check, got: 4321"),
},
"equal": {
self: knownvalue.Int64ValueExact(1234),
self: knownvalue.Int64Exact(1234),
other: json.Number("1234"),
},
}
Expand All @@ -67,7 +67,7 @@ func TestInt64Value_CheckValue(t *testing.T) {
func TestInt64Value_String(t *testing.T) {
t.Parallel()

got := knownvalue.Int64ValueExact(1234567890123).String()
got := knownvalue.Int64Exact(1234567890123).String()

if diff := cmp.Diff(got, "1234567890123"); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
18 changes: 9 additions & 9 deletions knownvalue/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"fmt"
)

var _ Check = listValueExact{}
var _ Check = listExact{}

type listValueExact struct {
type listExact struct {
value []Check
}

// CheckValue determines whether the passed value is of type []any, and
// contains matching slice entries in the same sequence.
func (v listValueExact) CheckValue(other any) error {
func (v listExact) CheckValue(other any) error {
otherVal, ok := other.([]any)

if !ok {
return fmt.Errorf("expected []any value for ListValueExact check, got: %T", other)
return fmt.Errorf("expected []any value for ListExact check, got: %T", other)
}

if len(otherVal) != len(v.value) {
Expand All @@ -34,7 +34,7 @@ func (v listValueExact) CheckValue(other any) error {
actualElements = "element"
}

return fmt.Errorf("expected %d %s for ListValueExact check, got %d %s", len(v.value), expectedElements, len(otherVal), actualElements)
return fmt.Errorf("expected %d %s for ListExact check, got %d %s", len(v.value), expectedElements, len(otherVal), actualElements)
}

for i := 0; i < len(v.value); i++ {
Expand All @@ -47,7 +47,7 @@ func (v listValueExact) CheckValue(other any) error {
}

// String returns the string representation of the value.
func (v listValueExact) String() string {
func (v listExact) String() string {
var listVals []string

for _, val := range v.value {
Expand All @@ -57,11 +57,11 @@ func (v listValueExact) String() string {
return fmt.Sprintf("%s", listVals)
}

// ListValueExact returns a Check for asserting equality between the
// ListExact returns a Check for asserting equality between the
// supplied []Check and the value passed to the CheckValue method.
// This is an order-dependent check.
func ListValueExact(value []Check) listValueExact {
return listValueExact{
func ListExact(value []Check) listExact {
return listExact{
value: value,
}
}
Loading

0 comments on commit aab63ef

Please sign in to comment.