2023-09-24 00:51:00 +00:00
|
|
|
package nul_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-08-01 23:28:38 +00:00
|
|
|
"github.com/reiver/go-nul"
|
2023-09-24 00:51:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNullable_MarshalJSON_int(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct{
|
|
|
|
Value nul.Nullable[int]
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected string
|
2023-09-24 00:51:00 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: nul.Null[int](),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: "null",
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: nul.Something(-65536),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-65536`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(-65535),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-65535`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: nul.Something(-256),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-256`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(-255),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-255`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: nul.Something(-5),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-5`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(-4),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-4`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(-3),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-3`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(-2),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-2`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(-1),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `-1`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(0),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `0`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(1),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `1`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(2),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `2`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(3),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `3`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
2023-09-24 00:56:21 +00:00
|
|
|
{
|
2023-09-24 00:51:00 +00:00
|
|
|
Value: nul.Something(4),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `4`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(5),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `5`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: nul.Something(255),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `255`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(256),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `256`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: nul.Something(65535),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `65535`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: nul.Something(65536),
|
2024-08-09 03:35:44 +00:00
|
|
|
Expected: `65536`,
|
2023-09-24 00:51:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for testNumber, test := range tests {
|
|
|
|
|
|
|
|
actualBytes, err := test.Value.MarshalJSON()
|
2024-08-09 03:35:44 +00:00
|
|
|
if nil != err {
|
|
|
|
t.Errorf("For test #%d, did not expect an error but actually got one." , testNumber)
|
2023-09-24 00:51:00 +00:00
|
|
|
t.Logf("ERROR: (%T) %s", err, err)
|
|
|
|
t.Logf("VALUE: %#v", test.Value)
|
|
|
|
continue
|
|
|
|
}
|
2024-08-09 03:35:44 +00:00
|
|
|
|
|
|
|
actual := string(actualBytes)
|
|
|
|
expected := test.Expected
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("For test #%d, the actual value for the JSON marshaling is not what was expected.", testNumber)
|
|
|
|
t.Logf("EXPECTED: %q", expected)
|
|
|
|
t.Logf("ACTUAL: %q", actual)
|
|
|
|
t.Logf("VALUE: %#v", test.Value)
|
|
|
|
continue
|
|
|
|
}
|
2023-09-24 00:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNullable_MarshalJSON_int_fail(t *testing.T) {
|
|
|
|
|
|
|
|
var nothing nul.Nullable[int]
|
|
|
|
|
|
|
|
actualBytes, err := nothing.MarshalJSON()
|
|
|
|
if nil == err {
|
|
|
|
t.Error("Expected an error but did not actually get one.")
|
|
|
|
t.Logf("ACTUAL: %q", actualBytes)
|
|
|
|
t.Logf("ERROR: (%T) %s", err, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if nil != actualBytes {
|
|
|
|
t.Error("Expected not bytes but actually get some.")
|
|
|
|
t.Logf("ACTUAL: %q", actualBytes)
|
|
|
|
t.Logf("ERROR: (%T) %s", err, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|