Skip to content

Commit

Permalink
-int-as-float also skips int64 tests
Browse files Browse the repository at this point in the history
Also add some tests for max float64 values.

Fixes #154
  • Loading branch information
arp242 committed May 23, 2024
1 parent 88ec5ad commit 0ce094c
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 8 deletions.
3 changes: 3 additions & 0 deletions cmd/toml-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func parseFlags() (tomltest.Runner, []string, int, string, bool, bool) {
IntAsFloat: intAsFloat.Bool(),
Errors: errs,
}
if intAsFloat.Bool() {
r.SkipTests = append(r.SkipTests, "valid/integer/long")
}

if len(f.Args) == 0 && !listFiles.Bool() {
zli.Fatalf("no parser command")
Expand Down
7 changes: 5 additions & 2 deletions cmd/toml-test/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Flags:
Quote glob characters so they won't be picked up by the
shell.
Supported paterns: https://godocs.io/path/filepath#Match
Supported patterns: https://godocs.io/path/filepath#Match
-skip Tests to skip, this uses the same syntax as the -run flag.
Expand All @@ -103,7 +103,10 @@ Flags:
useful to get a list of "known failures" for CI integrations
and such.
-int-as-float Treat all integers as floats, rather than integers.
-int-as-float Treat all integers as floats, rather than integers. This also
skips the int64 test as that's outside of the safe float
range (it still tests the boundary of safe float64 natural
numbers).
-errors TOML or JSON file with expected errors for invalid test
files; an invalid test is considered to be "failed" if the
Expand Down
10 changes: 10 additions & 0 deletions tests/valid/float/max-int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"max_float": {
"type": "float",
"value": "9007199254740991"
},
"min_float": {
"type": "float",
"value": "-9007199254740991"
}
}
3 changes: 3 additions & 0 deletions tests/valid/float/max-int.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Maximum and minimum safe natural numbers.
max_float = 9_007_199_254_740_991.0
min_float = -9_007_199_254_740_991.0
10 changes: 10 additions & 0 deletions tests/valid/integer/float64-max.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"max_int": {
"type": "integer",
"value": "9007199254740991"
},
"min_int": {
"type": "integer",
"value": "-9007199254740991"
}
}
4 changes: 4 additions & 0 deletions tests/valid/integer/float64-max.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Maximum and minimum safe float64 natural numbers. Mainly here for
# -int-as-float.
max_int = 9_007_199_254_740_991
min_int = -9_007_199_254_740_991
22 changes: 16 additions & 6 deletions toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"reflect"
"sort"
"strconv"
"strings"
"time"
)
Expand All @@ -21,16 +22,16 @@ func (r Test) CompareTOML(want, have any) Test {
if isTomlValue(want) {
if !isTomlValue(have) {
return r.fail("Type for key %q differs:\n"+
" Expected: %v (%s)\n"+
" Your encoder: %v (%s)",
r.Key, want, fmtType(want), have, fmtType(have))
" Expected: %s (%s)\n"+
" Your encoder: %s (%s)",
r.Key, fmtVal(want), fmtType(want), fmtVal(have), fmtType(have))
}

if !deepEqual(want, have) {
return r.fail("Values for key %q differ:\n"+
" Expected: %v (%s)\n"+
" Your encoder: %v (%s)",
r.Key, want, fmtType(want), have, fmtType(have))
" Expected: %s (%s)\n"+
" Your encoder: %s (%s)",
r.Key, fmtVal(want), fmtType(want), fmtVal(have), fmtType(have))
}
return r
}
Expand Down Expand Up @@ -156,6 +157,15 @@ func isTomlValue(v any) bool {
func fmtType(t any) string { return strings.ReplaceAll(fmt.Sprintf("%T", t), "interface {}", "any") }
func fmtHashV(t any) string { return strings.ReplaceAll(fmt.Sprintf("%#v", t), "interface {}", "any") }

func fmtVal(v any) string {
switch vv := v.(type) {
case float64:
return strconv.FormatFloat(vv, 'f', -1, 64)
default:
return fmt.Sprintf("%v", vv)
}
}

func mapKeys[M ~map[string]V, V any](m M) []string {
keys := make([]string, 0, len(m))
for k := range m {
Expand Down

0 comments on commit 0ce094c

Please sign in to comment.