1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-09 16:35:12 +00:00
arikawa/utils/json/nullable/enum_test.go
2020-05-11 04:30:34 +02:00

84 lines
1.4 KiB
Go

package nullable
import (
"reflect"
"testing"
)
func TestInt8ToJSON(t *testing.T) {
testCases := []struct {
name string
src Enum
expect []byte
}{
{
name: "null",
src: EnumNull,
expect: []byte("null"),
},
{
name: "value",
src: 12,
expect: []byte("12"),
},
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
actual := EnumToJSON(c.src)
if !reflect.DeepEqual(actual, c.expect) {
t.Errorf("expected nullable.Int8ToJSON to return: %+v, but got: %+v", c.expect, actual)
}
})
}
}
func TestInt8FromJSON(t *testing.T) {
testCases := []struct {
name string
src []byte
expect Enum
err bool
}{
{
name: "null",
src: []byte("null"),
expect: EnumNull,
err: false,
},
{
name: "value",
src: []byte("12"),
expect: 12,
err: false,
},
{
name: "invalid input",
src: []byte("NaN"),
expect: 0,
err: true,
},
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
actual, err := EnumFromJSON(c.src)
if c.err {
if err == nil {
t.Error("expected nullable.Int8FromJSON to return an error, but it did not")
}
} else {
if !reflect.DeepEqual(actual, c.expect) {
t.Errorf("expected nullable.Int8FromJSON to return: %+v, but got: %+v", c.expect, actual)
}
if err != nil {
t.Errorf("nullable.Int8FromJSON returned an error: %s", err.Error())
}
}
})
}
}