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 | |
| parent | e70e63a9737ed3a9e72f1329901075498190e6b4 (diff) | |
| download | yuescript-7c2a92b82e9808d3c5ea29b47d1c59d663fe984a.tar.gz yuescript-7c2a92b82e9808d3c5ea29b47d1c59d663fe984a.tar.bz2 yuescript-7c2a92b82e9808d3c5ea29b47d1c59d663fe984a.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')
| -rw-r--r-- | spec/outputs/test/chaining_comparison_spec.lua | 85 | ||||
| -rw-r--r-- | spec/outputs/test/if_assignment_spec.lua | 171 | ||||
| -rw-r--r-- | spec/outputs/test/in_expression_spec.lua | 489 | ||||
| -rw-r--r-- | spec/outputs/test/named_varargs_spec.lua | 246 | ||||
| -rw-r--r-- | spec/outputs/test/prefixed_return_spec.lua | 105 | ||||
| -rw-r--r-- | spec/outputs/test/reverse_index_spec.lua | 152 | ||||
| -rw-r--r-- | spec/outputs/test/table_append_spec.lua | 160 | ||||
| -rw-r--r-- | spec/outputs/test/varargs_assignment_spec.lua | 188 | ||||
| -rw-r--r-- | spec/outputs/test/while_assignment_spec.lua | 84 | ||||
| -rw-r--r-- | spec/outputs/test/yaml_string_spec.lua | 99 |
10 files changed, 1779 insertions, 0 deletions
diff --git a/spec/outputs/test/chaining_comparison_spec.lua b/spec/outputs/test/chaining_comparison_spec.lua new file mode 100644 index 0000000..fe61fae --- /dev/null +++ b/spec/outputs/test/chaining_comparison_spec.lua | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | local _anon_func_0 = function() | ||
| 2 | local _cond_0 = "b" | ||
| 3 | if not ("a" < _cond_0) then | ||
| 4 | return false | ||
| 5 | else | ||
| 6 | return _cond_0 < "c" | ||
| 7 | end | ||
| 8 | end | ||
| 9 | local _anon_func_1 = function() | ||
| 10 | local _cond_0 = "b" | ||
| 11 | if not ("a" <= _cond_0) then | ||
| 12 | return false | ||
| 13 | else | ||
| 14 | return _cond_0 <= "c" | ||
| 15 | end | ||
| 16 | end | ||
| 17 | local _anon_func_2 = function(v) | ||
| 18 | local _cond_0 = v(2) | ||
| 19 | if not (v(1) < _cond_0) then | ||
| 20 | return false | ||
| 21 | else | ||
| 22 | return _cond_0 < v(3) | ||
| 23 | end | ||
| 24 | end | ||
| 25 | return describe("chaining comparison", function() | ||
| 26 | it("should support simple chaining", function() | ||
| 27 | assert.is_true(1 < 2 and 2 < 3) | ||
| 28 | assert.is_true(1 <= 2 and 2 <= 3) | ||
| 29 | assert.is_true(3 > 2 and 2 > 1) | ||
| 30 | return assert.is_true(3 >= 2 and 2 >= 1) | ||
| 31 | end) | ||
| 32 | it("should support complex chaining", function() | ||
| 33 | return assert.is_true(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5) | ||
| 34 | end) | ||
| 35 | it("should work with variables", function() | ||
| 36 | local a = 5 | ||
| 37 | assert.is_true(1 <= a and a <= 10) | ||
| 38 | assert.is_true(a >= 3) | ||
| 39 | return assert.is_true(a <= 10) | ||
| 40 | end) | ||
| 41 | it("should handle mixed comparisons", function() | ||
| 42 | local x = 5 | ||
| 43 | assert.is_true(1 < x and x < 10) | ||
| 44 | return assert.is_true(1 <= x and x <= 5) | ||
| 45 | end) | ||
| 46 | it("should work with string comparisons", function() | ||
| 47 | assert.is_true(_anon_func_0()) | ||
| 48 | return assert.is_true(_anon_func_1()) | ||
| 49 | end) | ||
| 50 | it("should handle edge cases", function() | ||
| 51 | assert.is_true(0 <= 0 and 0 <= 0) | ||
| 52 | return assert.is_true(-5 < 0 and 0 < 5) | ||
| 53 | end) | ||
| 54 | it("should work in expressions", function() | ||
| 55 | local result | ||
| 56 | if 1 < 2 and 2 < 3 then | ||
| 57 | result = "yes" | ||
| 58 | else | ||
| 59 | result = "no" | ||
| 60 | end | ||
| 61 | return assert.same(result, "yes") | ||
| 62 | end) | ||
| 63 | it("should support != operator", function() | ||
| 64 | assert.is_true(1 ~= 2 and 2 ~= 3) | ||
| 65 | return assert.is_true(1 ~= 2 and 2 ~= 3) | ||
| 66 | end) | ||
| 67 | it("should handle boolean results", function() | ||
| 68 | assert.is_true(1 < 2 and 2 < 3) | ||
| 69 | return assert.is_false(3 < 2 and 2 < 1) | ||
| 70 | end) | ||
| 71 | it("should work with function calls", function() | ||
| 72 | local v | ||
| 73 | v = function(x) | ||
| 74 | return x | ||
| 75 | end | ||
| 76 | return assert.is_true(_anon_func_2(v)) | ||
| 77 | end) | ||
| 78 | it("should handle negation", function() | ||
| 79 | return assert.is_true(-10 < -5 and -5 < 0) | ||
| 80 | end) | ||
| 81 | return it("should support mixed operators", function() | ||
| 82 | assert.is_true(1 < 2 and 2 <= 2 and 2 < 3) | ||
| 83 | return assert.is_true(3 > 2 and 2 >= 2 and 2 > 1) | ||
| 84 | end) | ||
| 85 | end) | ||
diff --git a/spec/outputs/test/if_assignment_spec.lua b/spec/outputs/test/if_assignment_spec.lua new file mode 100644 index 0000000..7d3b708 --- /dev/null +++ b/spec/outputs/test/if_assignment_spec.lua | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | return describe("if assignment", function() | ||
| 2 | it("should assign and check truthy value", function() | ||
| 3 | local obj = { | ||
| 4 | find_user = function(name) | ||
| 5 | return name == "valid" and { | ||
| 6 | name = name | ||
| 7 | } or nil | ||
| 8 | end | ||
| 9 | } | ||
| 10 | local user = obj:find_user("valid") | ||
| 11 | if user then | ||
| 12 | return assert.same(user.name, "valid") | ||
| 13 | end | ||
| 14 | end) | ||
| 15 | it("should not enter block when nil", function() | ||
| 16 | local obj = { | ||
| 17 | find_user = function() | ||
| 18 | return nil | ||
| 19 | end | ||
| 20 | } | ||
| 21 | local user = obj:find_user() | ||
| 22 | if user then | ||
| 23 | return assert.is_true(false) | ||
| 24 | else | ||
| 25 | return assert.is_true(true) | ||
| 26 | end | ||
| 27 | end) | ||
| 28 | it("should work with elseif", function() | ||
| 29 | local get_value | ||
| 30 | get_value = function(key) | ||
| 31 | if "a" == key then | ||
| 32 | return 1 | ||
| 33 | elseif "b" == key then | ||
| 34 | return 2 | ||
| 35 | else | ||
| 36 | return nil | ||
| 37 | end | ||
| 38 | end | ||
| 39 | local result = nil | ||
| 40 | do | ||
| 41 | local val = get_value("c") | ||
| 42 | if val then | ||
| 43 | result = "c: " .. tostring(val) | ||
| 44 | else | ||
| 45 | val = get_value("b") | ||
| 46 | if val then | ||
| 47 | result = "b: " .. tostring(val) | ||
| 48 | else | ||
| 49 | result = "no match" | ||
| 50 | end | ||
| 51 | end | ||
| 52 | end | ||
| 53 | return assert.same(result, "b: 2") | ||
| 54 | end) | ||
| 55 | it("should scope variable to if block", function() | ||
| 56 | do | ||
| 57 | local x = 10 | ||
| 58 | if x then | ||
| 59 | assert.same(x, 10) | ||
| 60 | end | ||
| 61 | end | ||
| 62 | return assert.is_true(true) | ||
| 63 | end) | ||
| 64 | it("should work with multiple return values", function() | ||
| 65 | local fn | ||
| 66 | fn = function() | ||
| 67 | return true, "success" | ||
| 68 | end | ||
| 69 | local success, result = fn() | ||
| 70 | if success then | ||
| 71 | assert.is_true(success) | ||
| 72 | return assert.same(result, "success") | ||
| 73 | end | ||
| 74 | end) | ||
| 75 | it("should work with table destructuring", function() | ||
| 76 | local get_point | ||
| 77 | get_point = function() | ||
| 78 | return { | ||
| 79 | x = 10, | ||
| 80 | y = 20 | ||
| 81 | } | ||
| 82 | end | ||
| 83 | local _des_0 = get_point() | ||
| 84 | if _des_0 then | ||
| 85 | local x, y = _des_0.x, _des_0.y | ||
| 86 | assert.same(x, 10) | ||
| 87 | return assert.same(y, 20) | ||
| 88 | end | ||
| 89 | end) | ||
| 90 | it("should work with array destructuring", function() | ||
| 91 | local get_coords | ||
| 92 | get_coords = function() | ||
| 93 | return { | ||
| 94 | 1, | ||
| 95 | 2, | ||
| 96 | 3 | ||
| 97 | } | ||
| 98 | end | ||
| 99 | local _des_0 = get_coords() | ||
| 100 | if _des_0 then | ||
| 101 | local a, b, c = _des_0[1], _des_0[2], _des_0[3] | ||
| 102 | assert.same(a, 1) | ||
| 103 | assert.same(b, 2) | ||
| 104 | return assert.same(c, 3) | ||
| 105 | end | ||
| 106 | end) | ||
| 107 | it("should chain multiple assignments", function() | ||
| 108 | local a = 1 | ||
| 109 | if a then | ||
| 110 | local b = a + 1 | ||
| 111 | if b then | ||
| 112 | return assert.same(b, 2) | ||
| 113 | end | ||
| 114 | end | ||
| 115 | end) | ||
| 116 | it("should work in expression context", function() | ||
| 117 | local get_value | ||
| 118 | get_value = function(x) | ||
| 119 | if x > 0 then | ||
| 120 | return x | ||
| 121 | else | ||
| 122 | return nil | ||
| 123 | end | ||
| 124 | end | ||
| 125 | local result | ||
| 126 | do | ||
| 127 | local val = get_value(5) | ||
| 128 | if val then | ||
| 129 | result = val * 2 | ||
| 130 | else | ||
| 131 | result = 0 | ||
| 132 | end | ||
| 133 | end | ||
| 134 | return assert.same(result, 10) | ||
| 135 | end) | ||
| 136 | it("should work with os.getenv", function() | ||
| 137 | local path = os.getenv("PATH") | ||
| 138 | if path then | ||
| 139 | return assert.is_true(type(path) == "string") | ||
| 140 | else | ||
| 141 | return assert.is_true(true) | ||
| 142 | end | ||
| 143 | end) | ||
| 144 | it("should support table access", function() | ||
| 145 | local tb = { | ||
| 146 | key = "value" | ||
| 147 | } | ||
| 148 | local val = tb.key | ||
| 149 | if val then | ||
| 150 | return assert.same(val, "value") | ||
| 151 | end | ||
| 152 | end) | ||
| 153 | it("should work with function call results", function() | ||
| 154 | local fn | ||
| 155 | fn = function() | ||
| 156 | return "result" | ||
| 157 | end | ||
| 158 | local s = fn() | ||
| 159 | if s then | ||
| 160 | return assert.same(s, "result") | ||
| 161 | end | ||
| 162 | end) | ||
| 163 | return it("should handle false values", function() | ||
| 164 | local val = false | ||
| 165 | if val then | ||
| 166 | return assert.is_true(false) | ||
| 167 | else | ||
| 168 | return assert.is_true(true) | ||
| 169 | end | ||
| 170 | end) | ||
| 171 | end) | ||
diff --git a/spec/outputs/test/in_expression_spec.lua b/spec/outputs/test/in_expression_spec.lua new file mode 100644 index 0000000..fc118c2 --- /dev/null +++ b/spec/outputs/test/in_expression_spec.lua | |||
| @@ -0,0 +1,489 @@ | |||
| 1 | local _anon_func_0 = function(items) | ||
| 2 | local _val_0 = 3 | ||
| 3 | for _index_0 = 1, #items do | ||
| 4 | if items[_index_0] == _val_0 then | ||
| 5 | return true | ||
| 6 | end | ||
| 7 | end | ||
| 8 | return false | ||
| 9 | end | ||
| 10 | local _anon_func_1 = function(items) | ||
| 11 | local _val_0 = 10 | ||
| 12 | for _index_0 = 1, #items do | ||
| 13 | if items[_index_0] == _val_0 then | ||
| 14 | return true | ||
| 15 | end | ||
| 16 | end | ||
| 17 | return false | ||
| 18 | end | ||
| 19 | local _anon_func_2 = function(chars) | ||
| 20 | local _val_0 = "b" | ||
| 21 | for _index_0 = 1, #chars do | ||
| 22 | if chars[_index_0] == _val_0 then | ||
| 23 | return true | ||
| 24 | end | ||
| 25 | end | ||
| 26 | return false | ||
| 27 | end | ||
| 28 | local _anon_func_3 = function(chars) | ||
| 29 | local _val_0 = "z" | ||
| 30 | for _index_0 = 1, #chars do | ||
| 31 | if chars[_index_0] == _val_0 then | ||
| 32 | return true | ||
| 33 | end | ||
| 34 | end | ||
| 35 | return false | ||
| 36 | end | ||
| 37 | local _anon_func_4 = function(obj) | ||
| 38 | local _val_0 = "x" | ||
| 39 | for _index_0 = 1, #obj do | ||
| 40 | if obj[_index_0] == _val_0 then | ||
| 41 | return true | ||
| 42 | end | ||
| 43 | end | ||
| 44 | return false | ||
| 45 | end | ||
| 46 | local _anon_func_5 = function(obj) | ||
| 47 | local _val_0 = "y" | ||
| 48 | for _index_0 = 1, #obj do | ||
| 49 | if obj[_index_0] == _val_0 then | ||
| 50 | return true | ||
| 51 | end | ||
| 52 | end | ||
| 53 | return false | ||
| 54 | end | ||
| 55 | local _anon_func_6 = function(obj) | ||
| 56 | local _val_0 = "w" | ||
| 57 | for _index_0 = 1, #obj do | ||
| 58 | if obj[_index_0] == _val_0 then | ||
| 59 | return true | ||
| 60 | end | ||
| 61 | end | ||
| 62 | return false | ||
| 63 | end | ||
| 64 | local _anon_func_7 = function(items) | ||
| 65 | local _val_0 = 1 | ||
| 66 | for _index_0 = 1, #items do | ||
| 67 | if items[_index_0] == _val_0 then | ||
| 68 | return true | ||
| 69 | end | ||
| 70 | end | ||
| 71 | return false | ||
| 72 | end | ||
| 73 | local _anon_func_8 = function(items) | ||
| 74 | local _val_0 = "two" | ||
| 75 | for _index_0 = 1, #items do | ||
| 76 | if items[_index_0] == _val_0 then | ||
| 77 | return true | ||
| 78 | end | ||
| 79 | end | ||
| 80 | return false | ||
| 81 | end | ||
| 82 | local _anon_func_9 = function(items) | ||
| 83 | local _val_0 = true | ||
| 84 | for _index_0 = 1, #items do | ||
| 85 | if items[_index_0] == _val_0 then | ||
| 86 | return true | ||
| 87 | end | ||
| 88 | end | ||
| 89 | return false | ||
| 90 | end | ||
| 91 | local _anon_func_10 = function(items) | ||
| 92 | local _val_0 = false | ||
| 93 | for _index_0 = 1, #items do | ||
| 94 | if items[_index_0] == _val_0 then | ||
| 95 | return true | ||
| 96 | end | ||
| 97 | end | ||
| 98 | return false | ||
| 99 | end | ||
| 100 | local _anon_func_11 = function(empty) | ||
| 101 | local _val_0 = 1 | ||
| 102 | for _index_0 = 1, #empty do | ||
| 103 | if empty[_index_0] == _val_0 then | ||
| 104 | return true | ||
| 105 | end | ||
| 106 | end | ||
| 107 | return false | ||
| 108 | end | ||
| 109 | local _anon_func_12 = function(empty) | ||
| 110 | local _val_0 = "test" | ||
| 111 | for _index_0 = 1, #empty do | ||
| 112 | if empty[_index_0] == _val_0 then | ||
| 113 | return true | ||
| 114 | end | ||
| 115 | end | ||
| 116 | return false | ||
| 117 | end | ||
| 118 | local _anon_func_13 = function(items) | ||
| 119 | local _val_0 = 2 | ||
| 120 | for _index_0 = 1, #items do | ||
| 121 | if items[_index_0] == _val_0 then | ||
| 122 | return true | ||
| 123 | end | ||
| 124 | end | ||
| 125 | return false | ||
| 126 | end | ||
| 127 | local _anon_func_14 = function(items) | ||
| 128 | local _val_0 = 4 | ||
| 129 | for _index_0 = 1, #items do | ||
| 130 | if items[_index_0] == _val_0 then | ||
| 131 | return true | ||
| 132 | end | ||
| 133 | end | ||
| 134 | return false | ||
| 135 | end | ||
| 136 | local _anon_func_15 = function(items) | ||
| 137 | local _val_0 = 2 | ||
| 138 | for _index_0 = 1, #items do | ||
| 139 | if items[_index_0] == _val_0 then | ||
| 140 | return true | ||
| 141 | end | ||
| 142 | end | ||
| 143 | return false | ||
| 144 | end | ||
| 145 | local _anon_func_16 = function(nested) | ||
| 146 | local _val_0 = { | ||
| 147 | 1, | ||
| 148 | 2 | ||
| 149 | } | ||
| 150 | for _index_0 = 1, #nested do | ||
| 151 | if nested[_index_0] == _val_0 then | ||
| 152 | return true | ||
| 153 | end | ||
| 154 | end | ||
| 155 | return false | ||
| 156 | end | ||
| 157 | local _anon_func_17 = function(nested) | ||
| 158 | local _val_0 = { | ||
| 159 | 1, | ||
| 160 | 3 | ||
| 161 | } | ||
| 162 | for _index_0 = 1, #nested do | ||
| 163 | if nested[_index_0] == _val_0 then | ||
| 164 | return true | ||
| 165 | end | ||
| 166 | end | ||
| 167 | return false | ||
| 168 | end | ||
| 169 | local _anon_func_18 = function(bools) | ||
| 170 | local _val_0 = true | ||
| 171 | for _index_0 = 1, #bools do | ||
| 172 | if bools[_index_0] == _val_0 then | ||
| 173 | return true | ||
| 174 | end | ||
| 175 | end | ||
| 176 | return false | ||
| 177 | end | ||
| 178 | local _anon_func_19 = function(bools) | ||
| 179 | local _val_0 = false | ||
| 180 | for _index_0 = 1, #bools do | ||
| 181 | if bools[_index_0] == _val_0 then | ||
| 182 | return true | ||
| 183 | end | ||
| 184 | end | ||
| 185 | return false | ||
| 186 | end | ||
| 187 | local _anon_func_20 = function(i, items) | ||
| 188 | for _index_0 = 1, #items do | ||
| 189 | if items[_index_0] == i then | ||
| 190 | return true | ||
| 191 | end | ||
| 192 | end | ||
| 193 | return false | ||
| 194 | end | ||
| 195 | local _anon_func_21 = function(key1, tb) | ||
| 196 | for _index_0 = 1, #tb do | ||
| 197 | if tb[_index_0] == key1 then | ||
| 198 | return true | ||
| 199 | end | ||
| 200 | end | ||
| 201 | return false | ||
| 202 | end | ||
| 203 | local _anon_func_22 = function(key2, tb) | ||
| 204 | for _index_0 = 1, #tb do | ||
| 205 | if tb[_index_0] == key2 then | ||
| 206 | return true | ||
| 207 | end | ||
| 208 | end | ||
| 209 | return false | ||
| 210 | end | ||
| 211 | local _anon_func_23 = function(get_items) | ||
| 212 | local _check_0 = get_items() | ||
| 213 | local _val_0 = 2 | ||
| 214 | for _index_0 = 1, #_check_0 do | ||
| 215 | if _check_0[_index_0] == _val_0 then | ||
| 216 | return true | ||
| 217 | end | ||
| 218 | end | ||
| 219 | return false | ||
| 220 | end | ||
| 221 | local _anon_func_24 = function(get_items) | ||
| 222 | local _check_0 = get_items() | ||
| 223 | local _val_0 = 5 | ||
| 224 | for _index_0 = 1, #_check_0 do | ||
| 225 | if _check_0[_index_0] == _val_0 then | ||
| 226 | return true | ||
| 227 | end | ||
| 228 | end | ||
| 229 | return false | ||
| 230 | end | ||
| 231 | local _anon_func_25 = function(items) | ||
| 232 | local _val_0 = nil | ||
| 233 | for _index_0 = 1, #items do | ||
| 234 | if items[_index_0] == _val_0 then | ||
| 235 | return true | ||
| 236 | end | ||
| 237 | end | ||
| 238 | return false | ||
| 239 | end | ||
| 240 | local _anon_func_26 = function(items) | ||
| 241 | local _val_0 = 1 | ||
| 242 | for _index_0 = 1, #items do | ||
| 243 | if items[_index_0] == _val_0 then | ||
| 244 | return true | ||
| 245 | end | ||
| 246 | end | ||
| 247 | return false | ||
| 248 | end | ||
| 249 | local _anon_func_27 = function(obj) | ||
| 250 | local _val_0 = "name" | ||
| 251 | for _index_0 = 1, #obj do | ||
| 252 | if obj[_index_0] == _val_0 then | ||
| 253 | return true | ||
| 254 | end | ||
| 255 | end | ||
| 256 | return false | ||
| 257 | end | ||
| 258 | local _anon_func_28 = function(obj) | ||
| 259 | local _val_0 = "value" | ||
| 260 | for _index_0 = 1, #obj do | ||
| 261 | if obj[_index_0] == _val_0 then | ||
| 262 | return true | ||
| 263 | end | ||
| 264 | end | ||
| 265 | return false | ||
| 266 | end | ||
| 267 | local _anon_func_29 = function(obj) | ||
| 268 | local _val_0 = "missing" | ||
| 269 | for _index_0 = 1, #obj do | ||
| 270 | if obj[_index_0] == _val_0 then | ||
| 271 | return true | ||
| 272 | end | ||
| 273 | end | ||
| 274 | return false | ||
| 275 | end | ||
| 276 | local _anon_func_30 = function(items) | ||
| 277 | local _val_0 = 2 | ||
| 278 | for _index_0 = 1, #items do | ||
| 279 | if items[_index_0] == _val_0 then | ||
| 280 | return true | ||
| 281 | end | ||
| 282 | end | ||
| 283 | return false | ||
| 284 | end | ||
| 285 | local _anon_func_31 = function(allowed, item) | ||
| 286 | for _index_0 = 1, #allowed do | ||
| 287 | if allowed[_index_0] == item then | ||
| 288 | return true | ||
| 289 | end | ||
| 290 | end | ||
| 291 | return false | ||
| 292 | end | ||
| 293 | return describe("in expression", function() | ||
| 294 | it("should check value in table", function() | ||
| 295 | local items = { | ||
| 296 | 1, | ||
| 297 | 2, | ||
| 298 | 3, | ||
| 299 | 4, | ||
| 300 | 5 | ||
| 301 | } | ||
| 302 | assert.is_true(_anon_func_0(items)) | ||
| 303 | return assert.is_false(_anon_func_1(items)) | ||
| 304 | end) | ||
| 305 | it("should work with strings", function() | ||
| 306 | local chars = { | ||
| 307 | "a", | ||
| 308 | "b", | ||
| 309 | "c" | ||
| 310 | } | ||
| 311 | assert.is_true(_anon_func_2(chars)) | ||
| 312 | return assert.is_false(_anon_func_3(chars)) | ||
| 313 | end) | ||
| 314 | it("should check keys in table", function() | ||
| 315 | local obj = { | ||
| 316 | x = 1, | ||
| 317 | y = 2, | ||
| 318 | z = 3 | ||
| 319 | } | ||
| 320 | assert.is_true(_anon_func_4(obj)) | ||
| 321 | assert.is_true(_anon_func_5(obj)) | ||
| 322 | return assert.is_false(_anon_func_6(obj)) | ||
| 323 | end) | ||
| 324 | it("should work with mixed types", function() | ||
| 325 | local items = { | ||
| 326 | 1, | ||
| 327 | "two", | ||
| 328 | true, | ||
| 329 | nil | ||
| 330 | } | ||
| 331 | assert.is_true(_anon_func_7(items)) | ||
| 332 | assert.is_true(_anon_func_8(items)) | ||
| 333 | assert.is_true(_anon_func_9(items)) | ||
| 334 | return assert.is_false(_anon_func_10(items)) | ||
| 335 | end) | ||
| 336 | it("should handle empty table", function() | ||
| 337 | local empty = { } | ||
| 338 | assert.is_false(_anon_func_11(empty)) | ||
| 339 | return assert.is_false(_anon_func_12(empty)) | ||
| 340 | end) | ||
| 341 | it("should work in conditional", function() | ||
| 342 | local items = { | ||
| 343 | 1, | ||
| 344 | 2, | ||
| 345 | 3 | ||
| 346 | } | ||
| 347 | local result | ||
| 348 | if _anon_func_13(items) then | ||
| 349 | result = "found" | ||
| 350 | else | ||
| 351 | result = "not found" | ||
| 352 | end | ||
| 353 | return assert.same(result, "found") | ||
| 354 | end) | ||
| 355 | it("should support negation", function() | ||
| 356 | local items = { | ||
| 357 | 1, | ||
| 358 | 2, | ||
| 359 | 3 | ||
| 360 | } | ||
| 361 | assert.is_true(not (_anon_func_14(items))) | ||
| 362 | return assert.is_false(not (_anon_func_15(items))) | ||
| 363 | end) | ||
| 364 | it("should work with nested tables", function() | ||
| 365 | local nested = { | ||
| 366 | { | ||
| 367 | 1, | ||
| 368 | 2 | ||
| 369 | }, | ||
| 370 | { | ||
| 371 | 3, | ||
| 372 | 4 | ||
| 373 | }, | ||
| 374 | { | ||
| 375 | 5, | ||
| 376 | 6 | ||
| 377 | } | ||
| 378 | } | ||
| 379 | assert.is_true(_anon_func_16(nested)) | ||
| 380 | return assert.is_false(_anon_func_17(nested)) | ||
| 381 | end) | ||
| 382 | it("should handle boolean values", function() | ||
| 383 | local bools = { | ||
| 384 | true, | ||
| 385 | false | ||
| 386 | } | ||
| 387 | assert.is_true(_anon_func_18(bools)) | ||
| 388 | return assert.is_true(_anon_func_19(bools)) | ||
| 389 | end) | ||
| 390 | it("should work in loop", function() | ||
| 391 | local items = { | ||
| 392 | 1, | ||
| 393 | 2, | ||
| 394 | 3, | ||
| 395 | 4, | ||
| 396 | 5 | ||
| 397 | } | ||
| 398 | local count = 0 | ||
| 399 | for i = 1, 10 do | ||
| 400 | if (#items > 0 and _anon_func_20(i, items)) then | ||
| 401 | count = count + 1 | ||
| 402 | end | ||
| 403 | end | ||
| 404 | return assert.same(count, 5) | ||
| 405 | end) | ||
| 406 | it("should support table as value", function() | ||
| 407 | local key1 = { | ||
| 408 | a = 1 | ||
| 409 | } | ||
| 410 | local key2 = { | ||
| 411 | b = 2 | ||
| 412 | } | ||
| 413 | local tb = { | ||
| 414 | [key1] = "first", | ||
| 415 | [key2] = "second" | ||
| 416 | } | ||
| 417 | assert.is_true((#tb > 0 and _anon_func_21(key1, tb))) | ||
| 418 | return assert.is_true((#tb > 0 and _anon_func_22(key2, tb))) | ||
| 419 | end) | ||
| 420 | it("should work with function results", function() | ||
| 421 | local get_items | ||
| 422 | get_items = function() | ||
| 423 | return { | ||
| 424 | 1, | ||
| 425 | 2, | ||
| 426 | 3 | ||
| 427 | } | ||
| 428 | end | ||
| 429 | assert.is_true(_anon_func_23(get_items)) | ||
| 430 | return assert.is_false(_anon_func_24(get_items)) | ||
| 431 | end) | ||
| 432 | it("should handle nil in table", function() | ||
| 433 | local items = { | ||
| 434 | 1, | ||
| 435 | nil, | ||
| 436 | 3 | ||
| 437 | } | ||
| 438 | assert.is_true(_anon_func_25(items)) | ||
| 439 | return assert.is_true(_anon_func_26(items)) | ||
| 440 | end) | ||
| 441 | it("should work with string keys", function() | ||
| 442 | local obj = { | ||
| 443 | name = "test", | ||
| 444 | value = 42 | ||
| 445 | } | ||
| 446 | assert.is_true(_anon_func_27(obj)) | ||
| 447 | assert.is_true(_anon_func_28(obj)) | ||
| 448 | return assert.is_false(_anon_func_29(obj)) | ||
| 449 | end) | ||
| 450 | it("should support complex expressions", function() | ||
| 451 | local items = { | ||
| 452 | 1, | ||
| 453 | 2, | ||
| 454 | 3 | ||
| 455 | } | ||
| 456 | local result = (_anon_func_30(items)) and "yes" or "no" | ||
| 457 | return assert.same(result, "yes") | ||
| 458 | end) | ||
| 459 | return it("should work in comprehension", function() | ||
| 460 | local source = { | ||
| 461 | 1, | ||
| 462 | 2, | ||
| 463 | 3, | ||
| 464 | 4, | ||
| 465 | 5 | ||
| 466 | } | ||
| 467 | local allowed = { | ||
| 468 | 2, | ||
| 469 | 4 | ||
| 470 | } | ||
| 471 | local result | ||
| 472 | do | ||
| 473 | local _accum_0 = { } | ||
| 474 | local _len_0 = 1 | ||
| 475 | for _index_0 = 1, #source do | ||
| 476 | local item = source[_index_0] | ||
| 477 | if (#allowed > 0 and _anon_func_31(allowed, item)) then | ||
| 478 | _accum_0[_len_0] = item | ||
| 479 | _len_0 = _len_0 + 1 | ||
| 480 | end | ||
| 481 | end | ||
| 482 | result = _accum_0 | ||
| 483 | end | ||
| 484 | return assert.same(result, { | ||
| 485 | 2, | ||
| 486 | 4 | ||
| 487 | }) | ||
| 488 | end) | ||
| 489 | end) | ||
diff --git a/spec/outputs/test/named_varargs_spec.lua b/spec/outputs/test/named_varargs_spec.lua new file mode 100644 index 0000000..2a71cea --- /dev/null +++ b/spec/outputs/test/named_varargs_spec.lua | |||
| @@ -0,0 +1,246 @@ | |||
| 1 | return describe("named varargs", function() | ||
| 2 | it("should store varargs in named table", function() | ||
| 3 | local f | ||
| 4 | f = function(...) | ||
| 5 | local t = { | ||
| 6 | n = select("#", ...), | ||
| 7 | ... | ||
| 8 | } | ||
| 9 | assert.same(t.n, 3) | ||
| 10 | assert.same(t[1], 1) | ||
| 11 | assert.same(t[2], 2) | ||
| 12 | return assert.same(t[3], 3) | ||
| 13 | end | ||
| 14 | return f(1, 2, 3) | ||
| 15 | end) | ||
| 16 | it("should handle string arguments", function() | ||
| 17 | local f | ||
| 18 | f = function(...) | ||
| 19 | local args = { | ||
| 20 | n = select("#", ...), | ||
| 21 | ... | ||
| 22 | } | ||
| 23 | assert.same(args.n, 3) | ||
| 24 | assert.same(args[1], "a") | ||
| 25 | assert.same(args[2], "b") | ||
| 26 | return assert.same(args[3], "c") | ||
| 27 | end | ||
| 28 | return f("a", "b", "c") | ||
| 29 | end) | ||
| 30 | it("should handle empty varargs", function() | ||
| 31 | local f | ||
| 32 | f = function(...) | ||
| 33 | local t = { | ||
| 34 | n = select("#", ...), | ||
| 35 | ... | ||
| 36 | } | ||
| 37 | assert.same(t.n, 0) | ||
| 38 | return assert.same(#t, 0) | ||
| 39 | end | ||
| 40 | return f() | ||
| 41 | end) | ||
| 42 | it("should preserve nil values", function() | ||
| 43 | local f | ||
| 44 | f = function(...) | ||
| 45 | local args = { | ||
| 46 | n = select("#", ...), | ||
| 47 | ... | ||
| 48 | } | ||
| 49 | assert.same(args.n, 5) | ||
| 50 | assert.same(args[1], 1) | ||
| 51 | assert.same(args[2], nil) | ||
| 52 | assert.same(args[3], 3) | ||
| 53 | assert.same(args[4], nil) | ||
| 54 | return assert.same(args[5], 5) | ||
| 55 | end | ||
| 56 | return f(1, nil, 3, nil, 5) | ||
| 57 | end) | ||
| 58 | it("should work with loop", function() | ||
| 59 | local f | ||
| 60 | f = function(...) | ||
| 61 | local t = { | ||
| 62 | n = select("#", ...), | ||
| 63 | ... | ||
| 64 | } | ||
| 65 | local sum = 0 | ||
| 66 | for i = 1, t.n do | ||
| 67 | if type(t[i]) == "number" then | ||
| 68 | sum = sum + t[i] | ||
| 69 | end | ||
| 70 | end | ||
| 71 | return sum | ||
| 72 | end | ||
| 73 | local result = f(1, 2, 3, 4, 5) | ||
| 74 | return assert.same(result, 15) | ||
| 75 | end) | ||
| 76 | it("should handle mixed types", function() | ||
| 77 | local f | ||
| 78 | f = function(...) | ||
| 79 | local args = { | ||
| 80 | n = select("#", ...), | ||
| 81 | ... | ||
| 82 | } | ||
| 83 | local types | ||
| 84 | do | ||
| 85 | local _accum_0 = { } | ||
| 86 | local _len_0 = 1 | ||
| 87 | for i = 1, args.n do | ||
| 88 | _accum_0[_len_0] = type(args[i]) | ||
| 89 | _len_0 = _len_0 + 1 | ||
| 90 | end | ||
| 91 | types = _accum_0 | ||
| 92 | end | ||
| 93 | return types | ||
| 94 | end | ||
| 95 | local result = f("string", 123, true, nil, { }) | ||
| 96 | return assert.same(result, { | ||
| 97 | "string", | ||
| 98 | "number", | ||
| 99 | "boolean", | ||
| 100 | "nil", | ||
| 101 | "table" | ||
| 102 | }) | ||
| 103 | end) | ||
| 104 | it("should work with table access", function() | ||
| 105 | local f | ||
| 106 | f = function(...) | ||
| 107 | local t = { | ||
| 108 | n = select("#", ...), | ||
| 109 | ... | ||
| 110 | } | ||
| 111 | local first = t[1] | ||
| 112 | local last = t[t.n] | ||
| 113 | return { | ||
| 114 | first, | ||
| 115 | last | ||
| 116 | } | ||
| 117 | end | ||
| 118 | local result = f(1, 2, 3, 4, 5) | ||
| 119 | return assert.same(result, { | ||
| 120 | 1, | ||
| 121 | 5 | ||
| 122 | }) | ||
| 123 | end) | ||
| 124 | it("should support select with named args", function() | ||
| 125 | local f | ||
| 126 | f = function(...) | ||
| 127 | local args = { | ||
| 128 | n = select("#", ...), | ||
| 129 | ... | ||
| 130 | } | ||
| 131 | local second = select(2, table.unpack(args)) | ||
| 132 | return second | ||
| 133 | end | ||
| 134 | local result = f("a", "b", "c") | ||
| 135 | return assert.same(result, "b") | ||
| 136 | end) | ||
| 137 | it("should work with pcall", function() | ||
| 138 | local f | ||
| 139 | f = function(...) | ||
| 140 | local t = { | ||
| 141 | n = select("#", ...), | ||
| 142 | ... | ||
| 143 | } | ||
| 144 | local success = true | ||
| 145 | for i = 1, t.n do | ||
| 146 | if t[i] == nil then | ||
| 147 | success = false | ||
| 148 | end | ||
| 149 | end | ||
| 150 | return success | ||
| 151 | end | ||
| 152 | local result = f(1, nil, 3) | ||
| 153 | return assert.is_false(result) | ||
| 154 | end) | ||
| 155 | it("should handle function results", function() | ||
| 156 | local g | ||
| 157 | g = function() | ||
| 158 | return 1, 2, 3 | ||
| 159 | end | ||
| 160 | local f | ||
| 161 | f = function(...) | ||
| 162 | local t = { | ||
| 163 | n = select("#", ...), | ||
| 164 | ... | ||
| 165 | } | ||
| 166 | return t.n | ||
| 167 | end | ||
| 168 | local result = f(g()) | ||
| 169 | return assert.same(result, 3) | ||
| 170 | end) | ||
| 171 | it("should work with unpacking", function() | ||
| 172 | local f | ||
| 173 | f = function(...) | ||
| 174 | local args = { | ||
| 175 | n = select("#", ...), | ||
| 176 | ... | ||
| 177 | } | ||
| 178 | return { | ||
| 179 | table.unpack(args) | ||
| 180 | } | ||
| 181 | end | ||
| 182 | local result = f("a", "b", "c") | ||
| 183 | return assert.same(result, { | ||
| 184 | "a", | ||
| 185 | "b", | ||
| 186 | "c" | ||
| 187 | }) | ||
| 188 | end) | ||
| 189 | it("should support passing named varargs to another function", function() | ||
| 190 | local outer | ||
| 191 | outer = function(...) | ||
| 192 | local t = { | ||
| 193 | n = select("#", ...), | ||
| 194 | ... | ||
| 195 | } | ||
| 196 | return inner((table.unpack(t))) | ||
| 197 | end | ||
| 198 | local inner | ||
| 199 | inner = function(a, b, c) | ||
| 200 | return { | ||
| 201 | a, | ||
| 202 | b, | ||
| 203 | c | ||
| 204 | } | ||
| 205 | end | ||
| 206 | local result = outer(1, 2, 3) | ||
| 207 | return assert.same(result, { | ||
| 208 | 1, | ||
| 209 | 2, | ||
| 210 | 3 | ||
| 211 | }) | ||
| 212 | end) | ||
| 213 | it("should work with default parameter", function() | ||
| 214 | local f | ||
| 215 | f = function(x, ...) | ||
| 216 | if x == nil then | ||
| 217 | x = 10 | ||
| 218 | end | ||
| 219 | local t = { | ||
| 220 | n = select("#", ...), | ||
| 221 | ... | ||
| 222 | } | ||
| 223 | return x + t[1] or 0 | ||
| 224 | end | ||
| 225 | local result = f(5, 15) | ||
| 226 | return assert.same(result, 20) | ||
| 227 | end) | ||
| 228 | return it("should handle single argument", function() | ||
| 229 | local f | ||
| 230 | f = function(...) | ||
| 231 | local t = { | ||
| 232 | n = select("#", ...), | ||
| 233 | ... | ||
| 234 | } | ||
| 235 | return { | ||
| 236 | t.n, | ||
| 237 | t[1] | ||
| 238 | } | ||
| 239 | end | ||
| 240 | local result = f(42) | ||
| 241 | return assert.same(result, { | ||
| 242 | 1, | ||
| 243 | 42 | ||
| 244 | }) | ||
| 245 | end) | ||
| 246 | end) | ||
diff --git a/spec/outputs/test/prefixed_return_spec.lua b/spec/outputs/test/prefixed_return_spec.lua new file mode 100644 index 0000000..4a73d81 --- /dev/null +++ b/spec/outputs/test/prefixed_return_spec.lua | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | return describe("prefixed return", function() | ||
| 2 | it("should return prefixed value with no explicit return", function() | ||
| 3 | local findFirstEven | ||
| 4 | findFirstEven = function(list) | ||
| 5 | for _index_0 = 1, #list do | ||
| 6 | local item = list[_index_0] | ||
| 7 | if type(item) == "table" then | ||
| 8 | for _index_1 = 1, #item do | ||
| 9 | local sub = item[_index_1] | ||
| 10 | if sub % 2 == 0 then | ||
| 11 | return sub | ||
| 12 | end | ||
| 13 | end | ||
| 14 | end | ||
| 15 | end | ||
| 16 | return nil | ||
| 17 | end | ||
| 18 | local result = findFirstEven({ | ||
| 19 | 1, | ||
| 20 | 3, | ||
| 21 | { | ||
| 22 | 4, | ||
| 23 | 6 | ||
| 24 | }, | ||
| 25 | 5 | ||
| 26 | }) | ||
| 27 | return assert.same(result, 4) | ||
| 28 | end) | ||
| 29 | it("should return prefixed nil when not found", function() | ||
| 30 | local findValue | ||
| 31 | findValue = function(list) | ||
| 32 | for _index_0 = 1, #list do | ||
| 33 | local item = list[_index_0] | ||
| 34 | if item == 999 then | ||
| 35 | return item | ||
| 36 | end | ||
| 37 | end | ||
| 38 | return nil | ||
| 39 | end | ||
| 40 | local result = findValue({ | ||
| 41 | 1, | ||
| 42 | 2, | ||
| 43 | 3 | ||
| 44 | }) | ||
| 45 | return assert.same(result, nil) | ||
| 46 | end) | ||
| 47 | it("should return prefixed string", function() | ||
| 48 | local findName | ||
| 49 | findName = function(items) | ||
| 50 | for _index_0 = 1, #items do | ||
| 51 | local item = items[_index_0] | ||
| 52 | if item.name == "target" then | ||
| 53 | return item.name | ||
| 54 | end | ||
| 55 | end | ||
| 56 | return "not found" | ||
| 57 | end | ||
| 58 | local result = findName({ | ||
| 59 | { | ||
| 60 | name = "a" | ||
| 61 | }, | ||
| 62 | { | ||
| 63 | name = "b" | ||
| 64 | } | ||
| 65 | }) | ||
| 66 | return assert.same(result, "not found") | ||
| 67 | end) | ||
| 68 | it("should return prefixed number", function() | ||
| 69 | local calculateSum | ||
| 70 | calculateSum = function() | ||
| 71 | local total = 0 | ||
| 72 | return 0 | ||
| 73 | end | ||
| 74 | local result = calculateSum() | ||
| 75 | return assert.same(result, 0) | ||
| 76 | end) | ||
| 77 | return it("should work with nested logic", function() | ||
| 78 | local findNested | ||
| 79 | findNested = function(data) | ||
| 80 | for _index_0 = 1, #data do | ||
| 81 | local category = data[_index_0] | ||
| 82 | if type(category) == "table" then | ||
| 83 | for _index_1 = 1, #category do | ||
| 84 | local item = category[_index_1] | ||
| 85 | if item == "target" then | ||
| 86 | return "found" | ||
| 87 | end | ||
| 88 | end | ||
| 89 | end | ||
| 90 | end | ||
| 91 | return "missing" | ||
| 92 | end | ||
| 93 | local result = findNested({ | ||
| 94 | { | ||
| 95 | 1, | ||
| 96 | 2 | ||
| 97 | }, | ||
| 98 | { | ||
| 99 | "target", | ||
| 100 | 3 | ||
| 101 | } | ||
| 102 | }) | ||
| 103 | return assert.same(result, "found") | ||
| 104 | end) | ||
| 105 | end) | ||
diff --git a/spec/outputs/test/reverse_index_spec.lua b/spec/outputs/test/reverse_index_spec.lua new file mode 100644 index 0000000..396c3b9 --- /dev/null +++ b/spec/outputs/test/reverse_index_spec.lua | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | return describe("reverse index", function() | ||
| 2 | it("should get last element", function() | ||
| 3 | local data = { | ||
| 4 | items = { | ||
| 5 | 1, | ||
| 6 | 2, | ||
| 7 | 3, | ||
| 8 | 4, | ||
| 9 | 5 | ||
| 10 | } | ||
| 11 | } | ||
| 12 | local last | ||
| 13 | do | ||
| 14 | local _item_0 = data.items | ||
| 15 | last = _item_0[#_item_0] | ||
| 16 | end | ||
| 17 | return assert.same(last, 5) | ||
| 18 | end) | ||
| 19 | it("should get second last element", function() | ||
| 20 | local data = { | ||
| 21 | items = { | ||
| 22 | 1, | ||
| 23 | 2, | ||
| 24 | 3, | ||
| 25 | 4, | ||
| 26 | 5 | ||
| 27 | } | ||
| 28 | } | ||
| 29 | local second_last | ||
| 30 | do | ||
| 31 | local _item_0 = data.items | ||
| 32 | second_last = _item_0[#_item_0 - 1] | ||
| 33 | end | ||
| 34 | return assert.same(second_last, 4) | ||
| 35 | end) | ||
| 36 | it("should get third last element", function() | ||
| 37 | local data = { | ||
| 38 | items = { | ||
| 39 | 1, | ||
| 40 | 2, | ||
| 41 | 3, | ||
| 42 | 4, | ||
| 43 | 5 | ||
| 44 | } | ||
| 45 | } | ||
| 46 | local third_last | ||
| 47 | do | ||
| 48 | local _item_0 = data.items | ||
| 49 | third_last = _item_0[#_item_0 - 2] | ||
| 50 | end | ||
| 51 | return assert.same(third_last, 3) | ||
| 52 | end) | ||
| 53 | it("should set last element", function() | ||
| 54 | local data = { | ||
| 55 | items = { | ||
| 56 | 1, | ||
| 57 | 2, | ||
| 58 | 3, | ||
| 59 | 4, | ||
| 60 | 5 | ||
| 61 | } | ||
| 62 | } | ||
| 63 | local _obj_0 = data.items | ||
| 64 | _obj_0[#_obj_0] = 10 | ||
| 65 | return assert.same(data.items[5], 10) | ||
| 66 | end) | ||
| 67 | it("should set second last element", function() | ||
| 68 | local data = { | ||
| 69 | items = { | ||
| 70 | 1, | ||
| 71 | 2, | ||
| 72 | 3, | ||
| 73 | 4, | ||
| 74 | 5 | ||
| 75 | } | ||
| 76 | } | ||
| 77 | local _obj_0 = data.items | ||
| 78 | _obj_0[#_obj_0 - 1] = 20 | ||
| 79 | return assert.same(data.items[4], 20) | ||
| 80 | end) | ||
| 81 | it("should work with single element", function() | ||
| 82 | local tab = { | ||
| 83 | 42 | ||
| 84 | } | ||
| 85 | return assert.same(tab[#tab], 42) | ||
| 86 | end) | ||
| 87 | it("should work with empty table", function() | ||
| 88 | local tab = { } | ||
| 89 | return assert.same(tab[#tab], nil) | ||
| 90 | end) | ||
| 91 | it("should work in expressions", function() | ||
| 92 | local tab = { | ||
| 93 | 1, | ||
| 94 | 2, | ||
| 95 | 3, | ||
| 96 | 4, | ||
| 97 | 5 | ||
| 98 | } | ||
| 99 | local result = tab[#tab] + tab[#tab - 1] | ||
| 100 | return assert.same(result, 9) | ||
| 101 | end) | ||
| 102 | it("should support chaining", function() | ||
| 103 | local data = { | ||
| 104 | items = { | ||
| 105 | nested = { | ||
| 106 | 1, | ||
| 107 | 2, | ||
| 108 | 3 | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | local last | ||
| 113 | do | ||
| 114 | local _item_0 = data.items.nested | ||
| 115 | last = _item_0[#_item_0] | ||
| 116 | end | ||
| 117 | return assert.same(last, 3) | ||
| 118 | end) | ||
| 119 | it("should work with string", function() | ||
| 120 | local s = "hello" | ||
| 121 | return assert.same(s[#s], "o") | ||
| 122 | end) | ||
| 123 | it("should handle negative offsets", function() | ||
| 124 | local tab = { | ||
| 125 | 1, | ||
| 126 | 2, | ||
| 127 | 3, | ||
| 128 | 4, | ||
| 129 | 5 | ||
| 130 | } | ||
| 131 | assert.same(tab[#tab - 3], 2) | ||
| 132 | return assert.same(tab[#tab - 4], 1) | ||
| 133 | end) | ||
| 134 | return it("should work in loops", function() | ||
| 135 | local tab = { | ||
| 136 | 1, | ||
| 137 | 2, | ||
| 138 | 3, | ||
| 139 | 4, | ||
| 140 | 5 | ||
| 141 | } | ||
| 142 | local results = { } | ||
| 143 | for i = 0, 2 do | ||
| 144 | table.insert(results, tab[#tab - i]) | ||
| 145 | end | ||
| 146 | return assert.same(results, { | ||
| 147 | 5, | ||
| 148 | 4, | ||
| 149 | 3 | ||
| 150 | }) | ||
| 151 | end) | ||
| 152 | end) | ||
diff --git a/spec/outputs/test/table_append_spec.lua b/spec/outputs/test/table_append_spec.lua new file mode 100644 index 0000000..5ce1156 --- /dev/null +++ b/spec/outputs/test/table_append_spec.lua | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | return describe("table append", function() | ||
| 2 | it("should append single value", function() | ||
| 3 | local tab = { } | ||
| 4 | tab[#tab + 1] = "Value" | ||
| 5 | assert.same(tab[1], "Value") | ||
| 6 | return assert.same(#tab, 1) | ||
| 7 | end) | ||
| 8 | it("should append multiple values", function() | ||
| 9 | local tab = { } | ||
| 10 | tab[#tab + 1] = 1 | ||
| 11 | tab[#tab + 1] = 2 | ||
| 12 | tab[#tab + 1] = 3 | ||
| 13 | return assert.same(tab, { | ||
| 14 | 1, | ||
| 15 | 2, | ||
| 16 | 3 | ||
| 17 | }) | ||
| 18 | end) | ||
| 19 | it("should append with spread operator", function() | ||
| 20 | local tbA = { | ||
| 21 | 1, | ||
| 22 | 2, | ||
| 23 | 3 | ||
| 24 | } | ||
| 25 | local tbB = { | ||
| 26 | 4, | ||
| 27 | 5, | ||
| 28 | 6 | ||
| 29 | } | ||
| 30 | local _len_0 = #tbA + 1 | ||
| 31 | for _index_0 = 1, #tbB do | ||
| 32 | local _elm_0 = tbB[_index_0] | ||
| 33 | tbA[_len_0], _len_0 = _elm_0, _len_0 + 1 | ||
| 34 | end | ||
| 35 | return assert.same(tbA, { | ||
| 36 | 1, | ||
| 37 | 2, | ||
| 38 | 3, | ||
| 39 | 4, | ||
| 40 | 5, | ||
| 41 | 6 | ||
| 42 | }) | ||
| 43 | end) | ||
| 44 | it("should append table with single element", function() | ||
| 45 | local tab = { | ||
| 46 | 1, | ||
| 47 | 2 | ||
| 48 | } | ||
| 49 | local tb2 = { | ||
| 50 | 3 | ||
| 51 | } | ||
| 52 | tab[#tab + 1] = table.unpack(tb2) | ||
| 53 | return assert.same(tab, { | ||
| 54 | 1, | ||
| 55 | 2, | ||
| 56 | 3 | ||
| 57 | }) | ||
| 58 | end) | ||
| 59 | it("should append empty table", function() | ||
| 60 | local tab = { | ||
| 61 | 1, | ||
| 62 | 2 | ||
| 63 | } | ||
| 64 | local tb2 = { } | ||
| 65 | local _len_0 = #tab + 1 | ||
| 66 | for _index_0 = 1, #tb2 do | ||
| 67 | local _elm_0 = tb2[_index_0] | ||
| 68 | tab[_len_0], _len_0 = _elm_0, _len_0 + 1 | ||
| 69 | end | ||
| 70 | return assert.same(tab, { | ||
| 71 | 1, | ||
| 72 | 2 | ||
| 73 | }) | ||
| 74 | end) | ||
| 75 | it("should append nil values", function() | ||
| 76 | local tab = { } | ||
| 77 | tab[#tab + 1] = nil | ||
| 78 | tab[#tab + 1] = "value" | ||
| 79 | assert.same(tab[1], nil) | ||
| 80 | return assert.same(tab[2], "value") | ||
| 81 | end) | ||
| 82 | it("should work in loop", function() | ||
| 83 | local tab = { } | ||
| 84 | for i = 1, 3 do | ||
| 85 | tab[#tab + 1] = i * 2 | ||
| 86 | end | ||
| 87 | return assert.same(tab, { | ||
| 88 | 2, | ||
| 89 | 4, | ||
| 90 | 6 | ||
| 91 | }) | ||
| 92 | end) | ||
| 93 | it("should append with expressions", function() | ||
| 94 | local tab = { } | ||
| 95 | local x = 10 | ||
| 96 | tab[#tab + 1] = x + 5 | ||
| 97 | return assert.same(tab[1], 15) | ||
| 98 | end) | ||
| 99 | it("should append mixed types", function() | ||
| 100 | local tab = { } | ||
| 101 | tab[#tab + 1] = "string" | ||
| 102 | tab[#tab + 1] = 123 | ||
| 103 | tab[#tab + 1] = true | ||
| 104 | tab[#tab + 1] = nil | ||
| 105 | return assert.same(tab, { | ||
| 106 | "string", | ||
| 107 | 123, | ||
| 108 | true, | ||
| 109 | nil | ||
| 110 | }) | ||
| 111 | end) | ||
| 112 | it("should append to table with existing elements", function() | ||
| 113 | local tab = { | ||
| 114 | 1, | ||
| 115 | 2, | ||
| 116 | 3 | ||
| 117 | } | ||
| 118 | tab[#tab + 1] = 4 | ||
| 119 | tab[#tab + 1] = 5 | ||
| 120 | return assert.same(tab, { | ||
| 121 | 1, | ||
| 122 | 2, | ||
| 123 | 3, | ||
| 124 | 4, | ||
| 125 | 5 | ||
| 126 | }) | ||
| 127 | end) | ||
| 128 | it("should work with nested tables", function() | ||
| 129 | local tab = { } | ||
| 130 | tab[#tab + 1] = { | ||
| 131 | a = 1, | ||
| 132 | b = 2 | ||
| 133 | } | ||
| 134 | tab[#tab + 1] = { | ||
| 135 | 3, | ||
| 136 | 4 | ||
| 137 | } | ||
| 138 | assert.same(tab[1], { | ||
| 139 | a = 1, | ||
| 140 | b = 2 | ||
| 141 | }) | ||
| 142 | return assert.same(tab[2], { | ||
| 143 | 3, | ||
| 144 | 4 | ||
| 145 | }) | ||
| 146 | end) | ||
| 147 | return it("should append function results", function() | ||
| 148 | local fn | ||
| 149 | fn = function() | ||
| 150 | return 1, 2, 3 | ||
| 151 | end | ||
| 152 | local tab = { } | ||
| 153 | tab[#tab + 1] = fn() | ||
| 154 | return assert.same(tab, { | ||
| 155 | 1, | ||
| 156 | 2, | ||
| 157 | 3 | ||
| 158 | }) | ||
| 159 | end) | ||
| 160 | end) | ||
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) | ||
diff --git a/spec/outputs/test/while_assignment_spec.lua b/spec/outputs/test/while_assignment_spec.lua new file mode 100644 index 0000000..289e16e --- /dev/null +++ b/spec/outputs/test/while_assignment_spec.lua | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | return describe("while assignment", function() | ||
| 2 | it("should loop while value is truthy", function() | ||
| 3 | local counter = 0 | ||
| 4 | local get_next | ||
| 5 | get_next = function() | ||
| 6 | if counter < 3 then | ||
| 7 | counter = counter + 1 | ||
| 8 | return counter | ||
| 9 | else | ||
| 10 | return nil | ||
| 11 | end | ||
| 12 | end | ||
| 13 | local results = { } | ||
| 14 | repeat | ||
| 15 | local val = get_next() | ||
| 16 | if val then | ||
| 17 | table.insert(results, val) | ||
| 18 | else | ||
| 19 | break | ||
| 20 | end | ||
| 21 | until false | ||
| 22 | return assert.same(results, { | ||
| 23 | 1, | ||
| 24 | 2, | ||
| 25 | 3 | ||
| 26 | }) | ||
| 27 | end) | ||
| 28 | it("should work with function results", function() | ||
| 29 | local counter = 0 | ||
| 30 | local fn | ||
| 31 | fn = function() | ||
| 32 | counter = counter + 1 | ||
| 33 | if counter <= 3 then | ||
| 34 | return counter * 10 | ||
| 35 | else | ||
| 36 | return nil | ||
| 37 | end | ||
| 38 | end | ||
| 39 | local sum = 0 | ||
| 40 | repeat | ||
| 41 | local val = fn() | ||
| 42 | if val then | ||
| 43 | sum = sum + val | ||
| 44 | else | ||
| 45 | break | ||
| 46 | end | ||
| 47 | until false | ||
| 48 | return assert.same(sum, 60) | ||
| 49 | end) | ||
| 50 | it("should exit immediately on nil", function() | ||
| 51 | local get_val | ||
| 52 | get_val = function() | ||
| 53 | return nil | ||
| 54 | end | ||
| 55 | local counter = 0 | ||
| 56 | repeat | ||
| 57 | local val = get_val() | ||
| 58 | if val then | ||
| 59 | counter = counter + 1 | ||
| 60 | else | ||
| 61 | break | ||
| 62 | end | ||
| 63 | until false | ||
| 64 | return assert.same(counter, 0) | ||
| 65 | end) | ||
| 66 | return it("should support break in loop", function() | ||
| 67 | local items = { | ||
| 68 | 1, | ||
| 69 | 2, | ||
| 70 | 3, | ||
| 71 | 4, | ||
| 72 | 5 | ||
| 73 | } | ||
| 74 | local sum = 0 | ||
| 75 | for _index_0 = 1, #items do | ||
| 76 | local item = items[_index_0] | ||
| 77 | sum = sum + item | ||
| 78 | if sum > 6 then | ||
| 79 | break | ||
| 80 | end | ||
| 81 | end | ||
| 82 | return assert.same(sum, 10) | ||
| 83 | end) | ||
| 84 | end) | ||
diff --git a/spec/outputs/test/yaml_string_spec.lua b/spec/outputs/test/yaml_string_spec.lua new file mode 100644 index 0000000..258ab92 --- /dev/null +++ b/spec/outputs/test/yaml_string_spec.lua | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | return describe("yaml string", function() | ||
| 2 | it("should create basic yaml string", function() | ||
| 3 | local s = "hello\nworld" | ||
| 4 | assert.is_true(s:match("hello")) | ||
| 5 | return assert.is_true(s:match("world")) | ||
| 6 | end) | ||
| 7 | it("should preserve indentation", function() | ||
| 8 | local s = "key1: value1\nkey2: value2" | ||
| 9 | assert.is_true(s:match("key1")) | ||
| 10 | return assert.is_true(s:match("key2")) | ||
| 11 | end) | ||
| 12 | it("should support interpolation", function() | ||
| 13 | local name = "test" | ||
| 14 | local s = "hello " .. tostring(name) | ||
| 15 | return assert.same(s, "hello test") | ||
| 16 | end) | ||
| 17 | it("should handle complex interpolation", function() | ||
| 18 | local x, y = 10, 20 | ||
| 19 | local s = "point:\n\tx: " .. tostring(x) .. "\n\ty: " .. tostring(y) | ||
| 20 | assert.is_true(s:match("x: 10")) | ||
| 21 | return assert.is_true(s:match("y: 20")) | ||
| 22 | end) | ||
| 23 | it("should work with expressions", function() | ||
| 24 | local s = "result: " .. tostring(1 + 2) | ||
| 25 | return assert.is_true(s:match("result: 3")) | ||
| 26 | end) | ||
| 27 | it("should support multiline with variables", function() | ||
| 28 | local config = "database:\n\thost: localhost\n\tport: 5432\n\tname: mydb" | ||
| 29 | assert.is_true(config:match("database:")) | ||
| 30 | return assert.is_true(config:match("host:")) | ||
| 31 | end) | ||
| 32 | it("should escape special characters", function() | ||
| 33 | local s = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'" | ||
| 34 | assert.is_true(s:match("path:")) | ||
| 35 | return assert.is_true(s:match("note:")) | ||
| 36 | end) | ||
| 37 | it("should work in function", function() | ||
| 38 | local fn | ||
| 39 | fn = function() | ||
| 40 | local str = "foo:\n\tbar: baz" | ||
| 41 | return str | ||
| 42 | end | ||
| 43 | local result = fn() | ||
| 44 | assert.is_true(result:match("foo:")) | ||
| 45 | return assert.is_true(result:match("bar:")) | ||
| 46 | end) | ||
| 47 | it("should strip common leading whitespace", function() | ||
| 48 | local fn | ||
| 49 | fn = function() | ||
| 50 | local s = "nested:\n\titem: value" | ||
| 51 | return s | ||
| 52 | end | ||
| 53 | local result = fn() | ||
| 54 | assert.is_true(result:match("nested:")) | ||
| 55 | return assert.is_true(result:match("item:")) | ||
| 56 | end) | ||
| 57 | it("should support empty lines", function() | ||
| 58 | local s = "line1\nline3" | ||
| 59 | assert.is_true(s:match("line1")) | ||
| 60 | return assert.is_true(s:match("line3")) | ||
| 61 | end) | ||
| 62 | it("should work with table access in interpolation", function() | ||
| 63 | local t = { | ||
| 64 | value = 100 | ||
| 65 | } | ||
| 66 | local s = "value: " .. tostring(t.value) | ||
| 67 | return assert.is_true(s:match("value: 100")) | ||
| 68 | end) | ||
| 69 | it("should support function calls in interpolation", function() | ||
| 70 | local s = "result: " .. tostring((function() | ||
| 71 | return 42 | ||
| 72 | end)()) | ||
| 73 | return assert.is_true(s:match("result: 42")) | ||
| 74 | end) | ||
| 75 | it("should handle quotes correctly", function() | ||
| 76 | local s = "\"quoted\"\n'single quoted'" | ||
| 77 | assert.is_true(s:match('"quoted"')) | ||
| 78 | return assert.is_true(s:match("'single quoted'")) | ||
| 79 | end) | ||
| 80 | it("should work with multiple interpolations", function() | ||
| 81 | local a, b, c = 1, 2, 3 | ||
| 82 | local s = "values: " .. tostring(a) .. ", " .. tostring(b) .. ", " .. tostring(c) | ||
| 83 | return assert.is_true(s:match("values: 1, 2, 3")) | ||
| 84 | end) | ||
| 85 | return it("should preserve newlines", function() | ||
| 86 | local s = "first line\nsecond line\nthird line" | ||
| 87 | local lines | ||
| 88 | do | ||
| 89 | local _accum_0 = { } | ||
| 90 | local _len_0 = 1 | ||
| 91 | for line in s:gmatch("[^\n]+") do | ||
| 92 | _accum_0[_len_0] = line | ||
| 93 | _len_0 = _len_0 + 1 | ||
| 94 | end | ||
| 95 | lines = _accum_0 | ||
| 96 | end | ||
| 97 | return assert.same(#lines, 3) | ||
| 98 | end) | ||
| 99 | end) | ||
