diff --git t/compilers/json/from_parrot.t t/compilers/json/from_parrot.t
index 170176c..f328141 100644
|
|
|
1 | | #!perl |
| 1 | #!parrot |
2 | 2 | # Copyright (C) 2001-2006, Parrot Foundation. |
3 | 3 | # $Id$ |
4 | 4 | |
5 | | use strict; |
6 | | use warnings; |
7 | | use lib qw( t . lib ../lib ../../lib ); |
8 | | |
9 | | use Test::More; |
10 | | use Parrot::Test tests => 18; |
11 | | |
12 | 5 | =head1 NAME |
13 | 6 | |
14 | 7 | t/compilers/json/from_parrot.t - test parrot to JSON conversion. |
… |
… |
|
23 | 16 | |
24 | 17 | =cut |
25 | 18 | |
26 | | # no. 1 |
27 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of an empty string' ); |
| 19 | .sub main :main |
| 20 | .include 'test_more.pir' |
| 21 | plan(39) |
| 22 | |
| 23 | test_create_json_of_an_empty_string() |
| 24 | test_create_json_of_a_non_empty_string() |
| 25 | test_create_json_of_a_string_with_simple_escapes() |
| 26 | test_create_json_of_some_integers() |
| 27 | test_create_json_of_some_numbers() |
| 28 | test_create_json_of_various_scalars_with_pretty_option() |
| 29 | test_create_json_of_an_array() |
| 30 | test_create_pretty_json_of_an_array() |
| 31 | test_create_json_of_array_keep_element_ordering() |
| 32 | test_create_json_of_a_mixed_array() |
| 33 | test_create_json_of_hash() |
| 34 | test_create_non_pretty_json_of_hash() |
| 35 | test_create_json_of_nested_structure_including_resizablepmcarray_and_empties() |
| 36 | test_create_non_pretty_json_of_nested_structure() |
| 37 | test_create_json_of_string_pmcs() |
| 38 | test_create_json_of_integer_pmcs() |
| 39 | test_create_json_of_boolean_pmcs() |
| 40 | test_create_json_of_null_and_undef() |
| 41 | .end |
28 | 42 | |
29 | | .sub test :main |
| 43 | # no. 1 |
| 44 | .sub test_create_json_of_an_empty_string |
30 | 45 | .local string s |
31 | 46 | s = '' |
32 | 47 | |
33 | 48 | load_bytecode 'JSON.pbc' |
34 | 49 | $S0 = _json( s, 0 ) |
35 | | say $S0 |
| 50 | is($S0, '""', 'Create JSON of an empty string') |
36 | 51 | .end |
37 | | CODE |
38 | | "" |
39 | | OUT |
40 | 52 | |
41 | 53 | # no. 2 |
42 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a non-empty string' ); |
| 54 | .sub test_create_json_of_a_non_empty_string |
43 | 55 | |
44 | | .sub test :main |
45 | 56 | .local string s |
46 | 57 | s = 'abcdeABCDE01234$%^&*' |
47 | 58 | |
48 | 59 | load_bytecode 'JSON.pbc' |
49 | 60 | $S0 = _json( s, 0 ) |
50 | | say $S0 |
| 61 | is($S0, '"abcdeABCDE01234$%^&*"', 'Create JSON of a non-empty string') |
51 | 62 | .end |
52 | | CODE |
53 | | "abcdeABCDE01234$%^&*" |
54 | | OUT |
55 | 63 | |
56 | 64 | # no. 3 |
57 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a string with simple escapes' ); |
58 | | |
59 | | .sub test :main |
| 65 | .sub test_create_json_of_a_string_with_simple_escapes |
60 | 66 | .local string s |
61 | 67 | s = "abcde\\ABCDE\"01234\n$%^&*" |
62 | 68 | # XXX more escapes need to be tested; see http://www.json.org/ |
63 | 69 | load_bytecode 'JSON.pbc' |
64 | 70 | $S0 = _json( s, 0 ) |
65 | | say $S0 |
| 71 | is($S0, '"abcde\\ABCDE\"01234\n$%^&*"', 'Create JSON of a string with simple escapes') |
66 | 72 | .end |
67 | | CODE |
68 | | "abcde\\ABCDE\"01234\n$%^&*" |
69 | | OUT |
70 | 73 | |
71 | 74 | # no. 4 |
72 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of some integers' ); |
| 75 | .sub test_create_json_of_some_integers |
73 | 76 | |
74 | | .sub test :main |
75 | 77 | .local int i |
76 | 78 | i = 0 |
77 | 79 | load_bytecode 'JSON.pbc' |
78 | 80 | $S0 = _json( i, 0 ) |
79 | | say $S0 |
| 81 | is($S0, 0, 'Create JSON of some integers') |
80 | 82 | i = 35 |
81 | 83 | $S0 = _json( i, 0 ) |
82 | | say $S0 |
| 84 | is($S0, 35, 'Create JSON of some integers') |
83 | 85 | i = -42 |
84 | 86 | $S0 = _json( i, 0 ) |
85 | | say $S0 |
| 87 | is($S0, -42, 'Create JSON of some integers') |
86 | 88 | i = 2147483647 |
87 | 89 | $S0 = _json( i, 0 ) |
88 | | say $S0 |
| 90 | is($S0, 2147483647, 'Create JSON of some integers') |
89 | 91 | i = -2147483648 |
90 | 92 | $S0 = _json( i, 0 ) |
91 | | say $S0 |
| 93 | is($S0, -2147483648, 'Create JSON of some integers') |
92 | 94 | .end |
93 | | CODE |
94 | | 0 |
95 | | 35 |
96 | | -42 |
97 | | 2147483647 |
98 | | -2147483648 |
99 | | OUT |
100 | 95 | |
101 | 96 | # no. 5 |
102 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of some numbers' ); |
103 | | |
104 | | .sub test :main |
| 97 | .sub test_create_json_of_some_numbers |
105 | 98 | .local num n |
106 | 99 | n = 0.0 |
107 | 100 | load_bytecode 'JSON.pbc' |
108 | 101 | $S0 = _json( n ) |
109 | | say $S0 |
| 102 | is($S0, 0, 'Create JSON of some numbers') |
110 | 103 | n = 2.50 |
111 | 104 | $S0 = _json( n ) |
112 | | say $S0 |
| 105 | is($S0, 2.5, 'Create JSON of some numbers') |
113 | 106 | n = -42.0 |
114 | 107 | $S0 = _json( n ) |
115 | | say $S0 |
| 108 | is($S0, -42, 'Create JSON of some numbers') |
116 | 109 | n = 4.5e1 |
117 | 110 | $S0 = _json( n ) |
118 | | say $S0 |
| 111 | is($S0, 45, 'Create JSON of some numbers') |
119 | 112 | .end |
120 | | CODE |
121 | | 0 |
122 | | 2.5 |
123 | | -42 |
124 | | 45 |
125 | | OUT |
126 | 113 | |
127 | 114 | # no. 6 |
128 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of various scalars with pretty option' ); |
129 | | |
130 | | .sub test :main |
| 115 | .sub test_create_json_of_various_scalars_with_pretty_option |
131 | 116 | .local string s |
132 | 117 | s = "abcde\\ABCDE\"01234\n$%^&*" |
133 | 118 | load_bytecode 'JSON.pbc' |
134 | 119 | $S0 = _json( s, 1 ) |
135 | | print $S0 |
| 120 | is($S0, "\"abcde\\\\ABCDE\\\"01234\\n\$\%\^\&\*\"\n", 'Create JSON of various scalars with pretty option') |
136 | 121 | |
137 | 122 | .local int i |
138 | 123 | i = -42 |
139 | 124 | $S0 = _json( i, 1 ) |
140 | | print $S0 |
| 125 | is($S0, "-42\n", 'Create JSON of various scalars with pretty option') |
141 | 126 | |
142 | 127 | .local num n |
143 | 128 | n = 2.50 |
144 | 129 | $S0 = _json( n, 1 ) |
145 | | print $S0 |
| 130 | is($S0, "2.5\n", 'Create JSON of various scalars with pretty option') |
146 | 131 | .end |
147 | | CODE |
148 | | "abcde\\ABCDE\"01234\n$%^&*" |
149 | | -42 |
150 | | 2.5 |
151 | | OUT |
152 | 132 | |
153 | 133 | # no. 7 |
154 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of an array' ); |
155 | | |
156 | | .sub test :main |
| 134 | .sub test_create_json_of_an_array |
157 | 135 | .local pmc array |
158 | 136 | |
159 | 137 | new array, 'Array' |
… |
… |
|
171 | 149 | |
172 | 150 | load_bytecode 'JSON.pbc' |
173 | 151 | $S0 = _json( array, 0 ) |
174 | | say $S0 |
| 152 | is($S0, '[0,1,2,3,4,5,6,7,8,9]', 'Create JSON of an array') |
175 | 153 | .end |
176 | | CODE |
177 | | [0,1,2,3,4,5,6,7,8,9] |
178 | | OUT |
179 | 154 | |
180 | 155 | # no. 8 |
181 | | pir_output_is( <<'CODE', <<'OUT', 'Create pretty JSON of an array' ); |
182 | | |
183 | | .sub test :main |
| 156 | .sub test_create_pretty_json_of_an_array |
184 | 157 | .local pmc array |
185 | 158 | |
186 | 159 | new array, 'Array' |
… |
… |
|
198 | 171 | |
199 | 172 | load_bytecode 'JSON.pbc' |
200 | 173 | $S0 = _json( array, 1 ) |
201 | | print $S0 |
202 | | .end |
203 | | CODE |
| 174 | is($S0, <<'OUTPUTPUT', 'Create pretty JSON of an array') |
204 | 175 | [ |
205 | 176 | 0, |
206 | 177 | 1, |
… |
… |
|
213 | 184 | 8, |
214 | 185 | 9 |
215 | 186 | ] |
216 | | OUT |
| 187 | OUTPUTPUT |
| 188 | .end |
217 | 189 | |
218 | 190 | # no. 9 |
219 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of array, keep element ordering' ); |
220 | | |
221 | | .sub test :main |
| 191 | .sub test_create_json_of_array_keep_element_ordering |
222 | 192 | .local pmc array |
223 | 193 | |
224 | 194 | new array, 'Array' |
… |
… |
|
232 | 202 | |
233 | 203 | load_bytecode 'JSON.pbc' |
234 | 204 | $S0 = _json( array, 1 ) |
235 | | print $S0 |
236 | | .end |
237 | | CODE |
| 205 | is($S0, <<'OUTPUTPUT', 'Create JSON of an array, keep element ordering') |
238 | 206 | [ |
239 | 207 | 35, |
240 | 208 | 1, |
… |
… |
|
243 | 211 | -2147483648, |
244 | 212 | 2147483647 |
245 | 213 | ] |
246 | | OUT |
| 214 | OUTPUTPUT |
| 215 | .end |
247 | 216 | |
248 | 217 | # no. 10 |
249 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of a mixed array' ); |
250 | | |
251 | | .sub test :main |
| 218 | .sub test_create_json_of_a_mixed_array |
252 | 219 | .local pmc array |
253 | 220 | |
254 | 221 | new array, 'Array' |
… |
… |
|
262 | 229 | |
263 | 230 | load_bytecode 'JSON.pbc' |
264 | 231 | $S0 = _json( array, 1 ) |
265 | | print $S0 |
266 | | .end |
267 | | CODE |
| 232 | is($S0, <<'OUTPUT', 'Create JSON of a mixed array') |
268 | 233 | [ |
269 | 234 | 0, |
270 | 235 | 15, |
… |
… |
|
273 | 238 | "json", |
274 | 239 | 0 |
275 | 240 | ] |
276 | | OUT |
| 241 | OUTPUT |
| 242 | .end |
277 | 243 | |
278 | 244 | # no. 11 |
279 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of hash' ); |
280 | | |
281 | | .sub test :main |
| 245 | .sub test_create_json_of_hash |
282 | 246 | .local pmc hash |
283 | 247 | |
284 | 248 | new hash, 'Hash' |
… |
… |
|
289 | 253 | |
290 | 254 | load_bytecode 'JSON.pbc' |
291 | 255 | $S0 = _json( hash, 1 ) |
292 | | print $S0 |
293 | | .end |
294 | | CODE |
| 256 | is($S0, <<'OUTPUT', 'Create JSON of hash') |
295 | 257 | { |
296 | 258 | "alpha" : 29, |
297 | 259 | "beta" : "B", |
298 | 260 | "delta" : "DELTA", |
299 | 261 | "gamma" : 3.1 |
300 | 262 | } |
301 | | OUT |
| 263 | OUTPUT |
| 264 | .end |
302 | 265 | |
303 | 266 | # no. 12 |
304 | | pir_output_is( <<'CODE', <<'OUT', 'Create non-pretty JSON of hash' ); |
305 | | |
306 | | .sub test :main |
| 267 | .sub test_create_non_pretty_json_of_hash |
307 | 268 | .local pmc hash |
308 | 269 | |
309 | 270 | new hash, 'Hash' |
… |
… |
|
314 | 275 | |
315 | 276 | load_bytecode 'JSON.pbc' |
316 | 277 | $S0 = _json( hash, 0 ) |
317 | | say $S0 |
| 278 | is($S0, '{"alpha":29,"beta":"B","delta":"DELTA","gamma":3.1}', 'Create non-pretty JSON of hash') |
318 | 279 | .end |
319 | | CODE |
320 | | {"alpha":29,"beta":"B","delta":"DELTA","gamma":3.1} |
321 | | OUT |
322 | 280 | |
323 | 281 | # no. 13 |
324 | | pir_output_is( |
325 | | <<'CODE', <<'OUT', 'Create JSON of nested structure including ResizablePMCArray and empties' ); |
326 | | |
327 | | .sub test :main |
| 282 | .sub test_create_json_of_nested_structure_including_resizablepmcarray_and_empties |
328 | 283 | .local pmc street1, street2, city1, city2, country, world |
329 | 284 | |
330 | 285 | street1 = new 'Hash' |
… |
… |
|
351 | 306 | |
352 | 307 | load_bytecode 'JSON.pbc' |
353 | 308 | $S0 = _json( world, 1 ) |
354 | | print $S0 |
355 | | .end |
356 | | CODE |
| 309 | is($S0, <<'OUTPUT', 'Create JSON of nested structure including ResizablePMCArray and empties') |
357 | 310 | { |
358 | 311 | "population" : 1234567890, |
359 | 312 | "some_country" : [ |
… |
… |
|
370 | 323 | ] |
371 | 324 | ] |
372 | 325 | } |
373 | | OUT |
| 326 | OUTPUT |
| 327 | .end |
374 | 328 | |
375 | 329 | # no. 14 |
376 | | pir_output_is( <<'CODE', <<'OUT', 'Create non-pretty JSON of nested structure' ); |
377 | | |
378 | | .sub test :main |
| 330 | .sub test_create_non_pretty_json_of_nested_structure |
379 | 331 | .local pmc street1, street2, city1, city2, country, world |
380 | 332 | |
381 | 333 | street1 = new 'Hash' |
… |
… |
|
402 | 354 | |
403 | 355 | load_bytecode 'JSON.pbc' |
404 | 356 | $S0 = _json( world, 0 ) |
405 | | say $S0 |
| 357 | is($S0, '{"population":1234567890,"some_country":[[{"Perl":"Highway","Python":"Grove","Ruby":"Lane"},{}],[]]}', 'Create non-pretty JSON of nested structure') |
406 | 358 | .end |
407 | | CODE |
408 | | {"population":1234567890,"some_country":[[{"Perl":"Highway","Python":"Grove","Ruby":"Lane"},{}],[]]} |
409 | | OUT |
410 | 359 | |
411 | 360 | # no. 15 |
412 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of String PMCs' ); |
413 | | |
414 | | .sub test :main |
| 361 | .sub test_create_json_of_string_pmcs |
415 | 362 | .local pmc s |
416 | 363 | |
417 | 364 | s = new 'String' |
418 | 365 | s = '' |
419 | 366 | load_bytecode 'JSON.pbc' |
420 | 367 | $S0 = _json( s, 0 ) |
421 | | say $S0 |
| 368 | is($S0, '""', 'Create JSON of String PMCs') |
422 | 369 | $S0 = _json( s, 1 ) |
423 | | print $S0 |
| 370 | is($S0, "\"\"\n", 'Create JSON of String PMCs') |
424 | 371 | |
425 | 372 | s = new 'String' |
426 | 373 | s = "12345\"67890" |
427 | 374 | $S0 = _json( s, 0 ) |
428 | | say $S0 |
| 375 | is($S0, '"12345\"67890"', 'Create JSON of String PMCs') |
429 | 376 | $S0 = _json( s, 1 ) |
430 | | print $S0 |
| 377 | is($S0, "\"12345\\\"67890\"\n", 'Create JSON of String PMCs') |
431 | 378 | .end |
432 | | CODE |
433 | | "" |
434 | | "" |
435 | | "12345\"67890" |
436 | | "12345\"67890" |
437 | | OUT |
438 | 379 | |
439 | 380 | # no. 16 |
440 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of Integer PMCs' ); |
441 | | |
442 | | .sub test :main |
| 381 | .sub test_create_json_of_integer_pmcs |
443 | 382 | .local pmc i |
444 | 383 | |
445 | 384 | i = new 'Integer' |
446 | 385 | i = 0 |
447 | 386 | load_bytecode 'JSON.pbc' |
448 | 387 | $S0 = _json( i, 0 ) |
449 | | say $S0 |
| 388 | is($S0, 0, 'Create JSON of String PMCs') |
450 | 389 | $S0 = _json( i, 1 ) |
451 | | print $S0 |
| 390 | is($S0, "0\n", 'Create JSON of String PMCs') |
452 | 391 | |
453 | 392 | i = new 'Integer' |
454 | 393 | i = -42 |
455 | 394 | $S0 = _json( i, 0 ) |
456 | | say $S0 |
| 395 | is($S0, -42, 'Create JSON of String PMCs') |
457 | 396 | $S0 = _json( i, 1 ) |
458 | | print $S0 |
| 397 | is($S0, "-42\n", 'Create JSON of String PMCs') |
459 | 398 | .end |
460 | | CODE |
461 | | 0 |
462 | | 0 |
463 | | -42 |
464 | | -42 |
465 | | OUT |
466 | 399 | |
467 | 400 | # no. 17 |
468 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of Boolean PMCs' ); |
469 | | |
470 | | .sub test :main |
| 401 | .sub test_create_json_of_boolean_pmcs |
471 | 402 | .local pmc b |
472 | 403 | |
473 | 404 | b = new 'Boolean' |
474 | 405 | b = 0 |
475 | 406 | load_bytecode 'JSON.pbc' |
476 | 407 | $S0 = _json( b, 0 ) |
477 | | say $S0 |
| 408 | is($S0, 'false', 'Create JSON of Boolean PMCs') |
478 | 409 | $S0 = _json( b, 1 ) |
479 | | print $S0 |
| 410 | is($S0, "false\n", 'Create JSON of Boolean PMCs') |
480 | 411 | |
481 | 412 | b = new 'Boolean' |
482 | 413 | b = 1 |
483 | 414 | $S0 = _json( b, 0 ) |
484 | | say $S0 |
| 415 | is($S0, 'true', 'Create JSON of Boolean PMCs') |
485 | 416 | $S0 = _json( b, 1 ) |
486 | | print $S0 |
| 417 | is($S0, "true\n", 'Create JSON of Boolean PMCs') |
487 | 418 | .end |
488 | | CODE |
489 | | false |
490 | | false |
491 | | true |
492 | | true |
493 | | OUT |
494 | 419 | |
495 | 420 | # no. 18 |
496 | | pir_output_is( <<'CODE', <<'OUT', 'Create JSON of null and .Undef' ); |
497 | | |
498 | | .sub test :main |
| 421 | .sub test_create_json_of_null_and_undef |
499 | 422 | .local pmc n |
500 | 423 | null n |
501 | 424 | |
502 | 425 | load_bytecode 'JSON.pbc' |
503 | 426 | $S0 = _json( n, 0 ) |
504 | | say $S0 |
| 427 | is($S0, 'null', 'Create JSON of null and .Undef') |
505 | 428 | $S0 = _json( n, 1 ) |
506 | | print $S0 |
| 429 | is($S0, "null\n", 'Create JSON of null and .Undef') |
507 | 430 | |
508 | 431 | n = new 'Undef' |
509 | 432 | $S0 = _json( n, 0 ) |
510 | | say $S0 |
| 433 | is($S0, 'null', 'Create JSON of null and .Undef') |
511 | 434 | $S0 = _json( n, 1 ) |
512 | | print $S0 |
| 435 | is($S0, "null\n", 'Create JSON of null and .Undef') |
513 | 436 | .end |
514 | | CODE |
515 | | null |
516 | | null |
517 | | null |
518 | | null |
519 | | OUT |
520 | 437 | |
521 | 438 | # Local Variables: |
522 | 439 | # mode: cperl |
523 | 440 | # cperl-indent-level: 4 |
524 | 441 | # fill-column: 100 |
525 | 442 | # End: |
526 | | # vim: expandtab shiftwidth=4: |
| 443 | # vim: expandtab shiftwidth=4 filetype=pir: |