aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/tables_advanced_spec.yue
blob: c8cc7d5936cca20be401decd4ce561fe0ff64882 (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
describe "advanced tables", ->
	it "should create table with implicit keys", ->
		hair = "golden"
		height = 200
		person = { :hair, :height, shoe_size: 40 }
		assert.same person.hair, "golden"
		assert.same person.height, 200

	it "should work with computed keys", ->
		t = {
			[1 + 2]: "hello"
			["key_" .. "suffix"]: "value"
		}
		assert.same t[3], "hello"
		assert.same t["key_suffix"], "value"

	it "should support keyword keys", ->
		tbl = {
			do: "something"
			end: "hunger"
			function: "test"
		}
		assert.same tbl.do, "something"
		assert.same tbl.end, "hunger"
		assert.same tbl.function, "test"

	it "should handle array syntax with mixed content", ->
		tb = {
			1, 2, 3
			name: "superman"
			4, 5, 6
		}
		assert.same tb[1], 1
		assert.same tb.name, "superman"
		assert.same tb[4], 4

	it "should work with single line table literals", ->
		my_function dance: "Tango", partner: "none"
		assert.is_true true

	it "should support nested tables", ->
		tb =
			outer:
				inner:
					value: 42
		assert.same tb.outer.inner.value, 42

	it "should handle table without braces", ->
		profile =
			height: "4 feet"
			shoe_size: 13
			favorite_foods: ["ice cream", "donuts"]
		assert.same profile.height, "4 feet"
		assert.same profile.shoe_size, 13

	it "should work with colon syntax for keys", ->
		t = {
			name: "Bill"
			age: 200
			["favorite food"]: "rice"
		}
		assert.same t.name, "Bill"
		assert.same t["favorite food"], "rice"

	it "should support implicit object in table", ->
		tb =
			name: "abc"
			values:
				- "a"
				- "b"
				- "c"
		assert.same tb.values, {"a", "b", "c"}

	it "should handle array only table", ->
		some_values = [1, 2, 3, 4]
		assert.same some_values[1], 1
		assert.same some_values[4], 4

	it "should work with trailing comma", ->
		list_with_one = [1,]
		assert.same list_with_one[1], 1

	it "should support table spreading", ->
		a = {1, 2, 3, x: 1}
		b = {4, 5, y: 1}
		merge = {...a, ...b}
		assert.same merge[1], 1
		assert.same merge[4], 4
		assert.same merge.x, 1
		assert.same merge.y, 1

	it "should handle mixed spread", ->
		parts = {
			* "shoulders"
			* "knees"
		}
		lyrics =
			* "head"
			* ...parts
			* "and"
			* "toes"
		assert.same lyrics, {"head", "shoulders", "knees", "and", "toes"}

	it "should work with metatable creation", ->
		mt = {}
		add = (right) => <>: mt, value: @value + right.value
		mt.__add = add

		a = <>: mt, value: 1
		b = value: 2
		b.<>, mt
		c = a + b
		assert.same c.value, 3

	it "should support metatable accessing", ->
		tb = <"value">: 123
		tb.<index> = tb.<>
		assert.same tb.value, 123

	it "should handle metatable destructuring", ->
		tb = {
			item: "test"
			new: -> "created"
			close: -> "closed"
		}
		{:item, :new, :<close>} = tb
		assert.same item, "test"
		assert.same new!, "created"

	it "should work with string keys directly", ->
		t = {
			"hello world": true
			"test-key": "value"
		}
		assert.is_true t["hello world"]
		assert.same t["test-key"], "value"

	it "should support number keys", ->
		t = {
			[10]: "ten"
			[20]: "twenty"
		}
		assert.same t[10], "ten"
		assert.same t[20], "twenty"

	it "should handle empty tables", ->
		empty = {}
		assert.same #empty, 0

	it "should work with table literals in function calls", ->
		fn = (tb) -> tb.x + tb.y
		result = fn x: 10, y: 20
		assert.same result, 30