diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-27 00:30:56 +0000 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-27 00:30:56 +0000 |
| commit | 7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (patch) | |
| tree | ceba95c48bd8d5d9fff3d1206483ddf073c0e03d /spec/inputs/test/tables_advanced_spec.yue | |
| parent | e70e63a9737ed3a9e72f1329901075498190e6b4 (diff) | |
| download | yuescript-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/tables_advanced_spec.yue')
| -rw-r--r-- | spec/inputs/test/tables_advanced_spec.yue | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/spec/inputs/test/tables_advanced_spec.yue b/spec/inputs/test/tables_advanced_spec.yue new file mode 100644 index 0000000..c8cc7d5 --- /dev/null +++ b/spec/inputs/test/tables_advanced_spec.yue | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | describe "advanced tables", -> | ||
| 2 | it "should create table with implicit keys", -> | ||
| 3 | hair = "golden" | ||
| 4 | height = 200 | ||
| 5 | person = { :hair, :height, shoe_size: 40 } | ||
| 6 | assert.same person.hair, "golden" | ||
| 7 | assert.same person.height, 200 | ||
| 8 | |||
| 9 | it "should work with computed keys", -> | ||
| 10 | t = { | ||
| 11 | [1 + 2]: "hello" | ||
| 12 | ["key_" .. "suffix"]: "value" | ||
| 13 | } | ||
| 14 | assert.same t[3], "hello" | ||
| 15 | assert.same t["key_suffix"], "value" | ||
| 16 | |||
| 17 | it "should support keyword keys", -> | ||
| 18 | tbl = { | ||
| 19 | do: "something" | ||
| 20 | end: "hunger" | ||
| 21 | function: "test" | ||
| 22 | } | ||
| 23 | assert.same tbl.do, "something" | ||
| 24 | assert.same tbl.end, "hunger" | ||
| 25 | assert.same tbl.function, "test" | ||
| 26 | |||
| 27 | it "should handle array syntax with mixed content", -> | ||
| 28 | tb = { | ||
| 29 | 1, 2, 3 | ||
| 30 | name: "superman" | ||
| 31 | 4, 5, 6 | ||
| 32 | } | ||
| 33 | assert.same tb[1], 1 | ||
| 34 | assert.same tb.name, "superman" | ||
| 35 | assert.same tb[4], 4 | ||
| 36 | |||
| 37 | it "should work with single line table literals", -> | ||
| 38 | my_function dance: "Tango", partner: "none" | ||
| 39 | assert.is_true true | ||
| 40 | |||
| 41 | it "should support nested tables", -> | ||
| 42 | tb = | ||
| 43 | outer: | ||
| 44 | inner: | ||
| 45 | value: 42 | ||
| 46 | assert.same tb.outer.inner.value, 42 | ||
| 47 | |||
| 48 | it "should handle table without braces", -> | ||
| 49 | profile = | ||
| 50 | height: "4 feet" | ||
| 51 | shoe_size: 13 | ||
| 52 | favorite_foods: ["ice cream", "donuts"] | ||
| 53 | assert.same profile.height, "4 feet" | ||
| 54 | assert.same profile.shoe_size, 13 | ||
| 55 | |||
| 56 | it "should work with colon syntax for keys", -> | ||
| 57 | t = { | ||
| 58 | name: "Bill" | ||
| 59 | age: 200 | ||
| 60 | ["favorite food"]: "rice" | ||
| 61 | } | ||
| 62 | assert.same t.name, "Bill" | ||
| 63 | assert.same t["favorite food"], "rice" | ||
| 64 | |||
| 65 | it "should support implicit object in table", -> | ||
| 66 | tb = | ||
| 67 | name: "abc" | ||
| 68 | values: | ||
| 69 | - "a" | ||
| 70 | - "b" | ||
| 71 | - "c" | ||
| 72 | assert.same tb.values, {"a", "b", "c"} | ||
| 73 | |||
| 74 | it "should handle array only table", -> | ||
| 75 | some_values = [1, 2, 3, 4] | ||
| 76 | assert.same some_values[1], 1 | ||
| 77 | assert.same some_values[4], 4 | ||
| 78 | |||
| 79 | it "should work with trailing comma", -> | ||
| 80 | list_with_one = [1,] | ||
| 81 | assert.same list_with_one[1], 1 | ||
| 82 | |||
| 83 | it "should support table spreading", -> | ||
| 84 | a = {1, 2, 3, x: 1} | ||
| 85 | b = {4, 5, y: 1} | ||
| 86 | merge = {...a, ...b} | ||
| 87 | assert.same merge[1], 1 | ||
| 88 | assert.same merge[4], 4 | ||
| 89 | assert.same merge.x, 1 | ||
| 90 | assert.same merge.y, 1 | ||
| 91 | |||
| 92 | it "should handle mixed spread", -> | ||
| 93 | parts = { | ||
| 94 | * "shoulders" | ||
| 95 | * "knees" | ||
| 96 | } | ||
| 97 | lyrics = | ||
| 98 | * "head" | ||
| 99 | * ...parts | ||
| 100 | * "and" | ||
| 101 | * "toes" | ||
| 102 | assert.same lyrics, {"head", "shoulders", "knees", "and", "toes"} | ||
| 103 | |||
| 104 | it "should work with metatable creation", -> | ||
| 105 | mt = {} | ||
| 106 | add = (right) => <>: mt, value: @value + right.value | ||
| 107 | mt.__add = add | ||
| 108 | |||
| 109 | a = <>: mt, value: 1 | ||
| 110 | b = value: 2 | ||
| 111 | b.<>, mt | ||
| 112 | c = a + b | ||
| 113 | assert.same c.value, 3 | ||
| 114 | |||
| 115 | it "should support metatable accessing", -> | ||
| 116 | tb = <"value">: 123 | ||
| 117 | tb.<index> = tb.<> | ||
| 118 | assert.same tb.value, 123 | ||
| 119 | |||
| 120 | it "should handle metatable destructuring", -> | ||
| 121 | tb = { | ||
| 122 | item: "test" | ||
| 123 | new: -> "created" | ||
| 124 | close: -> "closed" | ||
| 125 | } | ||
| 126 | {:item, :new, :<close>} = tb | ||
| 127 | assert.same item, "test" | ||
| 128 | assert.same new!, "created" | ||
| 129 | |||
| 130 | it "should work with string keys directly", -> | ||
| 131 | t = { | ||
| 132 | "hello world": true | ||
| 133 | "test-key": "value" | ||
| 134 | } | ||
| 135 | assert.is_true t["hello world"] | ||
| 136 | assert.same t["test-key"], "value" | ||
| 137 | |||
| 138 | it "should support number keys", -> | ||
| 139 | t = { | ||
| 140 | [10]: "ten" | ||
| 141 | [20]: "twenty" | ||
| 142 | } | ||
| 143 | assert.same t[10], "ten" | ||
| 144 | assert.same t[20], "twenty" | ||
| 145 | |||
| 146 | it "should handle empty tables", -> | ||
| 147 | empty = {} | ||
| 148 | assert.same #empty, 0 | ||
| 149 | |||
| 150 | it "should work with table literals in function calls", -> | ||
| 151 | fn = (tb) -> tb.x + tb.y | ||
| 152 | result = fn x: 10, y: 20 | ||
| 153 | assert.same result, 30 | ||
