Skip to content

Commit

Permalink
Make sure to treat "nan", "-nan", and "+nan" as identical
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Sep 30, 2023
1 parent 807f9ab commit fe8e1e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/toml-lang/toml-test
go 1.18

require (
github.com/BurntSushi/toml v1.2.2-0.20230116181612-fcbab7400715
github.com/BurntSushi/toml v1.3.3-0.20230930221951-d17285a02783
// no_term branch, which doesn't depend on x/term and x/sys
zgo.at/zli v0.0.0-20220603144954-fdab8cc4d9d8
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/BurntSushi/toml v1.2.2-0.20230116181612-fcbab7400715 h1:wpQ/gpMTRuiaIOlcOImLxe7XF7FQ/Q5vmrQDEAHpGmI=
github.com/BurntSushi/toml v1.2.2-0.20230116181612-fcbab7400715/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.3-0.20230930221951-d17285a02783 h1:q97mwa1LRDI6nt31wvg+nEVzHdgifAkGzXBIzS2KCEk=
github.com/BurntSushi/toml v1.3.3-0.20230930221951-d17285a02783/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
zgo.at/zli v0.0.0-20220603144954-fdab8cc4d9d8 h1:Cvmn2mATNsxwIVjKv91UHWdInUqjA/oWMH9kAUuzojQ=
zgo.at/zli v0.0.0-20220603144954-fdab8cc4d9d8/go.mod h1:HLAc12TjNGT+VRXr76JnsNE3pbooQtwKWhX+RlDjQ2Y=
1 change: 1 addition & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (r Test) cmpAsStrings(want, have string) Test {
func (r Test) cmpFloats(want, have string) Test {
// Special case for NaN, since NaN != NaN.
if strings.HasSuffix(want, "nan") || strings.HasSuffix(have, "nan") {
want, have := strings.TrimLeft(want, "-+"), strings.TrimLeft(have, "-+")
if want != have {
return r.fail("Values for key '%s' don't match:\n"+
" Expected: %v\n"+
Expand Down
37 changes: 37 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,40 @@ func TestCompareDatetime(t *testing.T) {
})
}
}

func TestCompareNaN(t *testing.T) {
a := map[string]any{
"nan": map[string]any{
"type": "float",
"value": "nan",
},
}
b := map[string]any{
"nan": map[string]any{
"type": "float",
"value": "+nan",
},
}
c := map[string]any{
"nan": map[string]any{
"type": "float",
"value": "-nan",
},
}

{
r := Test{}
r = r.CompareJSON(a, b)
if r.Failure != "" {
t.Fatal(r.Failure)
}
}

{
r := Test{}
r = r.CompareJSON(b, c)
if r.Failure != "" {
t.Fatal(r.Failure)
}
}
}

0 comments on commit fe8e1e2

Please sign in to comment.