summaryrefslogtreecommitdiffhomepage
path: root/tests/00_syntax/13_object_literals
blob: 4b4f9341adf0a726080aa4de14ae0be8069db40e (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
The utpl script language supports declaring objects (dictionaries) using
either JSON or JavaScript notation.

-- Expect stdout --
{ }
{ "name": "Bob", "age": 31, "email": { "work": "bob@example.com", "private": "bob@example.org" } }
{ "banana": "yellow", "tomato": "red", "broccoli": "green" }
{ "foo": "bar", "complex key": "qrx" }
{ "foo": { "bar": true } }
-- End --

-- Testcase --
{%
	// An empty object can be declared using a pair of curly brackets
	empty_obj = { };

	// It is also possible to use JSON notation to declare an object
	json_obj = {
		"name": "Bob",
		"age": 31,
		"email": {
			"work": "bob@example.com",
			"private": "bob@example.org"
		}
	};

	// Declaring an object in JavaScript notation is supported as well
	another_obj = {
		banana: "yellow",
		tomato: "red",
		broccoli: "green"
	};

	// Mixing styles is allowed too
	third_obj = {
		foo: "bar",
		"complex key": "qrx"
	};

	// Important caveat: when nesting objects, ensure that curly brackets
	// are separated by space or newline to avoid interpretation as
	// expression block tag!
	nested_obj = { foo: { bar: true } }; // <-- mind the space in "} }"

	// Printing (or stringifying) objects will return their JSON representation
	print(empty_obj, "\n");
	print(json_obj, "\n");
	print(another_obj, "\n");
	print(third_obj, "\n");
	print(nested_obj, "\n");
-- End --