Skip to content

Commit abb6d46

Browse files
knownvalue: address failing tests
1 parent 45f2d35 commit abb6d46

File tree

8 files changed

+42
-12
lines changed

8 files changed

+42
-12
lines changed

knownvalue/bool_func_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestBoolFunc_CheckValue(t *testing.T) {
2828
"wrong-type": {
2929
self: knownvalue.BoolFunc(func(bool) error { return nil }),
3030
other: json.Number("1.234"),
31-
expectedError: fmt.Errorf("expected bool value for BoolFunc check, got: float64"),
31+
expectedError: fmt.Errorf("expected bool value for BoolFunc check, got: json.Number"),
3232
},
3333
"failure": {
3434
self: knownvalue.BoolFunc(func(b bool) error {

knownvalue/float32_func_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func TestFloat32Func_CheckValue(t *testing.T) {
3030
other: "wrongtype",
3131
expectedError: fmt.Errorf("expected json.Number value for Float32Func check, got: string"),
3232
},
33+
"no-digits": {
34+
self: knownvalue.Float32Func(func(float32) error { return nil }),
35+
other: json.Number("str"),
36+
expectedError: fmt.Errorf("expected json.Number to be parseable as float32 value for Float32Func check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
37+
},
3338
"failure": {
3439
self: knownvalue.Float32Func(func(f float32) error {
3540
if f != 1.1 {

knownvalue/float64_func_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func TestFloat64Func_CheckValue(t *testing.T) {
3030
other: "wrongtype",
3131
expectedError: fmt.Errorf("expected json.Number value for Float64Func check, got: string"),
3232
},
33+
"no-digits": {
34+
self: knownvalue.Float64Func(func(float64) error { return nil }),
35+
other: json.Number("str"),
36+
expectedError: fmt.Errorf("expected json.Number to be parseable as float64 value for Float64Func check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
37+
},
3338
"failure": {
3439
self: knownvalue.Float64Func(func(f float64) error {
3540
if f != 1.1 {

knownvalue/int32_func_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func TestInt32Func_CheckValue(t *testing.T) {
3030
other: "wrongtype",
3131
expectedError: fmt.Errorf("expected json.Number value for Int32Func check, got: string"),
3232
},
33+
"no-digits": {
34+
self: knownvalue.Int32Func(func(int32) error { return nil }),
35+
other: json.Number("str"),
36+
expectedError: fmt.Errorf("expected json.Number to be parseable as int32 value for Int32Func check: strconv.ParseInt: parsing \"str\": invalid syntax"),
37+
},
3338
"failure": {
3439
self: knownvalue.Int32Func(func(i int32) error {
3540
if i != 1 {

knownvalue/int64_func_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ func TestInt64Func_CheckValue(t *testing.T) {
2323
}{
2424
"nil": {
2525
self: knownvalue.Int64Func(func(int64) error { return nil }),
26-
expectedError: fmt.Errorf("expected json.Number value for Int32Func check, got: <nil>"),
26+
expectedError: fmt.Errorf("expected json.Number value for Int64Func check, got: <nil>"),
2727
},
2828
"wrong-type": {
2929
self: knownvalue.Int64Func(func(int64) error { return nil }),
3030
other: "wrongtype",
31-
expectedError: fmt.Errorf("expected json.Number value for Int32Func check, got: string"),
31+
expectedError: fmt.Errorf("expected json.Number value for Int64Func check, got: string"),
32+
},
33+
"no-digits": {
34+
self: knownvalue.Int64Func(func(int64) error { return nil }),
35+
other: json.Number("str"),
36+
expectedError: fmt.Errorf("expected json.Number to be parseable as int64 value for Int64Func check: strconv.ParseInt: parsing \"str\": invalid syntax"),
3237
},
3338
"failure": {
3439
self: knownvalue.Int64Func(func(i int64) error {

knownvalue/number_func.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (v numberFunc) CheckValue(other any) error {
2626

2727
otherVal, _, err := big.ParseFloat(jsonNum.String(), 10, 512, big.ToNearestEven)
2828
if err != nil {
29-
return fmt.Errorf("expected json.Number to be parseable as int64 value for NumberFunc check: %s", err)
29+
return fmt.Errorf("expected json.Number to be parseable as big.Float value for NumberFunc check: %s", err)
3030
}
3131

3232
return v.checkFunc(otherVal)

knownvalue/number_func_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import (
1717
func TestNumberFunc_CheckValue(t *testing.T) {
1818
t.Parallel()
1919

20+
expected, _, err := big.ParseFloat("1.797693134862315797693134862315797693134862315", 10, 512, big.ToNearestEven)
21+
if err != nil {
22+
t.Errorf("%s", err)
23+
}
24+
2025
testCases := map[string]struct {
2126
self knownvalue.Check
2227
other any
@@ -31,24 +36,29 @@ func TestNumberFunc_CheckValue(t *testing.T) {
3136
other: "wrongtype",
3237
expectedError: fmt.Errorf("expected json.Number value for NumberFunc check, got: string"),
3338
},
39+
"no-digits": {
40+
self: knownvalue.NumberFunc(func(*big.Float) error { return nil }),
41+
other: json.Number("str"),
42+
expectedError: fmt.Errorf("expected json.Number to be parseable as big.Float value for NumberFunc check: number has no digits"),
43+
},
3444
"failure": {
3545
self: knownvalue.NumberFunc(func(i *big.Float) error {
36-
if i != big.NewFloat(1.667114241575161769818551140818851511176942075) {
37-
return fmt.Errorf("%f was not 1.667114241575161769818551140818851511176942075", i)
46+
if i.Cmp(expected) != 0 {
47+
return fmt.Errorf("%s was not %s", i.Text('f', -1), expected.Text('f', -1))
3848
}
3949
return nil
4050
}),
41-
other: json.Number("1.797693134862315797693134862315797693134862315"),
42-
expectedError: fmt.Errorf("%f was not 1.667114241575161769818551140818851511176942075", 1.797693134862315797693134862315797693134862315),
51+
other: json.Number("1.667114241575161769818551140818851511176942075"),
52+
expectedError: fmt.Errorf("1.667114241575161769818551140818851511176942075 was not 1.797693134862315797693134862315797693134862315"),
4353
},
4454
"success": {
4555
self: knownvalue.NumberFunc(func(i *big.Float) error {
46-
if i != big.NewFloat(1.667114241575161769818551140818851511176942075) {
47-
return fmt.Errorf("%f was not 1.667114241575161769818551140818851511176942075", i)
56+
if i.Cmp(expected) != 0 {
57+
return fmt.Errorf("%s was not %s", i.Text('f', -1), expected.Text('f', -1))
4858
}
4959
return nil
5060
}),
51-
other: json.Number("1.667114241575161769818551140818851511176942075"),
61+
other: json.Number("1.797693134862315797693134862315797693134862315"),
5262
},
5363
}
5464

knownvalue/string_func_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestStringFunc_String(t *testing.T) {
7070

7171
got := knownvalue.StringFunc(func(string) error { return nil }).String()
7272

73-
if diff := cmp.Diff(got, "stringFunc"); diff != "" {
73+
if diff := cmp.Diff(got, "StringFunc"); diff != "" {
7474
t.Errorf("unexpected difference: %s", diff)
7575
}
7676
}

0 commit comments

Comments
 (0)