From e4d83b62602701cb2faea1bd76dd4b015202b276 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 25 Sep 2023 03:50:04 +0900 Subject: [PATCH] initail commits --- int_marshaljson_test.go | 210 +++++++++++++++++++++++++++++++ int_unmarshaljson_test.go | 253 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 463 insertions(+) create mode 100644 int_marshaljson_test.go create mode 100644 int_unmarshaljson_test.go diff --git a/int_marshaljson_test.go b/int_marshaljson_test.go new file mode 100644 index 0000000..58de042 --- /dev/null +++ b/int_marshaljson_test.go @@ -0,0 +1,210 @@ +package jsonint_test + +import ( + "testing" + + "sourcecode.social/reiver/go-jsonint" +) + +func TestInt_MarshalJSON(t *testing.T) { + + tests := []struct{ + Int jsonint.Int + Expected string + }{ + { + Int: jsonint.Int64(-65537), + Expected: "-65537", + }, + { + Int: jsonint.Int64(-65536), + Expected: "-65536", + }, + { + Int: jsonint.Int64(-65535), + Expected: "-65535", + }, + + { + Int: jsonint.Int64(-255), + Expected: "-255", + }, + + { + Int: jsonint.Int64(-5), + Expected: "-5", + }, + { + Int: jsonint.Int64(-4), + Expected: "-4", + }, + { + Int: jsonint.Int64(-3), + Expected: "-3", + }, + { + Int: jsonint.Int64(-2), + Expected: "-2", + }, + { + Int: jsonint.Int64(-1), + Expected: "-1", + }, + { + Int: jsonint.Int64(0), + Expected: "0", + }, + { + Int: jsonint.Int64(1), + Expected: "1", + }, + { + Int: jsonint.Int64(2), + Expected: "2", + }, + { + Int: jsonint.Int64(3), + Expected: "3", + }, + { + Int: jsonint.Int64(4), + Expected: "4", + }, + { + Int: jsonint.Int64(5), + Expected: "5", + }, + + { + Int: jsonint.Int64(255), + Expected: "255", + }, + + { + Int: jsonint.Int64(65535), + Expected: "65535", + }, + { + Int: jsonint.Int64(65536), + Expected: "65536", + }, + { + Int: jsonint.Int64(65537), + Expected: "65537", + }, + + + + { + Int: jsonint.Uint64(0), + Expected: "0", + }, + { + Int: jsonint.Uint64(1), + Expected: "1", + }, + { + Int: jsonint.Uint64(2), + Expected: "2", + }, + { + Int: jsonint.Uint64(3), + Expected: "3", + }, + { + Int: jsonint.Uint64(4), + Expected: "4", + }, + { + Int: jsonint.Uint64(5), + Expected: "5", + }, + + { + Int: jsonint.Uint64(255), + Expected: "255", + }, + { + Int: jsonint.Uint64(256), + Expected: "256", + }, + { + Int: jsonint.Uint64(257), + Expected: "257", + }, + + { + Int: jsonint.Uint64(65535), + Expected: "65535", + }, + { + Int: jsonint.Uint64(65536), + Expected: "65536", + }, + { + Int: jsonint.Uint64(65537), + Expected: "65537", + }, + + { + Int: jsonint.Uint64(4294967295), + Expected: "4294967295", + }, + { + Int: jsonint.Uint64(4294967296), + Expected: "4294967296", + }, + { + Int: jsonint.Uint64(4294967297), + Expected: "4294967297", + }, + + { + Int: jsonint.Uint64(18446744073709551614), + Expected: "18446744073709551614", + }, + { + Int: jsonint.Uint64(18446744073709551615), + Expected: "18446744073709551615", + }, + } + + for testNumber, test := range tests { + + actualBytes, err := test.Int.MarshalJSON() + if nil != err { + t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber) + t.Errorf("ERROR: (%T) %s", err, err) + continue + } + + { + actual := string(actualBytes) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual result from MarshalJSON() is not what was expected.", testNumber) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + continue + } + } + } +} + +func TestInt_MarshalJSON_fail(t *testing.T) { + + var datum jsonint.Int + + actualBytes, err := datum.MarshalJSON() + if nil == err { + t.Error("Expected an error but did not actually get one.") + return + } + if nil != actualBytes { + t.Error("Expected return bytes to be nil but actually wasn't.") + t.Logf("ACTUAL BYTES: %#v", actualBytes) + t.Logf("ACTUAL BYTES: %q", actualBytes) + return + } +} diff --git a/int_unmarshaljson_test.go b/int_unmarshaljson_test.go new file mode 100644 index 0000000..cca04c7 --- /dev/null +++ b/int_unmarshaljson_test.go @@ -0,0 +1,253 @@ +package jsonint_test + +import ( + "testing" + + "sourcecode.social/reiver/go-jsonint" +) + +func TestInt_UnmarshalJSON(t *testing.T) { + + tests := []struct { + JSON []byte + Expected jsonint.Int + }{ + { + JSON: []byte("-257"), + Expected: jsonint.Int64(-257), + }, + { + JSON: []byte("-256"), + Expected: jsonint.Int64(-256), + }, + { + JSON: []byte("-255"), + Expected: jsonint.Int64(-255), + }, + { + JSON: []byte("-254"), + Expected: jsonint.Int64(-254), + }, + + { + JSON: []byte("-5"), + Expected: jsonint.Int64(-5), + }, + { + JSON: []byte("-4"), + Expected: jsonint.Int64(-4), + }, + { + JSON: []byte("-3"), + Expected: jsonint.Int64(-3), + }, + { + JSON: []byte("-2"), + Expected: jsonint.Int64(-2), + }, + { + JSON: []byte("-1"), + Expected: jsonint.Int64(-1), + }, + + + + { + JSON: []byte("0"), + Expected: jsonint.Int64(0), + }, + { + JSON: []byte("0"), + Expected: jsonint.Uint64(0), + }, + + + + { + JSON: []byte("1"), + Expected: jsonint.Int64(1), + }, + { + JSON: []byte("1"), + Expected: jsonint.Uint64(1), + }, + { + JSON: []byte("+1"), + Expected: jsonint.Int64(1), + }, + { + JSON: []byte("+1"), + Expected: jsonint.Uint64(1), + }, + + + + { + JSON: []byte("2"), + Expected: jsonint.Int64(2), + }, + { + JSON: []byte("2"), + Expected: jsonint.Uint64(2), + }, + { + JSON: []byte("+2"), + Expected: jsonint.Int64(2), + }, + { + JSON: []byte("+2"), + Expected: jsonint.Uint64(2), + }, + + + + { + JSON: []byte("3"), + Expected: jsonint.Int64(3), + }, + { + JSON: []byte("3"), + Expected: jsonint.Uint64(3), + }, + { + JSON: []byte("+3"), + Expected: jsonint.Int64(3), + }, + { + JSON: []byte("+3"), + Expected: jsonint.Uint64(3), + }, + + + + { + JSON: []byte("4"), + Expected: jsonint.Int64(4), + }, + { + JSON: []byte("4"), + Expected: jsonint.Uint64(4), + }, + { + JSON: []byte("+4"), + Expected: jsonint.Int64(4), + }, + { + JSON: []byte("+4"), + Expected: jsonint.Uint64(4), + }, + + + + { + JSON: []byte("5"), + Expected: jsonint.Int64(5), + }, + { + JSON: []byte("5"), + Expected: jsonint.Uint64(5), + }, + { + JSON: []byte("5"), + Expected: jsonint.Int64(+5), + }, + { + JSON: []byte("5"), + Expected: jsonint.Uint64(+5), + }, + + + + + + + { + JSON: []byte("254"), + Expected: jsonint.Int64(254), + }, + { + JSON: []byte("254"), + Expected: jsonint.Uint64(254), + }, + { + JSON: []byte("+254"), + Expected: jsonint.Int64(254), + }, + { + JSON: []byte("+254"), + Expected: jsonint.Uint64(254), + }, + + + + { + JSON: []byte("255"), + Expected: jsonint.Int64(255), + }, + { + JSON: []byte("255"), + Expected: jsonint.Uint64(255), + }, + { + JSON: []byte("+255"), + Expected: jsonint.Int64(255), + }, + { + JSON: []byte("+255"), + Expected: jsonint.Uint64(255), + }, + + + + { + JSON: []byte("256"), + Expected: jsonint.Int64(256), + }, + { + JSON: []byte("256"), + Expected: jsonint.Uint64(256), + }, + { + JSON: []byte("+256"), + Expected: jsonint.Int64(256), + }, + { + JSON: []byte("+256"), + Expected: jsonint.Uint64(256), + }, + + + + { + JSON: []byte("257"), + Expected: jsonint.Int64(257), + }, + { + JSON: []byte("257"), + Expected: jsonint.Uint64(257), + }, + { + JSON: []byte("+257"), + Expected: jsonint.Int64(257), + }, + { + JSON: []byte("+257"), + Expected: jsonint.Uint64(257), + }, + } + + for testNumber, test := range tests { + + var actualInt jsonint.Int + + err := actualInt.UnmarshalJSON(test.JSON) + if nil != err { + t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber) + t.Logf("ERROR: (%T) %s", err, err) + t.Logf("JSON: %#v", test.JSON) + t.Logf("JSON: %q", test.JSON) + t.Logf("EXPECTED: %#v", test.Expected) + continue + } + } +}