diff options
Diffstat (limited to 'spec/inputs/test/table_append_spec.yue')
| -rw-r--r-- | spec/inputs/test/table_append_spec.yue | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/spec/inputs/test/table_append_spec.yue b/spec/inputs/test/table_append_spec.yue new file mode 100644 index 0000000..ab3d6d2 --- /dev/null +++ b/spec/inputs/test/table_append_spec.yue | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | describe "table append", -> | ||
| 2 | it "should append single value", -> | ||
| 3 | tab = [] | ||
| 4 | tab[] = "Value" | ||
| 5 | assert.same tab[1], "Value" | ||
| 6 | assert.same #tab, 1 | ||
| 7 | |||
| 8 | it "should append multiple values", -> | ||
| 9 | tab = [] | ||
| 10 | tab[] = 1 | ||
| 11 | tab[] = 2 | ||
| 12 | tab[] = 3 | ||
| 13 | assert.same tab, {1, 2, 3} | ||
| 14 | |||
| 15 | it "should append with spread operator", -> | ||
| 16 | tbA = [1, 2, 3] | ||
| 17 | tbB = [4, 5, 6] | ||
| 18 | tbA[] = ...tbB | ||
| 19 | assert.same tbA, {1, 2, 3, 4, 5, 6} | ||
| 20 | |||
| 21 | it "should append table with single element", -> | ||
| 22 | tab = [1, 2] | ||
| 23 | tb2 = {3} | ||
| 24 | tab[] = table.unpack tb2 | ||
| 25 | assert.same tab, {1, 2, 3} | ||
| 26 | |||
| 27 | it "should append empty table", -> | ||
| 28 | tab = [1, 2] | ||
| 29 | tb2 = [] | ||
| 30 | tab[] = ...tb2 | ||
| 31 | assert.same tab, {1, 2} | ||
| 32 | |||
| 33 | it "should append nil values", -> | ||
| 34 | tab = [] | ||
| 35 | tab[] = nil | ||
| 36 | tab[] = "value" | ||
| 37 | assert.same tab[1], nil | ||
| 38 | assert.same tab[2], "value" | ||
| 39 | |||
| 40 | it "should work in loop", -> | ||
| 41 | tab = [] | ||
| 42 | for i = 1, 3 | ||
| 43 | tab[] = i * 2 | ||
| 44 | assert.same tab, {2, 4, 6} | ||
| 45 | |||
| 46 | it "should append with expressions", -> | ||
| 47 | tab = [] | ||
| 48 | x = 10 | ||
| 49 | tab[] = x + 5 | ||
| 50 | assert.same tab[1], 15 | ||
| 51 | |||
| 52 | it "should append mixed types", -> | ||
| 53 | tab = [] | ||
| 54 | tab[] = "string" | ||
| 55 | tab[] = 123 | ||
| 56 | tab[] = true | ||
| 57 | tab[] = nil | ||
| 58 | assert.same tab, {"string", 123, true, nil} | ||
| 59 | |||
| 60 | it "should append to table with existing elements", -> | ||
| 61 | tab = [1, 2, 3] | ||
| 62 | tab[] = 4 | ||
| 63 | tab[] = 5 | ||
| 64 | assert.same tab, {1, 2, 3, 4, 5} | ||
| 65 | |||
| 66 | it "should work with nested tables", -> | ||
| 67 | tab = [] | ||
| 68 | tab[] = {a: 1, b: 2} | ||
| 69 | tab[] = [3, 4] | ||
| 70 | assert.same tab[1], {a: 1, b: 2} | ||
| 71 | assert.same tab[2], [3, 4] | ||
| 72 | |||
| 73 | it "should append function results", -> | ||
| 74 | fn = -> 1, 2, 3 | ||
| 75 | tab = [] | ||
| 76 | tab[] = fn! | ||
| 77 | assert.same tab, {1, 2, 3} | ||
