aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/cond_spec.lua
blob: 838809a00745ca26dca00788a91cf0acf10c15d4 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
local _anon_func_0 = function(flag)
	if flag then
		return 1
	else
		return 0
	end
end
local _anon_func_1 = function()
	if nil then
		return true
	else
		return false
	end
end
local _anon_func_2 = function()
	if false then
		return true
	else
		return false
	end
end
local _anon_func_3 = function()
	if 0 then
		return true
	else
		return false
	end
end
local _anon_func_4 = function()
	if "" then
		return true
	else
		return false
	end
end
local _anon_func_5 = function()
	if { } then
		return true
	else
		return false
	end
end
local _anon_func_6 = function()
	if 1 then
		return true
	else
		return false
	end
end
return describe("cond", function()
	it("should execute if branch when condition is true", function()
		local result = nil
		if true then
			result = "yes"
		end
		return assert.same(result, "yes")
	end)
	it("should execute else branch when condition is false", function()
		local result = nil
		if false then
			result = "yes"
		else
			result = "no"
		end
		return assert.same(result, "no")
	end)
	it("should support elseif chain", function()
		local value = 2
		local result
		if 1 == value then
			result = "one"
		elseif 2 == value then
			result = "two"
		else
			result = "other"
		end
		return assert.same(result, "two")
	end)
	it("should handle nested conditions", function()
		local result = nil
		if true then
			if true then
				result = "nested"
			end
		end
		return assert.same(result, "nested")
	end)
	it("should work as expression", function()
		local value
		if true then
			value = "yes"
		else
			value = "no"
		end
		return assert.same(value, "yes")
	end)
	it("should work in string interpolation", function()
		local flag = true
		local result = "value is " .. tostring(_anon_func_0(flag))
		return assert.same(result, "value is 1")
	end)
	it("should support chained comparisons", function()
		return assert.is_true(1 < 2 and 2 <= 2 and 2 < 3)
	end)
	it("should short-circuit and expression", function()
		local count = 0
		local inc
		inc = function()
			count = count + 1
			return false
		end
		local result = inc() and inc()
		return assert.same(count, 1)
	end)
	it("should short-circuit or expression", function()
		local count = 0
		local inc
		inc = function()
			count = count + 1
			return true
		end
		local result = inc() or inc()
		return assert.same(count, 1)
	end)
	it("should support unless keyword", function()
		local result = nil
		if not false then
			result = "executed"
		end
		return assert.same(result, "executed")
	end)
	it("should support unless with else", function()
		local result = nil
		if not true then
			result = "no"
		else
			result = "yes"
		end
		return assert.same(result, "yes")
	end)
	it("should handle postfix if", function()
		local result = nil
		if true then
			result = "yes"
		end
		return assert.same(result, "yes")
	end)
	it("should handle postfix unless", function()
		local result = nil
		if not false then
			result = "yes"
		end
		return assert.same(result, "yes")
	end)
	it("should evaluate truthiness correctly", function()
		assert.is_false(_anon_func_1())
		assert.is_false(_anon_func_2())
		assert.is_true(_anon_func_3())
		assert.is_true(_anon_func_4())
		assert.is_true(_anon_func_5())
		return assert.is_true(_anon_func_6())
	end)
	it("should support and/or operators", function()
		assert.same(true and false, false)
		assert.same(false or true, true)
		assert.same(nil or "default", "default")
		return assert.same("value" or "default", "value")
	end)
	it("should handle complex boolean expressions", function()
		local a, b, c = true, false, true
		local result = a and b or c
		return assert.same(result, c)
	end)
	it("should support not operator", function()
		assert.is_true(not false)
		assert.is_true(not nil)
		assert.is_false(not true)
		return assert.is_false(not 1)
	end)
	it("should work with table as condition", function()
		local result = nil
		if { } then
			result = "truthy"
		end
		return assert.same(result, "truthy")
	end)
	it("should work with string as condition", function()
		local result = nil
		if "" then
			result = "truthy"
		end
		return assert.same(result, "truthy")
	end)
	it("should work with zero as condition", function()
		local result = nil
		if 0 then
			result = "truthy"
		end
		return assert.same(result, "truthy")
	end)
	it("should support multiple elseif branches", function()
		local value = 3
		local result
		if value == 1 then
			result = "one"
		elseif value == 2 then
			result = "two"
		elseif value == 3 then
			result = "three"
		else
			result = "other"
		end
		return assert.same(result, "three")
	end)
	it("should handle then keyword syntax", function()
		local result
		if true then
			result = "yes"
		else
			result = "no"
		end
		return assert.same(result, "yes")
	end)
	return it("should work with function call in condition", function()
		local return_true
		return_true = function()
			return true
		end
		local result
		if return_true() then
			result = "yes"
		else
			result = "no"
		end
		return assert.same(result, "yes")
	end)
end)