From e02321107277a63e7dcb12ab163c9942ac101b87 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 26 Jan 2026 17:45:26 +0800 Subject: Updated tests. --- spec/outputs/test/existential_spec.lua | 244 +++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 spec/outputs/test/existential_spec.lua (limited to 'spec/outputs/test/existential_spec.lua') 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 @@ +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) -- cgit v1.2.3-55-g6feb