initail commits

master
Charles Iliya Krempeaux 2023-09-25 03:50:04 +09:00
parent 7956a46cfd
commit e4d83b6260
2 changed files with 463 additions and 0 deletions

View File

@ -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
}
}

View File

@ -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
}
}
}