go-tmp/temporal_marshaljson_bool_t...

99 lines
2.0 KiB
Go
Raw Normal View History

2023-11-02 01:36:14 +00:00
package tmp_test
import (
"testing"
2023-11-02 02:22:11 +00:00
"time"
2023-11-02 01:36:14 +00:00
"sourcecode.social/reiver/go-tmp"
)
func TestTemporal_MarshalJSON_bool(t *testing.T) {
tests := []struct{
Value tmp.Temporal[bool]
Expected string
}{
{
Value: tmp.Permanent(false),
Expected: "false",
},
{
Value: tmp.Permanent(true),
Expected: "true",
},
{
2023-11-02 02:22:11 +00:00
Value: tmp.Temporary(false, time.Unix(9_223_372_036_854_775_807,0)), // supposed to be a time far in the future
2023-11-02 01:36:14 +00:00
Expected: "false",
},
{
2023-11-02 02:22:11 +00:00
Value: tmp.Temporary(true, time.Unix(9_223_372_036_854_775_806,0)), // supposed to be a time far in the future
2023-11-02 01:36:14 +00:00
Expected: "true",
},
}
for testNumber, test := range tests {
actualBytes, err := test.Value.MarshalJSON()
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("VALUE: %#v", test.Value)
continue
}
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
}
}
}
func TestTemporal_MarshalJSON_bool_fail(t *testing.T) {
tests := []struct{
Value tmp.Temporal[bool]
}{
{
Value: tmp.Nothing[bool](),
},
{
2023-11-02 02:22:11 +00:00
Value: tmp.Temporary(false, time.Unix(1234,0)), // supposed to be a time that is already expired
2023-11-02 01:36:14 +00:00
},
{
2023-11-02 02:22:11 +00:00
Value: tmp.Temporary(true, time.Unix(5678,0)), // supposed to be a time that is already expired
2023-11-02 01:36:14 +00:00
},
}
for testNumber, test := range tests {
actualBytes, err := test.Value.MarshalJSON()
if nil == err {
t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber)
t.Logf("ACTUAL: %q", actualBytes)
t.Logf("ERROR: (%T) %s", err, err)
continue
}
if nil != actualBytes {
t.Errorf("For test #%d, expected not bytes but actually get some.", testNumber)
t.Logf("ACTUAL: %q", actualBytes)
t.Logf("ERROR: (%T) %s", err, err)
continue
}
}
}