diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-27 00:30:56 +0000 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-27 00:30:56 +0000 |
| commit | 7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (patch) | |
| tree | ceba95c48bd8d5d9fff3d1206483ddf073c0e03d /spec/outputs/test/varargs_assignment_spec.lua | |
| parent | e70e63a9737ed3a9e72f1329901075498190e6b4 (diff) | |
| download | yuescript-compiler-improvements.tar.gz yuescript-compiler-improvements.tar.bz2 yuescript-compiler-improvements.zip | |
Add compiler improvements and comprehensive test suitecompiler-improvements
- Fixed path option handling to avoid semicolon concatenation issues
- Added exception handling for std::length_error and general exceptions
- Added comprehensive test specifications for advanced language features
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/outputs/test/varargs_assignment_spec.lua')
| -rw-r--r-- | spec/outputs/test/varargs_assignment_spec.lua | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/spec/outputs/test/varargs_assignment_spec.lua b/spec/outputs/test/varargs_assignment_spec.lua new file mode 100644 index 0000000..60eab29 --- /dev/null +++ b/spec/outputs/test/varargs_assignment_spec.lua | |||
| @@ -0,0 +1,188 @@ | |||
| 1 | local _anon_func_0 = function(assert, select, _arg_0, ...) | ||
| 2 | local ok = _arg_0 | ||
| 3 | local count = select('#', ...) | ||
| 4 | assert.same(count, 5) | ||
| 5 | return assert.same(ok, true) | ||
| 6 | end | ||
| 7 | local _anon_func_1 = function(assert, select, ...) | ||
| 8 | local first = select(1, ...) | ||
| 9 | local second = select(2, ...) | ||
| 10 | local third = select(3, ...) | ||
| 11 | assert.same(first, 10) | ||
| 12 | assert.same(second, 20) | ||
| 13 | return assert.same(third, 30) | ||
| 14 | end | ||
| 15 | local _anon_func_2 = function(assert, select, _arg_0, ...) | ||
| 16 | local success = _arg_0 | ||
| 17 | assert.is_true(success) | ||
| 18 | return assert.same(select('#', ...), 3) | ||
| 19 | end | ||
| 20 | local _anon_func_3 = function(assert, select, ...) | ||
| 21 | local count = select('#', ...) | ||
| 22 | return assert.same(count, 0) | ||
| 23 | end | ||
| 24 | local _anon_func_4 = function(assert, select, _arg_0, ...) | ||
| 25 | local a = _arg_0 | ||
| 26 | assert.same(a, "first") | ||
| 27 | return assert.same(select('#', ...), 3) | ||
| 28 | end | ||
| 29 | local _anon_func_5 = function(assert, select, ...) | ||
| 30 | local count = select('#', ...) | ||
| 31 | assert.same(count, 5) | ||
| 32 | assert.same(select(1, ...), 1) | ||
| 33 | assert.same(select(2, ...), nil) | ||
| 34 | return assert.same(select(3, ...), 2) | ||
| 35 | end | ||
| 36 | local _anon_func_6 = function(assert, select, ...) | ||
| 37 | local count = select('#', ...) | ||
| 38 | return assert.same(count, 3) | ||
| 39 | end | ||
| 40 | local _anon_func_7 = function(a, assert, select, _arg_1, ...) | ||
| 41 | local b = _arg_1 | ||
| 42 | assert.same(a, 1) | ||
| 43 | assert.same(b, 4) | ||
| 44 | return assert.same(select('#', ...), 2) | ||
| 45 | end | ||
| 46 | local _anon_func_8 = function(assert, sum, ...) | ||
| 47 | local result = sum(...) | ||
| 48 | return assert.same(result, 15) | ||
| 49 | end | ||
| 50 | local _anon_func_9 = function(assert, string, ...) | ||
| 51 | local result = string.format("str: %s, num: %d, bool: %s", ...) | ||
| 52 | return assert.same(result, "str: hello, num: 123, bool: true") | ||
| 53 | end | ||
| 54 | local _anon_func_10 = function(assert, select, ...) | ||
| 55 | local count = select('#', ...) | ||
| 56 | assert.same(count, 1) | ||
| 57 | return assert.same(select(1, ...), 42) | ||
| 58 | end | ||
| 59 | local _anon_func_11 = function(assert, inner, _arg_0, _arg_1, ...) | ||
| 60 | local a, b = _arg_0, _arg_1 | ||
| 61 | local c, d = inner() | ||
| 62 | assert.same(a, 1) | ||
| 63 | assert.same(b, 2) | ||
| 64 | assert.same(c, 4) | ||
| 65 | return assert.same(d, 5) | ||
| 66 | end | ||
| 67 | return describe("varargs assignment", function() | ||
| 68 | it("should assign varargs from function", function() | ||
| 69 | local list = { | ||
| 70 | 1, | ||
| 71 | 2, | ||
| 72 | 3, | ||
| 73 | 4, | ||
| 74 | 5 | ||
| 75 | } | ||
| 76 | local fn | ||
| 77 | fn = function(ok) | ||
| 78 | return ok, table.unpack(list) | ||
| 79 | end | ||
| 80 | return _anon_func_0(assert, select, fn(true)) | ||
| 81 | end) | ||
| 82 | it("should access varargs elements", function() | ||
| 83 | local list = { | ||
| 84 | 10, | ||
| 85 | 20, | ||
| 86 | 30 | ||
| 87 | } | ||
| 88 | local fn | ||
| 89 | fn = function() | ||
| 90 | return table.unpack(list) | ||
| 91 | end | ||
| 92 | return _anon_func_1(assert, select, fn()) | ||
| 93 | end) | ||
| 94 | it("should work with pcall", function() | ||
| 95 | local fn | ||
| 96 | fn = function() | ||
| 97 | return 1, 2, 3 | ||
| 98 | end | ||
| 99 | return _anon_func_2(assert, select, pcall(fn)) | ||
| 100 | end) | ||
| 101 | it("should handle empty varargs", function() | ||
| 102 | local fn | ||
| 103 | fn = function() end | ||
| 104 | return _anon_func_3(assert, select, fn()) | ||
| 105 | end) | ||
| 106 | it("should work with mixed return values", function() | ||
| 107 | local fn | ||
| 108 | fn = function() | ||
| 109 | return "first", nil, "third", false | ||
| 110 | end | ||
| 111 | return _anon_func_4(assert, select, fn()) | ||
| 112 | end) | ||
| 113 | it("should preserve nil values in varargs", function() | ||
| 114 | local fn | ||
| 115 | fn = function() | ||
| 116 | return 1, nil, 2, nil, 3 | ||
| 117 | end | ||
| 118 | return _anon_func_5(assert, select, fn()) | ||
| 119 | end) | ||
| 120 | it("should work with table.unpack", function() | ||
| 121 | local tb = { | ||
| 122 | a = 1, | ||
| 123 | b = 2, | ||
| 124 | c = 3 | ||
| 125 | } | ||
| 126 | local fn | ||
| 127 | fn = function() | ||
| 128 | return table.unpack(tb) | ||
| 129 | end | ||
| 130 | return _anon_func_6(assert, select, fn()) | ||
| 131 | end) | ||
| 132 | it("should chain varargs assignment", function() | ||
| 133 | local fn1 | ||
| 134 | fn1 = function() | ||
| 135 | return 1, 2, 3 | ||
| 136 | end | ||
| 137 | local fn2 | ||
| 138 | fn2 = function() | ||
| 139 | return table.unpack({ | ||
| 140 | 4, | ||
| 141 | 5, | ||
| 142 | 6 | ||
| 143 | }) | ||
| 144 | end | ||
| 145 | return (function(_arg_0, ...) | ||
| 146 | local a = _arg_0 | ||
| 147 | return _anon_func_7(a, assert, select, fn2()) | ||
| 148 | end)(fn1()) | ||
| 149 | end) | ||
| 150 | it("should work in expressions", function() | ||
| 151 | local sum | ||
| 152 | sum = function(...) | ||
| 153 | local total = 0 | ||
| 154 | for i = 1, select('#', ...) do | ||
| 155 | if type(select(i, ...)) == "number" then | ||
| 156 | total = total + select(i, ...) | ||
| 157 | end | ||
| 158 | end | ||
| 159 | return total | ||
| 160 | end | ||
| 161 | local fn | ||
| 162 | fn = function() | ||
| 163 | return 1, 2, 3, 4, 5 | ||
| 164 | end | ||
| 165 | return _anon_func_8(assert, sum, fn()) | ||
| 166 | end) | ||
| 167 | it("should work with string.format", function() | ||
| 168 | return _anon_func_9(assert, string, "hello", 123, true) | ||
| 169 | end) | ||
| 170 | it("should handle single return value", function() | ||
| 171 | local fn | ||
| 172 | fn = function() | ||
| 173 | return 42 | ||
| 174 | end | ||
| 175 | return _anon_func_10(assert, select, fn()) | ||
| 176 | end) | ||
| 177 | return it("should work with nested functions", function() | ||
| 178 | local outer | ||
| 179 | outer = function() | ||
| 180 | return 1, 2, 3 | ||
| 181 | end | ||
| 182 | local inner | ||
| 183 | inner = function() | ||
| 184 | return 4, 5 | ||
| 185 | end | ||
| 186 | return _anon_func_11(assert, inner, outer()) | ||
| 187 | end) | ||
| 188 | end) | ||
