aboutsummaryrefslogtreecommitdiff
path: root/tests/function/json/testscript
blob: 9f8c2e9a98561e7f743d69d5d8ce2feea59668ca (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# file      : tests/function/json/testscript
# license   : MIT; see accompanying LICENSE file

# See also tests in type/json/.

.include ../../common.testscript

: type
:
$* <<EOI >>EOO
print $value_type([json] )
print $value_type([json] null)
print $value_type([json] true)
print $value_type([json] 123)
print $value_type([json] -123)
print $value_type([json] 123, true)
print $value_type([json] -123, true)
print $value_type([json] 1 2 3)
print $value_type([json] one@1 two@2 three@3)

j = [json] one@1 two@2 three@3
i = [uint64] 1
m = ($j[$i])
print $value_type($j[$i])
print $value_type($m)
EOI
null
null
boolean
number
number
unsigned number
signed number
array
object
object
object
EOO

: member
:
$* <<EOI >>EOO
j = [json] one@1 two@2 three@3
i = [uint64] 1
m = ($j[$i])
print $member_name($j[$i]) $member_value($j[$i])
print $member_name($m) $member_value($m)
for m: $j
  print $member_name($m) $member_value($m)
EOI
two 2
two 2
one 1
two 2
three 3
EOO

: size
:
$* <<EOI >>EOO
print $value_size([json] null)
print $value_size([json] true)
print $value_size([json] 123)
print $value_size([json] abc)
print $size([string] ([json] abc)) # @@ Should be 3 (quoted, type hint).
print $value_size([json] 1 2 3)
print $value_size([json] one@1 two@2 three@3)
EOI
0
1
1
1
5
3
3
EOO

: find
:
$* <<EOI >>EOO
j = [json] 1 ([json] one@1 two@2) 2 true 3 null 4 abc -5 null ([json] 1 2 3)
print $array_find_index($j, null)
print $array_find_index($j, true)
print $array_find_index($j, 3)
print $array_find_index($j, 0x4)
print $array_find_index($j, -5)
print $array_find_index($j, abc)
print $array_find_index($j, [json] 1 2 3)
print $array_find_index($j, [json] two@2 one@1)
print $array_find_index($j, [json] 1 2)
print $array_find_index($j, [json] one@1)
print $array_find_index($j, [json] one@1 two@2 three@3)
print $array_find_index($j, [json] one@1 TWO@3)
print $array_find_index($j, [json] one@1 two@3)
EOI
5
3
4
6
8
7
10
1
11
11
11
11
11
EOO

: parse
:
{
  : basics
  :
  $* <<EOI >>EOO
  print $json.parse('[123, "abc", {"one":1, "two":2}]')
  EOI
  [123,"abc",{"one":1,"two":2}]
  EOO

  : diagnostics-invalid-input
  :
  $* <<EOI 2>>EOE != 0
  print $json.parse('{"one":, "two":2}]')
  EOI
  error: invalid json input: unexpected byte ',' in value
    info: line 1, column 8, byte offset 8
    <stdin>:1:8: info: while calling json.parse(<untyped>)
  EOE

  : diagnostics-duplicate-input
  :
  $* <<EOI 2>>EOE != 0
  print $json.parse('{"one":1, "one":2}]')
  EOI
  error: invalid json input: duplicate object member 'one'
    info: line 1, column 11, byte offset 15
    <stdin>:1:8: info: while calling json.parse(<untyped>)
  EOE
}

: serialize
:
{
  : basics
  :
  $* <<EOI >>EOO
  j = [json] 123 abc ([json] one@1 two@2)
  print $json.serialize($j)
  print $json.serialize($j, 0)
  EOI
  [
    123,
    "abc",
    {
      "one": 1,
      "two": 2
    }
  ]
  [123,"abc",{"one":1,"two":2}]
  EOO

  : diagnostics
  :
  if false
  {
    # This is not easy to trigger normally so we have a normally-disabled
    # special hack in the $json.serialize() implementation to trigger this.
    #
    $* <<EOI 2>>EOE != 0
    print $json.serialize([json] deadbeef)
    EOI
    error: invalid json value: invalid UTF-8 text
      info: while serializing string value
      info: offending byte offset 4
      <stdin>:1:8: info: while calling json.serialize(json)
    EOE
  }

}

: load
:
{
  : basics
  :
  cat <<EOI >=input.json;
  {
    "str":"abc",
    "num":123,
    "arr":[1, 2, 3],
    "obj":{"one":1, "two":2, "three":3}
  }
  EOI
  $* <<EOI >>EOO
  j = $json.load(input.json)
  for m: $j
    print $member_name($m) $member_value($m)
  EOI
  str abc
  num 123
  arr [1,2,3]
  obj {"one":1,"two":2,"three":3}
  EOO

  : diagnostics
  :
  cat <<EOI >=input.json;
  {
    "str":"abc",
    "num":,
    "arr":[1, 2, 3],
    "obj":{"one":1, "two":2, "three":3}
  }
  EOI
  $* <<EOI 2>>EOE != 0
  j = $json.load(input.json)
  EOI
  input.json:3:9: error: invalid json input: unexpected byte ',' in value
    info: byte offset 26
    <stdin>:1:6: info: while calling json.load(<untyped>)
  EOE
}