aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/existential_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/outputs/test/existential_spec.lua')
-rw-r--r--spec/outputs/test/existential_spec.lua244
1 files changed, 244 insertions, 0 deletions
diff --git a/spec/outputs/test/existential_spec.lua b/spec/outputs/test/existential_spec.lua
new file mode 100644
index 0000000..9485c34
--- /dev/null
+++ b/spec/outputs/test/existential_spec.lua
@@ -0,0 +1,244 @@
1local _anon_func_0 = function(obj)
2 if obj ~= nil then
3 return obj.value
4 end
5 return nil
6end
7local _anon_func_1 = function(obj)
8 if obj ~= nil then
9 return obj.value
10 end
11 return nil
12end
13local _anon_func_2 = function(obj)
14 if obj ~= nil then
15 return obj.x
16 end
17 return nil
18end
19local _anon_func_3 = function(obj)
20 if obj ~= nil then
21 return obj.y
22 end
23 return nil
24end
25return describe("existential", function()
26 it("should handle ?. with existing object", function()
27 local obj = {
28 value = 42
29 }
30 local result
31 if obj ~= nil then
32 result = obj.value
33 end
34 return assert.same(result, 42)
35 end)
36 it("should handle ?. with nil object", function()
37 local obj = nil
38 local result
39 if obj ~= nil then
40 result = obj.value
41 end
42 return assert.same(result, nil)
43 end)
44 it("should chain ?. calls", function()
45 local obj = {
46 nested = {
47 value = 100
48 }
49 }
50 local result
51 if obj ~= nil then
52 do
53 local _obj_0 = obj.nested
54 if _obj_0 ~= nil then
55 result = _obj_0.value
56 end
57 end
58 end
59 return assert.same(result, 100)
60 end)
61 it("should return nil in chain with nil", function()
62 local obj = nil
63 local result
64 if obj ~= nil then
65 do
66 local _obj_0 = obj.nested
67 if _obj_0 ~= nil then
68 result = _obj_0.value
69 end
70 end
71 end
72 return assert.same(result, nil)
73 end)
74 it("should handle ?. with method call", function()
75 local obj = {
76 func = function()
77 return "result"
78 end
79 }
80 local result
81 if obj ~= nil then
82 result = obj.func()
83 end
84 return assert.same(result, "result")
85 end)
86 it("should handle ? on table index", function()
87 local tb = {
88 [1] = "first"
89 }
90 local result
91 if tb ~= nil then
92 result = tb[1]
93 end
94 return assert.same(result, "first")
95 end)
96 it("should return nil for missing index", function()
97 local tb = { }
98 local result
99 if tb ~= nil then
100 result = tb[99]
101 end
102 return assert.same(result, nil)
103 end)
104 it("should work with ? in if condition", function()
105 local obj = {
106 value = 5
107 }
108 local result
109 if _anon_func_0(obj) then
110 result = "exists"
111 end
112 return assert.same(result, "exists")
113 end)
114 it("should combine ?. with and/or", function()
115 local obj = {
116 value = 10
117 }
118 local result = _anon_func_1(obj) and 20 or 30
119 return assert.same(result, 20)
120 end)
121 it("should handle with? safely", function()
122 local obj = {
123 x = 1,
124 y = 2
125 }
126 local sum = _anon_func_2(obj) + _anon_func_3(obj)
127 return assert.same(sum, 3)
128 end)
129 it("should return nil with with? on nil", function()
130 local obj = nil
131 local result
132 if obj ~= nil then
133 result = obj.x
134 end
135 return assert.same(result, nil)
136 end)
137 it("should handle false value correctly", function()
138 local obj = {
139 value = false
140 }
141 local result
142 if obj ~= nil then
143 result = obj.value
144 end
145 return assert.same(result, false)
146 end)
147 it("should handle 0 value correctly", function()
148 local obj = {
149 value = 0
150 }
151 local result
152 if obj ~= nil then
153 result = obj.value
154 end
155 return assert.same(result, 0)
156 end)
157 it("should handle empty string correctly", function()
158 local obj = {
159 value = ""
160 }
161 local result
162 if obj ~= nil then
163 result = obj.value
164 end
165 return assert.same(result, "")
166 end)
167 it("should handle empty table correctly", function()
168 local obj = {
169 value = { }
170 }
171 local result
172 if obj ~= nil then
173 result = obj.value
174 end
175 return assert.same(type(result), "table")
176 end)
177 it("should work with deep chains", function()
178 local obj = {
179 a = {
180 b = {
181 c = {
182 d = "deep"
183 }
184 }
185 }
186 }
187 local result
188 if obj ~= nil then
189 do
190 local _obj_0 = obj.a
191 if _obj_0 ~= nil then
192 do
193 local _obj_1 = _obj_0.b
194 if _obj_1 ~= nil then
195 do
196 local _obj_2 = _obj_1.c
197 if _obj_2 ~= nil then
198 result = _obj_2.d
199 end
200 end
201 end
202 end
203 end
204 end
205 end
206 return assert.same(result, "deep")
207 end)
208 it("should break chain on first nil", function()
209 local obj = {
210 a = nil
211 }
212 local result
213 if obj ~= nil then
214 do
215 local _obj_0 = obj.a
216 if _obj_0 ~= nil then
217 do
218 local _obj_1 = _obj_0.b
219 if _obj_1 ~= nil then
220 result = _obj_1.c
221 end
222 end
223 end
224 end
225 end
226 return assert.same(result, nil)
227 end)
228 it("should handle ?. with string methods", function()
229 local s = "hello"
230 local result
231 if s ~= nil then
232 result = s:upper()
233 end
234 return assert.same(result, "HELLO")
235 end)
236 return it("should handle ?. with nil string", function()
237 local s = nil
238 local result
239 if s ~= nil then
240 result = s:upper()
241 end
242 return assert.same(result, nil)
243 end)
244end)