aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/existential_spec.lua
blob: 9485c346039c345523e1814b6e6e77b2f581072f (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
238
239
240
241
242
243
244
local _anon_func_0 = function(obj)
	if obj ~= nil then
		return obj.value
	end
	return nil
end
local _anon_func_1 = function(obj)
	if obj ~= nil then
		return obj.value
	end
	return nil
end
local _anon_func_2 = function(obj)
	if obj ~= nil then
		return obj.x
	end
	return nil
end
local _anon_func_3 = function(obj)
	if obj ~= nil then
		return obj.y
	end
	return nil
end
return describe("existential", function()
	it("should handle ?. with existing object", function()
		local obj = {
			value = 42
		}
		local result
		if obj ~= nil then
			result = obj.value
		end
		return assert.same(result, 42)
	end)
	it("should handle ?. with nil object", function()
		local obj = nil
		local result
		if obj ~= nil then
			result = obj.value
		end
		return assert.same(result, nil)
	end)
	it("should chain ?. calls", function()
		local obj = {
			nested = {
				value = 100
			}
		}
		local result
		if obj ~= nil then
			do
				local _obj_0 = obj.nested
				if _obj_0 ~= nil then
					result = _obj_0.value
				end
			end
		end
		return assert.same(result, 100)
	end)
	it("should return nil in chain with nil", function()
		local obj = nil
		local result
		if obj ~= nil then
			do
				local _obj_0 = obj.nested
				if _obj_0 ~= nil then
					result = _obj_0.value
				end
			end
		end
		return assert.same(result, nil)
	end)
	it("should handle ?. with method call", function()
		local obj = {
			func = function()
				return "result"
			end
		}
		local result
		if obj ~= nil then
			result = obj.func()
		end
		return assert.same(result, "result")
	end)
	it("should handle ? on table index", function()
		local tb = {
			[1] = "first"
		}
		local result
		if tb ~= nil then
			result = tb[1]
		end
		return assert.same(result, "first")
	end)
	it("should return nil for missing index", function()
		local tb = { }
		local result
		if tb ~= nil then
			result = tb[99]
		end
		return assert.same(result, nil)
	end)
	it("should work with ? in if condition", function()
		local obj = {
			value = 5
		}
		local result
		if _anon_func_0(obj) then
			result = "exists"
		end
		return assert.same(result, "exists")
	end)
	it("should combine ?. with and/or", function()
		local obj = {
			value = 10
		}
		local result = _anon_func_1(obj) and 20 or 30
		return assert.same(result, 20)
	end)
	it("should handle with? safely", function()
		local obj = {
			x = 1,
			y = 2
		}
		local sum = _anon_func_2(obj) + _anon_func_3(obj)
		return assert.same(sum, 3)
	end)
	it("should return nil with with? on nil", function()
		local obj = nil
		local result
		if obj ~= nil then
			result = obj.x
		end
		return assert.same(result, nil)
	end)
	it("should handle false value correctly", function()
		local obj = {
			value = false
		}
		local result
		if obj ~= nil then
			result = obj.value
		end
		return assert.same(result, false)
	end)
	it("should handle 0 value correctly", function()
		local obj = {
			value = 0
		}
		local result
		if obj ~= nil then
			result = obj.value
		end
		return assert.same(result, 0)
	end)
	it("should handle empty string correctly", function()
		local obj = {
			value = ""
		}
		local result
		if obj ~= nil then
			result = obj.value
		end
		return assert.same(result, "")
	end)
	it("should handle empty table correctly", function()
		local obj = {
			value = { }
		}
		local result
		if obj ~= nil then
			result = obj.value
		end
		return assert.same(type(result), "table")
	end)
	it("should work with deep chains", function()
		local obj = {
			a = {
				b = {
					c = {
						d = "deep"
					}
				}
			}
		}
		local result
		if obj ~= nil then
			do
				local _obj_0 = obj.a
				if _obj_0 ~= nil then
					do
						local _obj_1 = _obj_0.b
						if _obj_1 ~= nil then
							do
								local _obj_2 = _obj_1.c
								if _obj_2 ~= nil then
									result = _obj_2.d
								end
							end
						end
					end
				end
			end
		end
		return assert.same(result, "deep")
	end)
	it("should break chain on first nil", function()
		local obj = {
			a = nil
		}
		local result
		if obj ~= nil then
			do
				local _obj_0 = obj.a
				if _obj_0 ~= nil then
					do
						local _obj_1 = _obj_0.b
						if _obj_1 ~= nil then
							result = _obj_1.c
						end
					end
				end
			end
		end
		return assert.same(result, nil)
	end)
	it("should handle ?. with string methods", function()
		local s = "hello"
		local result
		if s ~= nil then
			result = s:upper()
		end
		return assert.same(result, "HELLO")
	end)
	return it("should handle ?. with nil string", function()
		local s = nil
		local result
		if s ~= nil then
			result = s:upper()
		end
		return assert.same(result, nil)
	end)
end)