-
Notifications
You must be signed in to change notification settings - Fork 9
/
json_test.go
99 lines (90 loc) · 1.81 KB
/
json_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package php_test
import (
"github.com/hyperjiang/php"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Image Functions", func() {
Describe("JSONEncode", func() {
It("happy flow", func() {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
tests := []struct {
input any
want string
}{
{
1,
"1",
},
{
1.2,
"1.2",
},
{
"abc",
"\"abc\"",
},
{
[]int{1, 2, 3, 4, 5},
"[1,2,3,4,5]",
},
{
map[string]int{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5},
"{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}",
},
{
group,
"{\"ID\":1,\"Name\":\"Reds\",\"Colors\":[\"Crimson\",\"Red\",\"Ruby\",\"Maroon\"]}",
},
{
nil,
"null",
},
{
false,
"false",
},
}
for _, t := range tests {
got, err := php.JSONEncode(t.input)
Expect(err).NotTo(HaveOccurred())
Expect(got).To(Equal(t.want))
}
})
It("invalid input", func() {
_, err := php.JSONEncode(func() {})
Expect(err).To(HaveOccurred())
})
})
Describe("JSONDecode", func() {
It("decode into map", func() {
var m = make(map[string]any)
err := php.JSONDecode("{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}", &m)
Expect(err).NotTo(HaveOccurred())
})
It("decode into slice", func() {
var a []int
err := php.JSONDecode("[1,2,3]", &a)
Expect(err).NotTo(HaveOccurred())
})
It("decode into string", func() {
var s string
err := php.JSONDecode("\"123\"", &s)
Expect(err).NotTo(HaveOccurred())
})
It("invalid json", func() {
var s string
err := php.JSONDecode("123", &s)
Expect(err).To(HaveOccurred())
})
})
})