• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/14_array_literals

  1 The ucode script language supports declaring arrays using JSON notation.
  2 
  3 -- Expect stdout --
  4 [ ]
  5 [ "first", "second", 123, [ "a", "nested", "array" ], { "a": "nested object" } ]
  6 -- End --
  7 
  8 -- Testcase --
  9 {%
 10         // An empty array can be declared using a pair of square brackets
 11         empty_array = [ ];
 12 
 13         // JSON notation is used to declare an array with contents
 14         json_array = [
 15                 "first",
 16                 "second",
 17                 123,
 18                 [ "a", "nested", "array" ],
 19                 { a: "nested object" }
 20         ];
 21 
 22         // Printing (or stringifying) arrays will return their JSON representation
 23         print(empty_array, "\n");
 24         print(json_array, "\n");
 25 -- End --
 26 
 27 
 28 Additionally, ucode implements ES6-like spread operators to allow shallow copying
 29 of array values into other arrays.
 30 
 31 -- Expect stdout --
 32 [ 1, 2, 3 ]
 33 [ 1, 2, 3, 4, 5, 6 ]
 34 [ 1, 2, 3, 4, 5, 6, false, true ]
 35 [ 1, 2, 3, false, true, 4, 5, 6 ]
 36 [ 1, 2, 3, [ 4, 5, 6 ] ]
 37 -- End --
 38 
 39 -- Testcase --
 40 {%
 41         a1 = [ 1, 2, 3 ];
 42         a2 = [ 4, 5, 6 ];
 43 
 44         print(join("\n", [
 45                 // copying one array into another
 46                 [ ...a1 ],
 47 
 48                 // combining two arrays
 49                 [ ...a1, ...a2 ],
 50 
 51                 // copying array and append values
 52                 [ ...a1, ...a2, false, true ],
 53 
 54                 // copy array and interleave values
 55                 [ ...a1, false, true, ...a2 ],
 56 
 57                 // nested spread operators
 58                 [ ...a1, [ ...a2 ] ]
 59         ]), "\n");
 60 %}
 61 -- End --
 62 
 63 Contrary to merging arrays into objects, objects cannot be merged into arrays.
 64 
 65 -- Expect stderr --
 66 Type error: ({ "foo": true, "bar": false }) is not iterable
 67 In line 5, byte 21:
 68 
 69  `    print([ ...arr, ...obj ], "\n");`
 70   Near here -------------^
 71 
 72 
 73 -- End --
 74 
 75 -- Testcase --
 76 {%
 77         arr = [ 1, 2, 3 ];
 78         obj = { foo: true, bar: false };
 79 
 80         print([ ...arr, ...obj ], "\n");
 81 %}
 82 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt