aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/with_spec.lua
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-26 17:45:26 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-26 17:45:56 +0800
commite02321107277a63e7dcb12ab163c9942ac101b87 (patch)
treef38c6f2fbf4ee35df4938933dbc1e645733356f4 /spec/outputs/test/with_spec.lua
parent5d5b657f606b5939062983b1f90c3359d542672e (diff)
downloadyuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip
Updated tests.
Diffstat (limited to 'spec/outputs/test/with_spec.lua')
-rw-r--r--spec/outputs/test/with_spec.lua166
1 files changed, 166 insertions, 0 deletions
diff --git a/spec/outputs/test/with_spec.lua b/spec/outputs/test/with_spec.lua
new file mode 100644
index 0000000..252fe7f
--- /dev/null
+++ b/spec/outputs/test/with_spec.lua
@@ -0,0 +1,166 @@
1return describe("with", function()
2 it("should access property with . syntax", function()
3 local obj = {
4 value = 42
5 }
6 local result = obj.value
7 assert.same(result, 42)
8 return obj
9 end)
10 it("should call method with : syntax", function()
11 local obj = {
12 func = function()
13 return "result"
14 end
15 }
16 local result = obj:func()
17 assert.same(result, "result")
18 return obj
19 end)
20 it("should work as statement", function()
21 local obj = {
22 x = 10,
23 y = 20
24 }
25 obj.sum = obj.x + obj.y
26 return assert.same(obj.sum, 30)
27 end)
28 it("should support nested with", function()
29 local outer = {
30 inner = {
31 value = 100
32 }
33 }
34 local _with_0 = outer.inner
35 local result = _with_0.value
36 assert.same(result, 100)
37 return _with_0
38 end)
39 it("should work with? safely", function()
40 local obj = {
41 x = 5
42 }
43 local result = obj.x
44 assert.same(result, 5)
45 return obj
46 end)
47 it("should work with if inside with", function()
48 local obj = {
49 x = 10,
50 y = 20
51 }
52 if obj.x > 5 then
53 local result = obj.x + obj.y
54 assert.same(result, 30)
55 end
56 return obj
57 end)
58 it("should work with switch inside with", function()
59 local obj = {
60 type = "add",
61 a = 5,
62 b = 3
63 }
64 local result
65 do
66 local _exp_0 = obj.type
67 if "add" == _exp_0 then
68 result = obj.a + obj.b
69 else
70 result = 0
71 end
72 end
73 assert.same(result, 8)
74 return obj
75 end)
76 it("should work with loop inside with", function()
77 local obj = {
78 items = {
79 1,
80 2,
81 3
82 }
83 }
84 local sum = 0
85 local _list_0 = obj.items
86 for _index_0 = 1, #_list_0 do
87 local item = _list_0[_index_0]
88 sum = sum + item
89 end
90 return assert.same(sum, 6)
91 end)
92 it("should work with destructure", function()
93 local obj = {
94 x = 1,
95 y = 2,
96 z = 3
97 }
98 local x, y, z = obj.x, obj.y, obj.z
99 assert.same(x, 1)
100 assert.same(y, 2)
101 assert.same(z, 3)
102 return obj
103 end)
104 it("should handle simple with body", function()
105 local obj = {
106 value = 42
107 }
108 obj.value2 = 100
109 return assert.same(obj.value2, 100)
110 end)
111 it("should work with return inside", function()
112 local obj = {
113 value = 100
114 }
115 local fn
116 fn = function()
117 return obj.value
118 end
119 return assert.same(fn(), 100)
120 end)
121 it("should work with break inside", function()
122 local sum = 0
123 for i = 1, 5 do
124 local obj = {
125 value = i
126 }
127 if obj.value == 3 then
128 break
129 end
130 sum = sum + obj.value
131 end
132 return assert.same(sum, 3)
133 end)
134 it("should chain property access", function()
135 local obj = {
136 a = {
137 b = {
138 c = 42
139 }
140 }
141 }
142 local _with_0 = obj.a.b
143 local result = _with_0.c
144 assert.same(result, 42)
145 return _with_0
146 end)
147 it("should work with multiple statements", function()
148 local obj = {
149 x = 1,
150 y = 2
151 }
152 local sum = 0
153 do
154 sum = sum + obj.x
155 sum = sum + obj.y
156 end
157 return assert.same(sum, 3)
158 end)
159 return it("should preserve object reference", function()
160 local obj = {
161 value = 42
162 }
163 obj.value = 100
164 return assert.same(obj.value, 100)
165 end)
166end)