Ai
1 Star 0 Fork 0

idsutong/gqlgen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
int_test.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
Adam Scarr 提交于 2019-03-04 10:49 +08:00 . Automatically bind to int32 and int64
package graphql
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInt(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
assert.Equal(t, "123", m2s(MarshalInt(123)))
})
t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, 123, mustUnmarshalInt(123))
assert.Equal(t, 123, mustUnmarshalInt(int64(123)))
assert.Equal(t, 123, mustUnmarshalInt(json.Number("123")))
assert.Equal(t, 123, mustUnmarshalInt("123"))
})
}
func mustUnmarshalInt(v interface{}) int {
res, err := UnmarshalInt(v)
if err != nil {
panic(err)
}
return res
}
func TestInt32(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
assert.Equal(t, "123", m2s(MarshalInt32(123)))
})
t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, int32(123), mustUnmarshalInt32(123))
assert.Equal(t, int32(123), mustUnmarshalInt32(int64(123)))
assert.Equal(t, int32(123), mustUnmarshalInt32(json.Number("123")))
assert.Equal(t, int32(123), mustUnmarshalInt32("123"))
})
}
func mustUnmarshalInt32(v interface{}) int32 {
res, err := UnmarshalInt32(v)
if err != nil {
panic(err)
}
return res
}
func TestInt64(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
assert.Equal(t, "123", m2s(MarshalInt32(123)))
})
t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, int64(123), mustUnmarshalInt64(123))
assert.Equal(t, int64(123), mustUnmarshalInt64(int64(123)))
assert.Equal(t, int64(123), mustUnmarshalInt64(json.Number("123")))
assert.Equal(t, int64(123), mustUnmarshalInt64("123"))
})
}
func mustUnmarshalInt64(v interface{}) int64 {
res, err := UnmarshalInt64(v)
if err != nil {
panic(err)
}
return res
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/idsutong/gqlgen.git
git@gitee.com:idsutong/gqlgen.git
idsutong
gqlgen
gqlgen
master

搜索帮助