aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/table_comprehension_spec.yue
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-27 00:30:56 +0000
committerLi Jin <dragon-fly@qq.com>2026-01-27 00:30:56 +0000
commit7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (patch)
treeceba95c48bd8d5d9fff3d1206483ddf073c0e03d /spec/inputs/test/table_comprehension_spec.yue
parente70e63a9737ed3a9e72f1329901075498190e6b4 (diff)
downloadyuescript-compiler-improvements.tar.gz
yuescript-compiler-improvements.tar.bz2
yuescript-compiler-improvements.zip
Add compiler improvements and comprehensive test suitecompiler-improvements
- Fixed path option handling to avoid semicolon concatenation issues - Added exception handling for std::length_error and general exceptions - Added comprehensive test specifications for advanced language features Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/inputs/test/table_comprehension_spec.yue')
-rw-r--r--spec/inputs/test/table_comprehension_spec.yue127
1 files changed, 127 insertions, 0 deletions
diff --git a/spec/inputs/test/table_comprehension_spec.yue b/spec/inputs/test/table_comprehension_spec.yue
new file mode 100644
index 0000000..f4d7cdb
--- /dev/null
+++ b/spec/inputs/test/table_comprehension_spec.yue
@@ -0,0 +1,127 @@
1describe "table comprehension", ->
2 it "should create simple table copy", ->
3 thing = {
4 color: "red"
5 name: "fast"
6 width: 123
7 }
8
9 thing_copy = {k, v for k, v in pairs thing}
10 assert.same thing_copy.color, thing.color
11 assert.same thing_copy.name, thing.name
12 assert.same thing_copy.width, thing.width
13
14 it "should filter with when clause", ->
15 thing = {
16 color: "red"
17 name: "fast"
18 width: 123
19 }
20
21 no_color = {k, v for k, v in pairs thing when k != "color"}
22 assert.same no_color.color, nil
23 assert.same no_color.name, "fast"
24 assert.same no_color.width, 123
25
26 it "should transform values", ->
27 numbers = {a: 1, b: 2, c: 3}
28 doubled = {k, v * 2 for k, v in pairs numbers}
29 assert.same doubled.a, 2
30 assert.same doubled.b, 4
31 assert.same doubled.c, 6
32
33 it "should transform keys", ->
34 data = {a: 1, b: 2}
35 upper_keys = {k\upper!, v for k, v in pairs data}
36 assert.same upper_keys.A, 1
37 assert.same upper_keys.B, 2
38
39 it "should work with ipairs", ->
40 items = {"a", "b", "c"}
41 reversed = {i, v for i, v in ipairs items}
42 assert.same reversed[1], "a"
43 assert.same reversed[2], "b"
44 assert.same reversed[3], "c"
45
46 it "should filter array items", ->
47 items = {1, 2, 3, 4, 5}
48 evens = {i, v for i, v in ipairs items when v % 2 == 0}
49 assert.same evens[2], 2
50 assert.same evens[4], 4
51 assert.same evens[1], nil
52
53 it "should work with numeric for loop", ->
54 squares = {i, i * i for i = 1, 5}
55 assert.same squares[1], 1
56 assert.same squares[2], 4
57 assert.same squares[3], 9
58 assert.same squares[4], 16
59 assert.same squares[5], 25
60
61 it "should support nested comprehensions", ->
62 matrix = {{1, 2}, {3, 4}, {5, 6}}
63 flat = {}
64 for row in *matrix
65 for i, v in ipairs row
66 flat[#flat + 1] = v
67
68 assert.same flat, {1, 2, 3, 4, 5, 6}
69
70 it "should combine pairs and when", ->
71 data = {a: 1, b: 2, c: 3, d: 4}
72 greater_than_two = {k, v for k, v in pairs data when v > 2}
73 assert.same greater_than_two.a, nil
74 assert.same greater_than_two.b, nil
75 assert.same greater_than_two.c, 3
76 assert.same greater_than_two.d, 4
77
78 it "should work with string keys", ->
79 obj = {["key-with-dash"]: "value1", ["key_with_underscore"]: "value2"}
80 result = {k, v for k, v in pairs obj}
81 assert.same result["key-with-dash"], "value1"
82 assert.same result["key_with_underscore"], "value2"
83
84 it "should handle empty source", ->
85 empty = {}
86 result = {k, v for k, v in pairs empty}
87 assert.same #result, 0
88
89 it "should work with computed keys", ->
90 base = {a: 1, b: 2}
91 result = {k .. "_suffix", v * 10 for k, v in pairs base}
92 assert.same result.a_suffix, 10
93 assert.same result.b_suffix, 20
94
95 it "should support nested table transformation", ->
96 data = {
97 first: {x: 1, y: 2}
98 second: {x: 3, y: 4}
99 }
100
101 transformed = {k, v.x + v.y for k, v in pairs data}
102 assert.same transformed.first, 3
103 assert.same transformed.second, 7
104
105 it "should filter with multiple conditions", ->
106 numbers = {a: 1, b: 2, c: 3, d: 4, e: 5}
107 result = {k, v for k, v in pairs numbers when v > 1 and v < 5}
108 assert.same result.a, nil
109 assert.same result.b, 2
110 assert.same result.c, 3
111 assert.same result.d, 4
112 assert.same result.e, nil
113
114 it "should work with custom iterator", ->
115 custom_iter = -> ->
116 state = 0
117 ->
118 state += 1
119 if state <= 3
120 state, state * 10
121 else
122 nil
123
124 result = {k, v for k, v in custom_iter!}
125 assert.same result[1], 10
126 assert.same result[2], 20
127 assert.same result[3], 30