Skip to content

Commit

Permalink
feat: in failure reports float64 type is now omitted (like int)
Browse files Browse the repository at this point in the history
but the value always contain a "." (except if ±Inf, Nan or with
exponant) to distinguish them from int.

Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Jan 3, 2022
1 parent 13f6a18 commit 515da4a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 19 deletions.
12 changes: 10 additions & 2 deletions internal/util/string.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -45,7 +45,15 @@ func ToString(val interface{}) string {
case int:
return strconv.Itoa(tval)

// no "(bool)" prefix for booleans
// no "(float64) " prefix for float64s
case float64:
s := strconv.FormatFloat(tval, 'g', -1, 64)
if strings.ContainsAny(s, "e.IN") { // I for Inf, N for NaN
return s
}
return s + ".0" // to distinguish from ints

// no "(bool) " prefix for booleans
case bool:
return TernStr(tval, "true", "false")

Expand Down
9 changes: 8 additions & 1 deletion internal/util/string_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand All @@ -8,6 +8,7 @@ package util_test

import (
"bytes"
"math"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -52,6 +53,12 @@ func TestToString(t *testing.T) {
{paramGot: true, expected: "true"},
{paramGot: false, expected: "false"},
{paramGot: int64(42), expected: "(int64) 42"},
{paramGot: float64(42), expected: "42.0"},
{paramGot: float64(42.56), expected: "42.56"},
{paramGot: float64(4e56), expected: "4e+56"},
{paramGot: math.Inf(1), expected: "+Inf"},
{paramGot: math.Inf(-1), expected: "-Inf"},
{paramGot: math.NaN(), expected: "NaN"},
} {
test.EqualStr(t, util.ToString(curTest.paramGot), curTest.expected)
}
Expand Down
6 changes: 3 additions & 3 deletions td/equal_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -594,8 +594,8 @@ func TestEqualOthers(t *testing.T) {
expectedError{
Message: mustBe("values differ"),
Path: mustBe("DATA.numf64"),
Got: mustBe("(float64) 1"),
Expected: mustBe("(float64) 2"),
Got: mustBe("1.0"),
Expected: mustBe("2.0"),
})

checkError(t, Private{numc64: complex(1, 2)}, Private{numc64: complex(2, 1)},
Expand Down
4 changes: 2 additions & 2 deletions td/td_json_pointer_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2020, Maxime Soulé
// Copyright (c) 2020-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestJSONPointer(t *testing.T) {
expectedError{
Message: mustBe("values differ"),
Path: mustBe("DATA.JSONPointer</foo>"),
Got: mustBe(`(float64) 42`),
Got: mustBe(`42.0`),
Expected: mustBe(`nil`),
})

Expand Down
6 changes: 3 additions & 3 deletions td/td_nan_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -28,7 +28,7 @@ func TestNaN(t *testing.T) {
expectedError{
Message: mustBe("values differ"),
Path: mustBe("DATA"),
Got: mustBe("(float64) 12"),
Got: mustBe("12.0"),
Expected: mustBe("NaN"),
})

Expand Down Expand Up @@ -56,7 +56,7 @@ func TestNotNaN(t *testing.T) {
expectedError{
Message: mustBe("values differ"),
Path: mustBe("DATA"),
Got: mustBe("(float64) NaN"),
Got: mustBe("NaN"),
Expected: mustBe("not NaN"),
})

Expand Down
4 changes: 2 additions & 2 deletions td/td_none_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -38,7 +38,7 @@ func TestNone(t *testing.T) {
expectedError{
Message: mustBe("comparing with None (part 1 of 2 is OK)"),
Path: mustBe("DATA"),
Got: mustBe("(float64) 6"),
Got: mustBe("6.0"),
Expected: mustBe("None(6,\n 7)"),
})

Expand Down
4 changes: 2 additions & 2 deletions td/td_set_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestSet(t *testing.T) {
expectedError{
Message: mustBe("comparing %% as a NotAny"),
Path: mustBe("DATA"),
Summary: mustBe("Extra item: ((float64) 3)"),
Summary: mustBe("Extra item: (3.0)"),
},
testName)
}
Expand Down
8 changes: 4 additions & 4 deletions td/td_zero_test.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Maxime Soulé
// Copyright (c) 2018-2022, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -46,8 +46,8 @@ func TestZero(t *testing.T) {
expectedError{
Message: mustBe("values differ"),
Path: mustBe("DATA"),
Got: mustBe("(float64) 12"),
Expected: mustBe("(float64) 0"),
Got: mustBe("12.0"),
Expected: mustBe("0.0"),
})
checkError(t, map[string]int{}, td.Zero(),
expectedError{
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestNotZero(t *testing.T) {
expectedError{
Message: mustBe("zero value"),
Path: mustBe("DATA"),
Got: mustBe("(float64) 0"),
Got: mustBe("0.0"),
Expected: mustBe("NotZero()"),
})
checkError(t, (map[string]int)(nil), td.NotZero(),
Expand Down

0 comments on commit 515da4a

Please sign in to comment.