1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-01 12:34:28 +00:00
arikawa/utils/json/enum/enum_test.go
2020-05-11 22:43:52 +02:00

84 lines
1.4 KiB
Go

package enum
import (
"reflect"
"testing"
)
func TestInt8ToJSON(t *testing.T) {
testCases := []struct {
name string
src Enum
expect []byte
}{
{
name: "null",
src: Null,
expect: []byte("null"),
},
{
name: "value",
src: 12,
expect: []byte("12"),
},
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
actual := ToJSON(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: Null,
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 := FromJSON(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())
}
}
})
}
}