diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-26 17:45:26 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-26 17:45:56 +0800 |
| commit | e02321107277a63e7dcb12ab163c9942ac101b87 (patch) | |
| tree | f38c6f2fbf4ee35df4938933dbc1e645733356f4 /spec/outputs/test/operators_spec.lua | |
| parent | 5d5b657f606b5939062983b1f90c3359d542672e (diff) | |
| download | yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2 yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip | |
Updated tests.
Diffstat (limited to 'spec/outputs/test/operators_spec.lua')
| -rw-r--r-- | spec/outputs/test/operators_spec.lua | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/spec/outputs/test/operators_spec.lua b/spec/outputs/test/operators_spec.lua new file mode 100644 index 0000000..a17ff03 --- /dev/null +++ b/spec/outputs/test/operators_spec.lua | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | return describe("operators", function() | ||
| 2 | it("should support addition", function() | ||
| 3 | return assert.same(1 + 2, 3) | ||
| 4 | end) | ||
| 5 | it("should support subtraction", function() | ||
| 6 | return assert.same(5 - 3, 2) | ||
| 7 | end) | ||
| 8 | it("should support multiplication", function() | ||
| 9 | return assert.same(4 * 3, 12) | ||
| 10 | end) | ||
| 11 | it("should support division", function() | ||
| 12 | return assert.same(10 / 2, 5) | ||
| 13 | end) | ||
| 14 | it("should support modulo", function() | ||
| 15 | return assert.same(10 % 3, 1) | ||
| 16 | end) | ||
| 17 | it("should support exponentiation", function() | ||
| 18 | return assert.same(2 ^ 3, 8) | ||
| 19 | end) | ||
| 20 | it("should support unary minus", function() | ||
| 21 | return assert.same(-5, -5) | ||
| 22 | end) | ||
| 23 | it("should support equality comparison", function() | ||
| 24 | assert.is_true(1 == 1) | ||
| 25 | return assert.is_false(1 == 2) | ||
| 26 | end) | ||
| 27 | it("should support inequality comparison", function() | ||
| 28 | assert.is_true(1 ~= 2) | ||
| 29 | return assert.is_false(1 ~= 1) | ||
| 30 | end) | ||
| 31 | it("should support less than", function() | ||
| 32 | assert.is_true(1 < 2) | ||
| 33 | return assert.is_false(2 < 1) | ||
| 34 | end) | ||
| 35 | it("should support greater than", function() | ||
| 36 | assert.is_true(2 > 1) | ||
| 37 | return assert.is_false(1 > 2) | ||
| 38 | end) | ||
| 39 | it("should support less than or equal", function() | ||
| 40 | assert.is_true(1 <= 2) | ||
| 41 | assert.is_true(2 <= 2) | ||
| 42 | return assert.is_false(3 <= 2) | ||
| 43 | end) | ||
| 44 | it("should support greater than or equal", function() | ||
| 45 | assert.is_true(2 >= 1) | ||
| 46 | assert.is_true(2 >= 2) | ||
| 47 | return assert.is_false(1 >= 2) | ||
| 48 | end) | ||
| 49 | it("should support logical and", function() | ||
| 50 | assert.same(true and false, false) | ||
| 51 | assert.same(true and true, true) | ||
| 52 | return assert.same(false and true, false) | ||
| 53 | end) | ||
| 54 | it("should support logical or", function() | ||
| 55 | assert.same(true or false, true) | ||
| 56 | assert.same(false or true, true) | ||
| 57 | return assert.same(false or false, false) | ||
| 58 | end) | ||
| 59 | it("should support logical not", function() | ||
| 60 | assert.same(not true, false) | ||
| 61 | assert.same(not false, true) | ||
| 62 | return assert.same(not nil, true) | ||
| 63 | end) | ||
| 64 | it("should support bitwise and", function() | ||
| 65 | return assert.same(5 & 3, 1) | ||
| 66 | end) | ||
| 67 | it("should support bitwise or", function() | ||
| 68 | return assert.same(5 | 3, 7) | ||
| 69 | end) | ||
| 70 | it("should support bitwise xor", function() | ||
| 71 | return assert.same(5 ~ 3, 6) | ||
| 72 | end) | ||
| 73 | it("should support left shift", function() | ||
| 74 | return assert.same(2 << 3, 16) | ||
| 75 | end) | ||
| 76 | it("should support right shift", function() | ||
| 77 | return assert.same(16 >> 2, 4) | ||
| 78 | end) | ||
| 79 | it("should support string concatenation", function() | ||
| 80 | return assert.same("hello" .. " world", "hello world") | ||
| 81 | end) | ||
| 82 | it("should support length operator", function() | ||
| 83 | assert.same(#"hello", 5) | ||
| 84 | return assert.same(#{ | ||
| 85 | 1, | ||
| 86 | 2, | ||
| 87 | 3 | ||
| 88 | }, 3) | ||
| 89 | end) | ||
| 90 | it("should respect operator precedence", function() | ||
| 91 | assert.same(1 + 2 * 3, 7) | ||
| 92 | return assert.same((1 + 2) * 3, 9) | ||
| 93 | end) | ||
| 94 | it("should support compound assignment", function() | ||
| 95 | local x = 10 | ||
| 96 | x = x + 5 | ||
| 97 | return assert.same(x, 15) | ||
| 98 | end) | ||
| 99 | it("should support compound subtraction", function() | ||
| 100 | local x = 10 | ||
| 101 | x = x - 3 | ||
| 102 | return assert.same(x, 7) | ||
| 103 | end) | ||
| 104 | it("should support compound multiplication", function() | ||
| 105 | local x = 5 | ||
| 106 | x = x * 2 | ||
| 107 | return assert.same(x, 10) | ||
| 108 | end) | ||
| 109 | it("should support compound division", function() | ||
| 110 | local x = 20 | ||
| 111 | x = x / 4 | ||
| 112 | return assert.same(x, 5) | ||
| 113 | end) | ||
| 114 | it("should handle division by zero", function() | ||
| 115 | local result = pcall(function() | ||
| 116 | local x = 10 / 0 | ||
| 117 | end) | ||
| 118 | return assert.is_true(result) | ||
| 119 | end) | ||
| 120 | it("should handle very large numbers", function() | ||
| 121 | local big = 1e100 | ||
| 122 | return assert.is_true(big > 0) | ||
| 123 | end) | ||
| 124 | it("should handle very small numbers", function() | ||
| 125 | local small = 1e-100 | ||
| 126 | return assert.is_true(small > 0) | ||
| 127 | end) | ||
| 128 | it("should support negation", function() | ||
| 129 | assert.same(-10, -10) | ||
| 130 | return assert.same | ||
| 131 | end) | ||
| 132 | it("should work with complex expressions", function() | ||
| 133 | local result = (1 + 2) * (3 + 4) / 2 | ||
| 134 | return assert.same(result, 10.5) | ||
| 135 | end) | ||
| 136 | it("should support power with decimal", function() | ||
| 137 | return assert.same(4 ^ 0.5, 2) | ||
| 138 | end) | ||
| 139 | return it("should handle modulo with negative numbers", function() | ||
| 140 | return assert.same(-10 % 3, 2) | ||
| 141 | end) | ||
| 142 | end) | ||
