diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-28 18:43:14 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-28 18:43:14 +0800 |
| commit | dd64edd58fe25ec74ae5958128cf3f74b0692f3b (patch) | |
| tree | 0f2de1df55897e2713977c5a53936757e14b477a /spec/outputs/test/operator_advanced_spec.lua | |
| parent | 7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (diff) | |
| download | yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.gz yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.bz2 yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.zip | |
Fixed compiler issues and added 800+ test cases.
Diffstat (limited to 'spec/outputs/test/operator_advanced_spec.lua')
| -rw-r--r-- | spec/outputs/test/operator_advanced_spec.lua | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/spec/outputs/test/operator_advanced_spec.lua b/spec/outputs/test/operator_advanced_spec.lua new file mode 100644 index 0000000..42e4c30 --- /dev/null +++ b/spec/outputs/test/operator_advanced_spec.lua | |||
| @@ -0,0 +1,210 @@ | |||
| 1 | local _anon_func_0 = function(v) | ||
| 2 | local _cond_0 = v(2) | ||
| 3 | if not (v(1) < _cond_0) then | ||
| 4 | return false | ||
| 5 | else | ||
| 6 | return _cond_0 <= v(3) | ||
| 7 | end | ||
| 8 | end | ||
| 9 | return describe("advanced operators", function() | ||
| 10 | it("should support chaining comparisons with functions", function() | ||
| 11 | local v | ||
| 12 | v = function(x) | ||
| 13 | return x | ||
| 14 | end | ||
| 15 | return assert.is_true(_anon_func_0(v)) | ||
| 16 | end) | ||
| 17 | it("should handle compound assignment with or", function() | ||
| 18 | local x = nil | ||
| 19 | x = x or "default" | ||
| 20 | return assert.same(x, "default") | ||
| 21 | end) | ||
| 22 | it("should not overwrite existing value with or", function() | ||
| 23 | local x = "existing" | ||
| 24 | x = x or "default" | ||
| 25 | return assert.same(x, "existing") | ||
| 26 | end) | ||
| 27 | it("should support compound string concatenation", function() | ||
| 28 | local s = "hello" | ||
| 29 | s = s .. " world" | ||
| 30 | return assert.same(s, "hello world") | ||
| 31 | end) | ||
| 32 | it("should work with table appending", function() | ||
| 33 | local tab = { | ||
| 34 | 1, | ||
| 35 | 2 | ||
| 36 | } | ||
| 37 | tab[#tab + 1] = 3 | ||
| 38 | tab[#tab + 1] = 4 | ||
| 39 | return assert.same(tab, { | ||
| 40 | 1, | ||
| 41 | 2, | ||
| 42 | 3, | ||
| 43 | 4 | ||
| 44 | }) | ||
| 45 | end) | ||
| 46 | it("should handle spread append", function() | ||
| 47 | local tbA = { | ||
| 48 | 1, | ||
| 49 | 2 | ||
| 50 | } | ||
| 51 | local tbB = { | ||
| 52 | 3, | ||
| 53 | 4 | ||
| 54 | } | ||
| 55 | local _len_0 = #tbA + 1 | ||
| 56 | for _index_0 = 1, #tbB do | ||
| 57 | local _elm_0 = tbB[_index_0] | ||
| 58 | tbA[_len_0], _len_0 = _elm_0, _len_0 + 1 | ||
| 59 | end | ||
| 60 | return assert.same(tbA, { | ||
| 61 | 1, | ||
| 62 | 2, | ||
| 63 | 3, | ||
| 64 | 4 | ||
| 65 | }) | ||
| 66 | end) | ||
| 67 | it("should support reverse indexing", function() | ||
| 68 | local items = { | ||
| 69 | 1, | ||
| 70 | 2, | ||
| 71 | 3, | ||
| 72 | 4, | ||
| 73 | 5 | ||
| 74 | } | ||
| 75 | assert.same(items[#items], 5) | ||
| 76 | assert.same(items[#items - 1], 4) | ||
| 77 | return assert.same(items[#items - 2], 3) | ||
| 78 | end) | ||
| 79 | it("should work with nil coalescing assignment", function() | ||
| 80 | local x = nil | ||
| 81 | if x == nil then | ||
| 82 | x = "default" | ||
| 83 | end | ||
| 84 | return assert.same(x, "default") | ||
| 85 | end) | ||
| 86 | it("should not assign with ??= when value exists", function() | ||
| 87 | local x = "existing" | ||
| 88 | if x == nil then | ||
| 89 | x = "default" | ||
| 90 | end | ||
| 91 | return assert.same(x, "existing") | ||
| 92 | end) | ||
| 93 | it("should chain nil coalescing", function() | ||
| 94 | local a = nil | ||
| 95 | local b = nil | ||
| 96 | local c = "value" | ||
| 97 | local result | ||
| 98 | if a ~= nil then | ||
| 99 | result = a | ||
| 100 | else | ||
| 101 | if b ~= nil then | ||
| 102 | result = b | ||
| 103 | else | ||
| 104 | result = c | ||
| 105 | end | ||
| 106 | end | ||
| 107 | return assert.same(result, "value") | ||
| 108 | end) | ||
| 109 | it("should support compound modulo", function() | ||
| 110 | local x = 20 | ||
| 111 | x = x % 3 | ||
| 112 | return assert.same(x, 2) | ||
| 113 | end) | ||
| 114 | it("should handle compound exponentiation", function() | ||
| 115 | local x = 2 | ||
| 116 | x = x ^ 3 | ||
| 117 | return assert.same(x, 8) | ||
| 118 | end) | ||
| 119 | it("should work with compound bitwise and", function() | ||
| 120 | local x = 15 | ||
| 121 | x = x & 7 | ||
| 122 | return assert.same(x, 7) | ||
| 123 | end) | ||
| 124 | it("should support compound bitwise or", function() | ||
| 125 | local x = 8 | ||
| 126 | x = x | 3 | ||
| 127 | return assert.same(x, 11) | ||
| 128 | end) | ||
| 129 | it("should handle compound bitwise xor", function() | ||
| 130 | local x = 12 | ||
| 131 | x = x ~ 10 | ||
| 132 | return assert.same(x, 6) | ||
| 133 | end) | ||
| 134 | it("should work with compound left shift", function() | ||
| 135 | local x = 2 | ||
| 136 | x = x << 3 | ||
| 137 | return assert.same(x, 16) | ||
| 138 | end) | ||
| 139 | it("should support compound right shift", function() | ||
| 140 | local x = 16 | ||
| 141 | x = x >> 2 | ||
| 142 | return assert.same(x, 4) | ||
| 143 | end) | ||
| 144 | it("should handle negation operator", function() | ||
| 145 | assert.same(-10, -10) | ||
| 146 | return assert.same | ||
| 147 | end) | ||
| 148 | it("should work with length operator on tables", function() | ||
| 149 | local tab = { | ||
| 150 | 1, | ||
| 151 | 2, | ||
| 152 | 3, | ||
| 153 | 4, | ||
| 154 | 5 | ||
| 155 | } | ||
| 156 | return assert.same(#tab, 5) | ||
| 157 | end) | ||
| 158 | it("should support length on strings", function() | ||
| 159 | local s = "hello" | ||
| 160 | return assert.same(#s, 5) | ||
| 161 | end) | ||
| 162 | it("should handle chaining assignment", function() | ||
| 163 | local a = 0 | ||
| 164 | local b = 0 | ||
| 165 | local c = 0 | ||
| 166 | local d = 0 | ||
| 167 | assert.same(a, 0) | ||
| 168 | assert.same(b, 0) | ||
| 169 | assert.same(c, 0) | ||
| 170 | return assert.same(d, 0) | ||
| 171 | end) | ||
| 172 | it("should work with chaining assignment with functions", function() | ||
| 173 | local f | ||
| 174 | f = function() | ||
| 175 | return 42 | ||
| 176 | end | ||
| 177 | local x = f() | ||
| 178 | local y = x | ||
| 179 | local z = x | ||
| 180 | assert.same(x, 42) | ||
| 181 | assert.same(y, 42) | ||
| 182 | return assert.same(z, 42) | ||
| 183 | end) | ||
| 184 | it("should support != as alias for ~=", function() | ||
| 185 | assert.is_true(1 ~= 2) | ||
| 186 | return assert.is_false(1 ~= 1) | ||
| 187 | end) | ||
| 188 | it("should work with :: for method chaining", function() | ||
| 189 | local obj = { | ||
| 190 | value = 10, | ||
| 191 | add = function(self, n) | ||
| 192 | self.value = self.value + n | ||
| 193 | return self | ||
| 194 | end, | ||
| 195 | get = function(self) | ||
| 196 | return self.value | ||
| 197 | end | ||
| 198 | } | ||
| 199 | local result = obj:add(5):get() | ||
| 200 | return assert.same(result, 15) | ||
| 201 | end) | ||
| 202 | it("should handle complex expressions with precedence", function() | ||
| 203 | local result = 1 + 2 * 3 - 4 / 2 | ||
| 204 | return assert.same(result, 5) | ||
| 205 | end) | ||
| 206 | return it("should support mixed operator types", function() | ||
| 207 | local result = 10 + 20 * 2 - 5 / 5 | ||
| 208 | return assert.same(result, 49) | ||
| 209 | end) | ||
| 210 | end) | ||
