aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/cond_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/cond_spec.lua
parent5d5b657f606b5939062983b1f90c3359d542672e (diff)
downloadyuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip
Updated tests.
Diffstat (limited to 'spec/outputs/test/cond_spec.lua')
-rw-r--r--spec/outputs/test/cond_spec.lua237
1 files changed, 237 insertions, 0 deletions
diff --git a/spec/outputs/test/cond_spec.lua b/spec/outputs/test/cond_spec.lua
new file mode 100644
index 0000000..838809a
--- /dev/null
+++ b/spec/outputs/test/cond_spec.lua
@@ -0,0 +1,237 @@
1local _anon_func_0 = function(flag)
2 if flag then
3 return 1
4 else
5 return 0
6 end
7end
8local _anon_func_1 = function()
9 if nil then
10 return true
11 else
12 return false
13 end
14end
15local _anon_func_2 = function()
16 if false then
17 return true
18 else
19 return false
20 end
21end
22local _anon_func_3 = function()
23 if 0 then
24 return true
25 else
26 return false
27 end
28end
29local _anon_func_4 = function()
30 if "" then
31 return true
32 else
33 return false
34 end
35end
36local _anon_func_5 = function()
37 if { } then
38 return true
39 else
40 return false
41 end
42end
43local _anon_func_6 = function()
44 if 1 then
45 return true
46 else
47 return false
48 end
49end
50return describe("cond", function()
51 it("should execute if branch when condition is true", function()
52 local result = nil
53 if true then
54 result = "yes"
55 end
56 return assert.same(result, "yes")
57 end)
58 it("should execute else branch when condition is false", function()
59 local result = nil
60 if false then
61 result = "yes"
62 else
63 result = "no"
64 end
65 return assert.same(result, "no")
66 end)
67 it("should support elseif chain", function()
68 local value = 2
69 local result
70 if 1 == value then
71 result = "one"
72 elseif 2 == value then
73 result = "two"
74 else
75 result = "other"
76 end
77 return assert.same(result, "two")
78 end)
79 it("should handle nested conditions", function()
80 local result = nil
81 if true then
82 if true then
83 result = "nested"
84 end
85 end
86 return assert.same(result, "nested")
87 end)
88 it("should work as expression", function()
89 local value
90 if true then
91 value = "yes"
92 else
93 value = "no"
94 end
95 return assert.same(value, "yes")
96 end)
97 it("should work in string interpolation", function()
98 local flag = true
99 local result = "value is " .. tostring(_anon_func_0(flag))
100 return assert.same(result, "value is 1")
101 end)
102 it("should support chained comparisons", function()
103 return assert.is_true(1 < 2 and 2 <= 2 and 2 < 3)
104 end)
105 it("should short-circuit and expression", function()
106 local count = 0
107 local inc
108 inc = function()
109 count = count + 1
110 return false
111 end
112 local result = inc() and inc()
113 return assert.same(count, 1)
114 end)
115 it("should short-circuit or expression", function()
116 local count = 0
117 local inc
118 inc = function()
119 count = count + 1
120 return true
121 end
122 local result = inc() or inc()
123 return assert.same(count, 1)
124 end)
125 it("should support unless keyword", function()
126 local result = nil
127 if not false then
128 result = "executed"
129 end
130 return assert.same(result, "executed")
131 end)
132 it("should support unless with else", function()
133 local result = nil
134 if not true then
135 result = "no"
136 else
137 result = "yes"
138 end
139 return assert.same(result, "yes")
140 end)
141 it("should handle postfix if", function()
142 local result = nil
143 if true then
144 result = "yes"
145 end
146 return assert.same(result, "yes")
147 end)
148 it("should handle postfix unless", function()
149 local result = nil
150 if not false then
151 result = "yes"
152 end
153 return assert.same(result, "yes")
154 end)
155 it("should evaluate truthiness correctly", function()
156 assert.is_false(_anon_func_1())
157 assert.is_false(_anon_func_2())
158 assert.is_true(_anon_func_3())
159 assert.is_true(_anon_func_4())
160 assert.is_true(_anon_func_5())
161 return assert.is_true(_anon_func_6())
162 end)
163 it("should support and/or operators", function()
164 assert.same(true and false, false)
165 assert.same(false or true, true)
166 assert.same(nil or "default", "default")
167 return assert.same("value" or "default", "value")
168 end)
169 it("should handle complex boolean expressions", function()
170 local a, b, c = true, false, true
171 local result = a and b or c
172 return assert.same(result, c)
173 end)
174 it("should support not operator", function()
175 assert.is_true(not false)
176 assert.is_true(not nil)
177 assert.is_false(not true)
178 return assert.is_false(not 1)
179 end)
180 it("should work with table as condition", function()
181 local result = nil
182 if { } then
183 result = "truthy"
184 end
185 return assert.same(result, "truthy")
186 end)
187 it("should work with string as condition", function()
188 local result = nil
189 if "" then
190 result = "truthy"
191 end
192 return assert.same(result, "truthy")
193 end)
194 it("should work with zero as condition", function()
195 local result = nil
196 if 0 then
197 result = "truthy"
198 end
199 return assert.same(result, "truthy")
200 end)
201 it("should support multiple elseif branches", function()
202 local value = 3
203 local result
204 if value == 1 then
205 result = "one"
206 elseif value == 2 then
207 result = "two"
208 elseif value == 3 then
209 result = "three"
210 else
211 result = "other"
212 end
213 return assert.same(result, "three")
214 end)
215 it("should handle then keyword syntax", function()
216 local result
217 if true then
218 result = "yes"
219 else
220 result = "no"
221 end
222 return assert.same(result, "yes")
223 end)
224 return it("should work with function call in condition", function()
225 local return_true
226 return_true = function()
227 return true
228 end
229 local result
230 if return_true() then
231 result = "yes"
232 else
233 result = "no"
234 end
235 return assert.same(result, "yes")
236 end)
237end)