1 Star 0 Fork 1

winie/sq

forked from unsafe-rust/sq 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
12_json_test.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
unsafe-rust 提交于 2021-03-28 10:11 . update
package sq
import (
"encoding/json"
"reflect"
"testing"
)
func TestJsonObject(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
want json.RawMessage
wantErr bool
}{
{
name: "test1",
args: args{
value: nil,
},
want: emptyJSON,
wantErr: false,
},
{
name: "test2",
args: args{
value: []byte(""),
},
want: emptyJSON,
wantErr: false,
},
{
name: "test3",
args: args{
value: "",
},
want: emptyJSON,
wantErr: false,
},
{
name: "test4",
args: args{
value: `{"other":1}`,
},
want: []byte(`{"other":1}`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := JsonObject(tt.args.value)
if (err != nil) != tt.wantErr {
t.Errorf("JsonValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("JsonValue() got = %v, want %v", string(got), string(tt.want))
}
})
}
}
func TestJSONText(t *testing.T) {
j := JSONText(`{"foo": 1, "bar": 2}`)
v, err := j.Value()
if err != nil {
t.Errorf("Was not expecting an error")
}
err = (&j).Scan(v)
if err != nil {
t.Errorf("Was not expecting an error")
}
m := map[string]interface{}{}
j.Unmarshal(&m)
if m["foo"].(float64) != 1 || m["bar"].(float64) != 2 {
t.Errorf("Expected valid json but got some garbage instead? %#v", m)
}
j = JSONText(`{"foo": 1, invalid, false}`)
v, err = j.Value()
if err == nil {
t.Errorf("Was expecting invalid json to fail!")
}
j = JSONText("")
v, err = j.Value()
if err != nil {
t.Errorf("Was not expecting an error")
}
err = (&j).Scan(v)
if err != nil {
t.Errorf("Was not expecting an error")
}
j = JSONText(nil)
v, err = j.Value()
if err != nil {
t.Errorf("Was not expecting an error")
}
err = (&j).Scan(v)
if err != nil {
t.Errorf("Was not expecting an error")
}
t.Run("Binary", func(t *testing.T) {
j := JSONText(`{"foo": 1, "bar": 2}`)
v, err := j.MarshalBinary()
if err != nil {
t.Errorf("Was not expecting an error")
}
if string(v) != `{"foo": 1, "bar": 2}` {
t.Errorf("MarshalBinary result error")
}
err = (&j).UnmarshalBinary(v)
if err != nil {
t.Errorf("Was not expecting an error")
}
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/winie_admin/sq.git
git@gitee.com:winie_admin/sq.git
winie_admin
sq
sq
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385