2023-09-23 20:44:49 +00:00
|
|
|
package opt_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-07-04 16:54:41 +00:00
|
|
|
"github.com/reiver/go-opt"
|
2023-09-23 20:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOptional_MarshalJSON_int(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct{
|
|
|
|
Value opt.Optional[int]
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected string
|
2023-09-23 20:44:49 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: opt.Something(-65536),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-65536`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(-65535),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-65535`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: opt.Something(-256),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-256`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(-255),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-255`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: opt.Something(-5),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-5`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(-4),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-4`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(-3),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-3`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(-2),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-2`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(-1),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `-1`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(0),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `0`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(1),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `1`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(2),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `2`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(3),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `3`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(4),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `4`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(5),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `5`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: opt.Something(255),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `255`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(256),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `256`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Value: opt.Something(65535),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `65535`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: opt.Something(65536),
|
2024-08-08 17:58:13 +00:00
|
|
|
Expected: `65536`,
|
2023-09-23 20:44:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for testNumber, test := range tests {
|
|
|
|
|
|
|
|
actualBytes, err := test.Value.MarshalJSON()
|
2024-08-08 17:58:13 +00:00
|
|
|
if nil != err {
|
|
|
|
t.Errorf("For test #%d, did not expect an error but actually got one." , testNumber)
|
2023-09-23 20:44:49 +00:00
|
|
|
t.Logf("ERROR: (%T) %s", err, err)
|
|
|
|
t.Logf("VALUE: %#v", test.Value)
|
|
|
|
continue
|
|
|
|
}
|
2024-08-08 17:58:13 +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-23 20:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOptional_MarshalJSON_int_fail(t *testing.T) {
|
|
|
|
|
|
|
|
var nothing opt.Optional[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
|
|
|
|
}
|
|
|
|
}
|