Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 committed Mar 8, 2022
1 parent 81bb305 commit 6c30ec7
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions internal/testrunner/runners/pipeline/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package pipeline
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -324,7 +324,6 @@ func TestDiffUlite(t *testing.T) {
var jsonUnmarshalUsingNumberTests = []struct {
name string
msg string
want interface{}
}{
{
name: "empty",
Expand All @@ -333,66 +332,96 @@ var jsonUnmarshalUsingNumberTests = []struct {
{
name: "string",
msg: `"message"`,
want: "message",
},
{
name: "long",
msg: "9223372036854773807",
want: json.Number("9223372036854773807"),
},
{
name: "array",
msg: "[1,2,3,4,5]",
want: []interface{}{json.Number("1"), json.Number("2"), json.Number("3"), json.Number("4"), json.Number("5")},
},
{
name: "object",
msg: `{"key":42}`,
want: map[string]interface{}{"key": json.Number("42")},
},
{
name: "object",
msg: `{"key":42}answer`, // Will error "invalid character 'a' after top-level value".
},
// Test whitespace parity with json.Unmarshal.
// Test extra data whitespace parity with json.Unmarshal for error parity.
{
name: "object",
msg: `{"key":42} `,
want: map[string]interface{}{"key": json.Number("42")},
},
{
name: "object",
msg: `{"key":42}` + "\t",
want: map[string]interface{}{"key": json.Number("42")},
},
{
name: "object",
msg: `{"key":42}` + "\r",
want: map[string]interface{}{"key": json.Number("42")},
},
{
name: "object",
msg: `{"key":42}` + "\n",
want: map[string]interface{}{"key": json.Number("42")},
},
{
name: "0x1p52+1",
msg: fmt.Sprint(uint64(0x1p52) + 1),
},
{
name: "0x1p53-1",
msg: fmt.Sprint(uint64(0x1p53) - 1),
},
// The following three cases will fail if json.Unmarshal is used in place
// of jsonUnmarshalUsingNumber, as they are past the cutover.
{
name: "0x1p53+1",
msg: fmt.Sprint(uint64(0x1p53) + 1),
},
{
name: "0x1p54+1",
msg: fmt.Sprint(uint64(0x1p54) + 1),
},
{
name: "long",
msg: "9223372036854773807",
},
}

func TestJsonUnmarshalUsingNumberTests(t *testing.T) {
func TestJsonUnmarshalUsingNumberRoundTrip(t *testing.T) {
// This tests that jsonUnmarshalUsingNumber behaves the same
// way as json.Unmarshal with the exception that numbers are
// not unmarshaled through float64. This is important to avoid
// low-bit truncation of long numeric values that are greater
// than or equal to 0x1p53, the limit of bijective equivalence
// with 64 bit-integers.

for _, test := range jsonUnmarshalUsingNumberTests {
t.Run(test.name, func(t *testing.T) {
var got interface{}
err := jsonUnmarshalUsingNumber([]byte(test.msg), &got)
jerr := json.Unmarshal([]byte(test.msg), new(interface{}))
var val interface{}
err := jsonUnmarshalUsingNumber([]byte(test.msg), &val)

// Confirm that we get the same errors with jsonUnmarshalUsingNumber
// as are returned by json.Unmarshal.
jerr := json.Unmarshal([]byte(test.msg), new(interface{}))
// String comparison because we are not returning a real json.SyntaxError.
if fmt.Sprint(err) != fmt.Sprint(jerr) {
t.Errorf("unexpected error: got:%#v want:%#v", err, jerr)
}
if err != nil {
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("unexpected result: got:%v want:%v", got, test.want)

// Confirm that we round-trip the message correctly without
// alteration beyond trailing whitespace.
got, err := json.Marshal(val)
if err != nil {
t.Errorf("unexpected error: got:%#v want:%#v", err, jerr)
}
// Truncate trailing whitespace from the input since it won't
// be rendered in the output. This set of space characters is
// defined in encoding/json/scanner.go as func isSpace.
want := strings.TrimRight(test.msg, " \t\r\n")
if string(got) != want {
t.Errorf("unexpected result: got:%v want:%v", val, want)
}
})
}
Expand Down

0 comments on commit 6c30ec7

Please sign in to comment.