-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_parse_json_test.go
118 lines (104 loc) · 2.45 KB
/
func_parse_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package testing
import "testing"
func TestParseJson_Int(t *testing.T) {
rsl := `
a = parse_json("2")
print(a + 1)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "3\n")
assertNoErrors(t)
}
func TestParseJson_Float(t *testing.T) {
rsl := `
a = parse_json("2.1")
print(a + 1)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "3.1\n")
assertNoErrors(t)
}
func TestParseJson_Bool(t *testing.T) {
rsl := `
a = parse_json("true")
print(a or false)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "true\n")
assertNoErrors(t)
}
func TestParseJson_String(t *testing.T) {
rsl := `
a = parse_json('"alice"')
print(a + "e")
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "alicee\n")
assertNoErrors(t)
}
func TestParseJson_Map(t *testing.T) {
rsl := `
a = parse_json(r'{"name": "alice", "age": 20, "height": 5.5, "is_student": true, "cars": ["audi", "bmw"], "friends": {"bob": 1, "charlie": 2}}')
print(a["name"] + "e")
print(a["age"] + 1)
print(a["height"] + 1.1)
print(a["is_student"] or false)
print(a["cars"][0] + "e")
print(a["friends"]["bob"] + 1)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `alicee
21
6.6
true
audie
2
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func TestParseJson_ErrorsOnInvalidJson(t *testing.T) {
rsl := `
parse_json(r'{asd asd}')
`
setupAndRunCode(t, rsl, "--color=never")
expected := `Error at L2:1
parse_json(r'{asd asd}')
^^^^^^^^^^^^^^^^^^^^^^^^
Error parsing JSON: invalid character 'a' looking for beginning of object key string
`
assertError(t, 1, expected)
}
func TestParseJson_ErrorsOnInvalidType(t *testing.T) {
rsl := `
parse_json(10)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `Error at L2:12
parse_json(10)
^^ Got "int" as the 1st argument of parse_json(), but must be: string
`
assertError(t, 1, expected)
}
func TestParseJson_ErrorsOnNoArgs(t *testing.T) {
rsl := `
parse_json()
`
setupAndRunCode(t, rsl, "--color=never")
expected := `Error at L2:1
parse_json()
^^^^^^^^^^^^ parse_json() requires at least 1 argument, but got 0
`
assertError(t, 1, expected)
}
func TestParseJson_ErrorsOnTooManyArgs(t *testing.T) {
rsl := `
parse_json("1", "2")
`
setupAndRunCode(t, rsl, "--color=never")
expected := `Error at L2:1
parse_json("1", "2")
^^^^^^^^^^^^^^^^^^^^ parse_json() requires at most 1 argument, but got 2
`
assertError(t, 1, expected)
}