1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-16 20:03:38 +00:00
arikawa/utils/json/enum/enum_test.go

84 lines
1.4 KiB
Go
Raw Normal View History

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