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

Sources/ucode/tests/custom/00_syntax/13_object_literals

  1 The ucode script language supports declaring objects (dictionaries) using
  2 either JSON or JavaScript notation.
  3 
  4 -- Expect stdout --
  5 { }
  6 { "name": "Bob", "age": 31, "email": { "work": "bob@example.com", "private": "bob@example.org" } }
  7 { "banana": "yellow", "tomato": "red", "broccoli": "green" }
  8 { "foo": "bar", "complex key": "qrx" }
  9 { "foo": { "bar": true } }
 10 -- End --
 11 
 12 -- Testcase --
 13 {%
 14         // An empty object can be declared using a pair of curly brackets
 15         empty_obj = { };
 16 
 17         // It is also possible to use JSON notation to declare an object
 18         json_obj = {
 19                 "name": "Bob",
 20                 "age": 31,
 21                 "email": {
 22                         "work": "bob@example.com",
 23                         "private": "bob@example.org"
 24                 }
 25         };
 26 
 27         // Declaring an object in JavaScript notation is supported as well
 28         another_obj = {
 29                 banana: "yellow",
 30                 tomato: "red",
 31                 broccoli: "green"
 32         };
 33 
 34         // Mixing styles is allowed too
 35         third_obj = {
 36                 foo: "bar",
 37                 "complex key": "qrx"
 38         };
 39 
 40         // Important caveat: when nesting objects, ensure that curly brackets
 41         // are separated by space or newline to avoid interpretation as
 42         // expression block tag!
 43         nested_obj = { foo: { bar: true } }; // <-- mind the space in "} }"
 44 
 45         // Printing (or stringifying) objects will return their JSON representation
 46         print(empty_obj, "\n");
 47         print(json_obj, "\n");
 48         print(another_obj, "\n");
 49         print(third_obj, "\n");
 50         print(nested_obj, "\n");
 51 %}
 52 -- End --
 53 
 54 
 55 Additionally, ucode implements ES6-like spread operators to allow shallow copying
 56 of object properties into other objects.
 57 
 58 -- Expect stdout --
 59 { "foo": true, "bar": false }
 60 { "foo": true, "bar": false, "baz": 123, "qrx": 456 }
 61 { "foo": false, "bar": true, "baz": 123, "qrx": 456 }
 62 { "foo": true, "bar": false }
 63 { "foo": true, "bar": false, "level2": { "baz": 123, "qrx": 456 } }
 64 { "foo": true, "bar": false, "0": 7, "1": 8, "2": 9 }
 65 -- End --
 66 
 67 -- Testcase --
 68 {%
 69         o1 = { foo: true, bar: false };
 70         o2 = { baz: 123, qrx: 456 };
 71         arr = [7, 8, 9];
 72 
 73         print(join("\n", [
 74                 // copying one object into another
 75                 { ...o1 },
 76 
 77                 // combining two objects
 78                 { ...o1, ...o2 },
 79 
 80                 // copying object and override properties
 81                 { ...o1, ...o2, foo: false, bar: true },
 82 
 83                 // default properties overwritten by spread operator
 84                 { foo: 123, bar: 456, ...o1 },
 85 
 86                 // nested spread operators
 87                 { ...o1, level2: { ...o2 } },
 88 
 89                 // merging array into objects
 90                 { ...o1, ...arr }
 91         ]), "\n");
 92 %}
 93 -- End --
 94 
 95 
 96 ES2015 short hand property notation is supported as well.
 97 
 98 -- Expect stdout --
 99 { "a": 123, "b": true, "c": "test" }
100 -- End --
101 
102 -- Testcase --
103 {%
104         a = 123;
105         b = true;
106         c = "test";
107 
108         o = { a, b, c };
109 
110         print(o, "\n");
111 %}
112 -- End --
113 
114 -- Expect stderr --
115 Syntax error: Unexpected token
116 Expecting ':'
117 In line 2, byte 14:
118 
119  `    o = { "foo" };`
120   Near here ------^
121 
122 
123 -- End --
124 
125 -- Testcase --
126 {%
127         o = { "foo" };
128 %}
129 -- End --
130 
131 -- Expect stderr --
132 Syntax error: Invalid identifier
133 In line 2, byte 8:
134 
135  `    o = { function };`
136             ^-- Near here
137 
138 
139 -- End --
140 
141 -- Testcase --
142 {%
143         o = { function };
144 %}
145 -- End --
146 
147 
148 ES2015 computed property names are supported.
149 
150 -- Expect stdout --
151 { "test": true, "hello": false, "ABC": 123 }
152 -- End --
153 
154 -- Testcase --
155 {%
156         s = "test";
157         o = {
158                 [s]: true,
159                 ["he" + "llo"]: false,
160                 [uc("abc")]: 123
161         };
162 
163         print(o, "\n");
164 %}
165 -- End --
166 
167 -- Expect stderr --
168 Syntax error: Expecting expression
169 In line 2, byte 10:
170 
171  `    o1 = { []: true };`
172   Near here --^
173 
174 
175 Syntax error: Unexpected token
176 Expecting ']'
177 In line 3, byte 14:
178 
179  `    o2 = { [true, false]: 123 };`
180   Near here ------^
181 
182 
183 -- End --
184 
185 -- Testcase --
186 {%
187         o1 = { []: true };
188         o2 = { [true, false]: 123 };
189 %}
190 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt