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