-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrj_test.go
71 lines (60 loc) · 1.59 KB
/
rj_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
66
67
68
69
70
71
package rj
import "testing"
func TestParse(t *testing.T) {
in := `name: "a\n"
array: ["a","b"]`
node, err := ParseString(in)
if err != nil {
t.Error("ParseString failed, expected no error, got:", err)
} else {
n := node.GetString("name")
if n != "a\n" {
t.Error("ParseString failed, expected: a\n, got: ", n)
}
array := node.GetStringArray("array")
expected := []string{"a", "b"}
if !arrayEquals(array, expected) {
t.Error("ParseString failed, expected:", expected, ", got: ", array)
}
}
in = `name: "a\c"
array: ["a","b"]`
node, err = ParseString(in)
if err != nil {
name := node.GetString("name")
if name != "" {
t.Error("ParseString failed, expect empty string because of escape error, but got:", name)
}
arr := node.GetStringArray("array")
if !arrayEquals(arr, []string{"a", "b"}) {
t.Error("ParseString failed, expect correct array since we should skip error lines")
}
} else {
t.Error("ParseString failed, expect invalid escape error, but got nil")
}
}
func TestMarshal(t *testing.T) {
type s struct {
Name string
Age int
}
in := `Name: "abc"
Age: 12
`
s1 := new(s)
err := Unmarshal([]byte(in), s1)
if err == nil {
if s1.Name != "abc" {
t.Error("Test unmarshal failed, input:", in, ", expected s1.Name: abc, got:", s1.Name)
}
if s1.Age != 12 {
t.Error("Test unmarshal failed, input:", in, ", expected s1.Age: 12, got:", s1.Age)
}
bytes := Marshal(s1)
if string(bytes) != in {
t.Error("Test marshal failed, expected: ", in, ", got:", string(bytes))
}
} else {
t.Error("Test unmarshal failed, expected no error, got:", err)
}
}