aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/implicit_object_spec.lua
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-28 18:43:14 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-28 18:43:14 +0800
commitdd64edd58fe25ec74ae5958128cf3f74b0692f3b (patch)
tree0f2de1df55897e2713977c5a53936757e14b477a /spec/outputs/test/implicit_object_spec.lua
parent7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (diff)
downloadyuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.gz
yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.bz2
yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.zip
Fixed compiler issues and added 800+ test cases.
Diffstat (limited to 'spec/outputs/test/implicit_object_spec.lua')
-rw-r--r--spec/outputs/test/implicit_object_spec.lua243
1 files changed, 243 insertions, 0 deletions
diff --git a/spec/outputs/test/implicit_object_spec.lua b/spec/outputs/test/implicit_object_spec.lua
new file mode 100644
index 0000000..bd23978
--- /dev/null
+++ b/spec/outputs/test/implicit_object_spec.lua
@@ -0,0 +1,243 @@
1return describe("implicit object", function()
2 it("should create list with asterisk", function()
3 local list = {
4 1,
5 2,
6 3
7 }
8 return assert.same(list, {
9 1,
10 2,
11 3
12 })
13 end)
14 it("should create list with dash", function()
15 local items = {
16 "a",
17 "b",
18 "c"
19 }
20 return assert.same(items, {
21 "a",
22 "b",
23 "c"
24 })
25 end)
26 it("should work with function call", function()
27 local results = { }
28 local fn = {
29 1,
30 2,
31 3
32 }
33 for _index_0 = 1, #fn do
34 local item = fn[_index_0]
35 table.insert(results, item)
36 end
37 return assert.same(results, {
38 1,
39 2,
40 3
41 })
42 end)
43 it("should support nested implicit objects", function()
44 local tb = {
45 name = "test",
46 values = {
47 "a",
48 "b",
49 "c"
50 },
51 objects = {
52 {
53 name = "first",
54 value = 1
55 },
56 {
57 name = "second",
58 value = 2
59 }
60 }
61 }
62 assert.same(tb.values, {
63 "a",
64 "b",
65 "c"
66 })
67 assert.same(tb.objects[1].name, "first")
68 return assert.same(tb.objects[2].value, 2)
69 end)
70 it("should work with return statement", function()
71 local fn
72 fn = function()
73 return {
74 1,
75 2,
76 3
77 }
78 end
79 return assert.same(fn(), {
80 1,
81 2,
82 3
83 })
84 end)
85 it("should handle mixed content", function()
86 local tb = {
87 key = "value",
88 items = {
89 1,
90 2
91 },
92 other = "data"
93 }
94 assert.same(tb.key, "value")
95 assert.same(tb.items, {
96 1,
97 2
98 })
99 return assert.same(tb.other, "data")
100 end)
101 it("should work in assignment", function()
102 local list = {
103 "x",
104 "y",
105 "z"
106 }
107 return assert.same(list, {
108 "x",
109 "y",
110 "z"
111 })
112 end)
113 it("should support nested structures with asterisk", function()
114 local tb = {
115 1,
116 2,
117 nested = {
118 3,
119 4
120 }
121 }
122 assert.same(tb[1], 1)
123 assert.same(tb[2], 2)
124 return assert.same(tb.nested, {
125 3,
126 4
127 })
128 end)
129 it("should handle implicit object in tables", function()
130 local tb = {
131 name = "test",
132 list = {
133 1,
134 2
135 },
136 value = 42
137 }
138 return assert.same(tb.list, {
139 1,
140 2
141 })
142 end)
143 it("should work with expressions", function()
144 local x = 10
145 local list = {
146 x + 1,
147 x + 2,
148 x + 3
149 }
150 return assert.same(list, {
151 11,
152 12,
153 13
154 })
155 end)
156 it("should support method calls in implicit object", function()
157 local tb = {
158 name = "test",
159 items = {
160 {
161 name = "item1",
162 getName = function(self)
163 return self.name
164 end
165 },
166 {
167 name = "item2",
168 getName = function(self)
169 return self.name
170 end
171 }
172 }
173 }
174 assert.same(tb.items[1]:getName(), "item1")
175 return assert.same(tb.items[2]:getName(), "item2")
176 end)
177 it("should work with complex nested structures", function()
178 local config = {
179 database = {
180 host = "localhost",
181 ports = {
182 8080,
183 8081,
184 8082
185 }
186 },
187 servers = {
188 {
189 name = "server1",
190 port = 8080
191 },
192 {
193 name = "server2",
194 port = 8081
195 }
196 }
197 }
198 assert.same(config.database.ports, {
199 8080,
200 8081,
201 8082
202 })
203 return assert.same(config.servers[1].name, "server1")
204 end)
205 it("should handle empty implicit object", function()
206 local tb = {
207 items = {
208 nil
209 }
210 }
211 return assert.same(tb, {
212 items = {
213 nil
214 }
215 })
216 end)
217 it("should work in function arguments", function()
218 local fn
219 fn = function(items)
220 return #items
221 end
222 local result = fn({
223 1,
224 2,
225 3
226 })
227 return assert.same(result, 3)
228 end)
229 return it("should support mixed asterisk and dash", function()
230 local tb = {
231 values = {
232 1,
233 2,
234 3
235 }
236 }
237 return assert.same(tb.values, {
238 1,
239 2,
240 3
241 })
242 end)
243end)