-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
65 lines (56 loc) · 1.28 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package fetch
import (
"reflect"
"testing"
)
func TestUnmarshal(t *testing.T) {
type testCase struct {
V string
E any
}
var cases = []testCase{
{V: `{"name":"1"}`, E: Pet{Name: "1"}},
{V: `{"name":"2"}`, E: PetLowerCaseTag{Name: "2"}},
{V: `{"Name":"3"}`, E: PetUpperCaseTag{Name: "3"}},
}
for _, c := range cases {
p := reflect.New(reflect.TypeOf(c.E)).Interface()
err := UnmarshalInto(c.V, p)
if err != nil {
t.Fatalf("Unmarshal error: %s", err)
}
got := reflect.ValueOf(p).Elem().Interface()
if got != c.E {
t.Errorf("Marshal result mismatch, got=%v, expected=%v", got, c.E)
}
}
}
func TestUnmarshalString(t *testing.T) {
_, err := Unmarshal[string]("hello")
if err == nil {
t.Fatal(err)
}
}
func TestUnmarshalJ(t *testing.T) {
var n Nil
_, err := UnmarshalJ[string](n)
if err == nil {
t.Fatalf("nil shouldn't be unmarshaled")
}
r, err := UnmarshalJ[map[string]string](M{"name": "Lola"})
if err != nil {
t.Fatal("UnmarshalJ error:", err)
}
if r["name"] != "Lola" {
t.Errorf("UnmarshalJ result mismatch, got=%s", r)
}
}
func TestParse(t *testing.T) {
num, ok := Parse(`{"num":1}`).Q("num").AsNumber()
if !ok || num != 1 {
t.Errorf("parsing failed")
}
if !Parse("{whatisthis?").IsNil() {
t.Errorf("wrong json should return nil")
}
}