aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/table_append_spec.yue
blob: ab3d6d297790ddbb1336ffee8476fc05fac27309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
describe "table append", ->
	it "should append single value", ->
		tab = []
		tab[] = "Value"
		assert.same tab[1], "Value"
		assert.same #tab, 1

	it "should append multiple values", ->
		tab = []
		tab[] = 1
		tab[] = 2
		tab[] = 3
		assert.same tab, {1, 2, 3}

	it "should append with spread operator", ->
		tbA = [1, 2, 3]
		tbB = [4, 5, 6]
		tbA[] = ...tbB
		assert.same tbA, {1, 2, 3, 4, 5, 6}

	it "should append table with single element", ->
		tab = [1, 2]
		tb2 = {3}
		tab[] = table.unpack tb2
		assert.same tab, {1, 2, 3}

	it "should append empty table", ->
		tab = [1, 2]
		tb2 = []
		tab[] = ...tb2
		assert.same tab, {1, 2}

	it "should append nil values", ->
		tab = []
		tab[] = nil
		tab[] = "value"
		assert.same tab[1], nil
		assert.same tab[2], "value"

	it "should work in loop", ->
		tab = []
		for i = 1, 3
			tab[] = i * 2
		assert.same tab, {2, 4, 6}

	it "should append with expressions", ->
		tab = []
		x = 10
		tab[] = x + 5
		assert.same tab[1], 15

	it "should append mixed types", ->
		tab = []
		tab[] = "string"
		tab[] = 123
		tab[] = true
		tab[] = nil
		assert.same tab, {"string", 123, true, nil}

	it "should append to table with existing elements", ->
		tab = [1, 2, 3]
		tab[] = 4
		tab[] = 5
		assert.same tab, {1, 2, 3, 4, 5}

	it "should work with nested tables", ->
		tab = []
		tab[] = {a: 1, b: 2}
		tab[] = [3, 4]
		assert.same tab[1], {a: 1, b: 2}
		assert.same tab[2], [3, 4]

	it "should append function results", ->
		fn = -> 1, 2, 3
		tab = []
		tab[] = fn!
		assert.same tab, {1, 2, 3}