diff options
Diffstat (limited to 'spec/outputs/test/const_attribute_spec.lua')
| -rw-r--r-- | spec/outputs/test/const_attribute_spec.lua | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/spec/outputs/test/const_attribute_spec.lua b/spec/outputs/test/const_attribute_spec.lua new file mode 100644 index 0000000..2e2f7a3 --- /dev/null +++ b/spec/outputs/test/const_attribute_spec.lua | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | return describe("const attribute", function() | ||
| 2 | it("should declare const variable", function() | ||
| 3 | local a <const> = 123 | ||
| 4 | return assert.same(a, 123) | ||
| 5 | end) | ||
| 6 | it("should prevent reassignment", function() | ||
| 7 | local b <const> = 456 | ||
| 8 | return assert.same(b, 456) | ||
| 9 | end) | ||
| 10 | it("should work with strings", function() | ||
| 11 | local name <const> = "test" | ||
| 12 | return assert.same(name, "test") | ||
| 13 | end) | ||
| 14 | it("should support const with destructuring", function() | ||
| 15 | local tb = { | ||
| 16 | a = 1, | ||
| 17 | b = 2, | ||
| 18 | 3, | ||
| 19 | 4 | ||
| 20 | } | ||
| 21 | local a, b, c, d | ||
| 22 | a, b, c, d = tb.a, tb.b, tb[1], tb[2] | ||
| 23 | assert.same(a, 1) | ||
| 24 | assert.same(b, 2) | ||
| 25 | assert.same(c, 3) | ||
| 26 | return assert.same(d, 4) | ||
| 27 | end) | ||
| 28 | it("should handle nested const", function() | ||
| 29 | local nested <const> = { | ||
| 30 | inner = { | ||
| 31 | value = 10 | ||
| 32 | } | ||
| 33 | } | ||
| 34 | return assert.same(nested.inner.value, 10) | ||
| 35 | end) | ||
| 36 | it("should work with arrays", function() | ||
| 37 | local items | ||
| 38 | items = { | ||
| 39 | 1, | ||
| 40 | 2, | ||
| 41 | 3 | ||
| 42 | } | ||
| 43 | return assert.same(items[1], 1) | ||
| 44 | end) | ||
| 45 | it("should support const in function scope", function() | ||
| 46 | local fn | ||
| 47 | fn = function() | ||
| 48 | local local_const <const> = "local" | ||
| 49 | return local_const | ||
| 50 | end | ||
| 51 | local result = fn() | ||
| 52 | return assert.same(result, "local") | ||
| 53 | end) | ||
| 54 | it("should work with multiple const declarations", function() | ||
| 55 | local x <const> = 1 | ||
| 56 | local y <const> = 2 | ||
| 57 | local z <const> = 3 | ||
| 58 | return assert.same(x + y + z, 6) | ||
| 59 | end) | ||
| 60 | it("should handle const functions", function() | ||
| 61 | local add | ||
| 62 | add = function(a, b) | ||
| 63 | return a + b | ||
| 64 | end | ||
| 65 | return assert.same(add(5, 10), 15) | ||
| 66 | end) | ||
| 67 | it("should work with const tables", function() | ||
| 68 | local config <const> = { | ||
| 69 | host = "localhost", | ||
| 70 | port = 8080 | ||
| 71 | } | ||
| 72 | assert.same(config.host, "localhost") | ||
| 73 | return assert.same(config.port, 8080) | ||
| 74 | end) | ||
| 75 | it("should support global const", function() | ||
| 76 | GLOBAL_CONST = 999 | ||
| 77 | return assert.same(GLOBAL_CONST, 999) | ||
| 78 | end) | ||
| 79 | it("should work with boolean const", function() | ||
| 80 | local flag <const> = true | ||
| 81 | local another <const> = false | ||
| 82 | assert.is_true(flag) | ||
| 83 | return assert.is_false(another) | ||
| 84 | end) | ||
| 85 | it("should handle nil const", function() | ||
| 86 | local nil_value <const> = nil | ||
| 87 | return assert.same(nil_value, nil) | ||
| 88 | end) | ||
| 89 | it("should work with expressions", function() | ||
| 90 | local calculated <const> = 10 + 20 | ||
| 91 | return assert.same(calculated, 30) | ||
| 92 | end) | ||
| 93 | it("should work in table comprehension", function() | ||
| 94 | local multiplier <const> = 2 | ||
| 95 | local items = { | ||
| 96 | 1, | ||
| 97 | 2, | ||
| 98 | 3 | ||
| 99 | } | ||
| 100 | local result | ||
| 101 | do | ||
| 102 | local _accum_0 = { } | ||
| 103 | local _len_0 = 1 | ||
| 104 | for _index_0 = 1, #items do | ||
| 105 | local item = items[_index_0] | ||
| 106 | _accum_0[_len_0] = item * multiplier | ||
| 107 | _len_0 = _len_0 + 1 | ||
| 108 | end | ||
| 109 | result = _accum_0 | ||
| 110 | end | ||
| 111 | return assert.same(result, { | ||
| 112 | 2, | ||
| 113 | 4, | ||
| 114 | 6 | ||
| 115 | }) | ||
| 116 | end) | ||
| 117 | return it("should work with complex expressions", function() | ||
| 118 | local complex <const> = { | ||
| 119 | data = { | ||
| 120 | 1, | ||
| 121 | 2, | ||
| 122 | 3 | ||
| 123 | }, | ||
| 124 | nested = { | ||
| 125 | key = "value" | ||
| 126 | } | ||
| 127 | } | ||
| 128 | assert.same(complex.data[1], 1) | ||
| 129 | return assert.same(complex.nested.key, "value") | ||
| 130 | end) | ||
| 131 | end) | ||
