Skip to content

Commit

Permalink
Unexporting types that implement known value check (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jan 11, 2024
1 parent efb0b98 commit d14429e
Show file tree
Hide file tree
Showing 37 changed files with 378 additions and 382 deletions.
18 changes: 8 additions & 10 deletions knownvalue/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,37 @@ import (
"strconv"
)

var _ Check = BoolValue{}
var _ Check = boolValueExact{}

// BoolValue is a Check for asserting equality between the value supplied
// to BoolValueExact and the value passed to the CheckValue method.
type BoolValue struct {
type boolValueExact struct {
value bool
}

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

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

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

return nil
}

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

// BoolValueExact returns a Check for asserting equality between the
// supplied bool and the value passed to the CheckValue method.
func BoolValueExact(value bool) BoolValue {
return BoolValue{
func BoolValueExact(value bool) boolValueExact {
return boolValueExact{
value: value,
}
}
12 changes: 7 additions & 5 deletions knownvalue/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@ func TestBoolValue_CheckValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
self knownvalue.BoolValue
self knownvalue.Check
other any
expectedError error
}{
"zero-nil": {
expectedError: fmt.Errorf("expected bool value for BoolValue check, got: <nil>"),
self: knownvalue.BoolValueExact(false),
expectedError: fmt.Errorf("expected bool value for BoolValueExact check, got: <nil>"),
},
"zero-other": {
self: knownvalue.BoolValueExact(false),
other: false, // checking against the underlying value field zero-value
},
"nil": {
self: knownvalue.BoolValueExact(false),
expectedError: fmt.Errorf("expected bool value for BoolValue check, got: <nil>"),
expectedError: fmt.Errorf("expected bool value for BoolValueExact check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.BoolValueExact(true),
other: 1.23,
expectedError: fmt.Errorf("expected bool value for BoolValue check, got: float64"),
expectedError: fmt.Errorf("expected bool value for BoolValueExact check, got: float64"),
},
"not-equal": {
self: knownvalue.BoolValueExact(true),
other: false,
expectedError: fmt.Errorf("expected value true for BoolValue check, got: false"),
expectedError: fmt.Errorf("expected value true for BoolValueExact check, got: false"),
},
"equal": {
self: knownvalue.BoolValueExact(true),
Expand Down
20 changes: 9 additions & 11 deletions knownvalue/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,43 @@ import (
"strconv"
)

var _ Check = Float64Value{}
var _ Check = float64ValueExact{}

// Float64Value is a Check for asserting equality between the value supplied
// to Float64ValueExact and the value passed to the CheckValue method.
type Float64Value struct {
type float64ValueExact struct {
value float64
}

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

if !ok {
return fmt.Errorf("expected json.Number value for Float64Value check, got: %T", other)
return fmt.Errorf("expected json.Number value for Float64ValueExact 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)
return fmt.Errorf("expected json.Number to be parseable as float64 value for Float64ValueExact check: %s", err)
}

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

return nil
}

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

// Float64ValueExact returns a Check for asserting equality between the
// supplied float64 and the value passed to the CheckValue method.
func Float64ValueExact(value float64) Float64Value {
return Float64Value{
func Float64ValueExact(value float64) float64ValueExact {
return float64ValueExact{
value: value,
}
}
12 changes: 7 additions & 5 deletions knownvalue/float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@ func TestFloat64Value_CheckValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
self knownvalue.Float64Value
self knownvalue.Check
other any
expectedError error
}{
"zero-nil": {
expectedError: fmt.Errorf("expected json.Number value for Float64Value check, got: <nil>"),
self: knownvalue.Float64ValueExact(0),
expectedError: fmt.Errorf("expected json.Number value for Float64ValueExact check, got: <nil>"),
},
"zero-other": {
self: knownvalue.Float64ValueExact(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 Float64Value check, got: <nil>"),
expectedError: fmt.Errorf("expected json.Number value for Float64ValueExact check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.Float64ValueExact(1.234),
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"),
expectedError: fmt.Errorf("expected json.Number to be parseable as float64 value for Float64ValueExact check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
},
"not-equal": {
self: knownvalue.Float64ValueExact(1.234),
other: json.Number("4.321"),
expectedError: fmt.Errorf("expected value 1.234 for Float64Value check, got: 4.321"),
expectedError: fmt.Errorf("expected value 1.234 for Float64ValueExact check, got: 4.321"),
},
"equal": {
self: knownvalue.Float64ValueExact(1.234),
Expand Down
20 changes: 9 additions & 11 deletions knownvalue/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,43 @@ import (
"strconv"
)

var _ Check = Int64Value{}
var _ Check = int64ValueExact{}

// Int64Value is a Check for asserting equality between the value supplied
// to Int64ValueExact and the value passed to the CheckValue method.
type Int64Value struct {
type int64ValueExact struct {
value int64
}

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

if !ok {
return fmt.Errorf("expected json.Number value for Int64Value check, got: %T", other)
return fmt.Errorf("expected json.Number value for Int64ValueExact 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)
return fmt.Errorf("expected json.Number to be parseable as int64 value for Int64ValueExact check: %s", err)
}

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

return nil
}

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

// Int64ValueExact returns a Check for asserting equality between the
// supplied int64 and the value passed to the CheckValue method.
func Int64ValueExact(value int64) Int64Value {
return Int64Value{
func Int64ValueExact(value int64) int64ValueExact {
return int64ValueExact{
value: value,
}
}
12 changes: 7 additions & 5 deletions knownvalue/int64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@ func TestInt64Value_CheckValue(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
self knownvalue.Int64Value
self knownvalue.Check
other any
expectedError error
}{
"zero-nil": {
expectedError: fmt.Errorf("expected json.Number value for Int64Value check, got: <nil>"),
self: knownvalue.Int64ValueExact(0),
expectedError: fmt.Errorf("expected json.Number value for Int64ValueExact check, got: <nil>"),
},
"zero-other": {
self: knownvalue.Int64ValueExact(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 Int64Value check, got: <nil>"),
expectedError: fmt.Errorf("expected json.Number value for Int64ValueExact check, got: <nil>"),
},
"wrong-type": {
self: knownvalue.Int64ValueExact(1234),
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"),
expectedError: fmt.Errorf("expected json.Number to be parseable as int64 value for Int64ValueExact check: strconv.ParseInt: parsing \"str\": invalid syntax"),
},
"not-equal": {
self: knownvalue.Int64ValueExact(1234),
other: json.Number("4321"),
expectedError: fmt.Errorf("expected value 1234 for Int64Value check, got: 4321"),
expectedError: fmt.Errorf("expected value 1234 for Int64ValueExact check, got: 4321"),
},
"equal": {
self: knownvalue.Int64ValueExact(1234),
Expand Down
18 changes: 8 additions & 10 deletions knownvalue/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import (
"fmt"
)

var _ Check = ListValue{}
var _ Check = listValueExact{}

// ListValue is a Check for asserting equality between the value supplied
// to ListValueExact and the value passed to the CheckValue method.
type ListValue struct {
type listValueExact struct {
value []Check
}

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

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

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

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

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

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

for _, val := range v.value {
Expand All @@ -62,8 +60,8 @@ func (v ListValue) String() string {
// ListValueExact 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) ListValue {
return ListValue{
func ListValueExact(value []Check) listValueExact {
return listValueExact{
value: value,
}
}
20 changes: 9 additions & 11 deletions knownvalue/list_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import (
"strconv"
)

var _ Check = ListElements{}
var _ Check = listElementsExact{}

// ListElements is a Check for asserting equality between the value supplied
// to ListElementsExact and the value passed to the CheckValue method.
type ListElements struct {
type listElementsExact struct {
num int
}

// CheckValue verifies that the passed value is a list, map, object,
// or set, and contains a matching number of elements.
func (v ListElements) CheckValue(other any) error {
func (v listElementsExact) CheckValue(other any) error {
otherVal, ok := other.([]any)

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

if len(otherVal) != v.num {
Expand All @@ -37,21 +35,21 @@ func (v ListElements) CheckValue(other any) error {
actualElements = "element"
}

return fmt.Errorf("expected %d %s for ListElements check, got %d %s", v.num, expectedElements, len(otherVal), actualElements)
return fmt.Errorf("expected %d %s for ListElementsExact check, got %d %s", v.num, expectedElements, len(otherVal), actualElements)
}

return nil
}

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

// ListElementsExact returns a Check for asserting that
// a list num elements.
func ListElementsExact(num int) ListElements {
return ListElements{
// a list has num elements.
func ListElementsExact(num int) listElementsExact {
return listElementsExact{
num: num,
}
}
Loading

0 comments on commit d14429e

Please sign in to comment.