diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-28 18:43:14 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-28 18:43:14 +0800 |
| commit | dd64edd58fe25ec74ae5958128cf3f74b0692f3b (patch) | |
| tree | 0f2de1df55897e2713977c5a53936757e14b477a /spec/inputs/test | |
| parent | 7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (diff) | |
| download | yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.gz yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.bz2 yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.zip | |
Fixed compiler issues and added 800+ test cases.
Diffstat (limited to 'spec/inputs/test')
21 files changed, 172 insertions, 178 deletions
diff --git a/spec/inputs/test/advanced_macro_spec.yue b/spec/inputs/test/advanced_macro_spec.yue index 3d7b10a..d88807f 100644 --- a/spec/inputs/test/advanced_macro_spec.yue +++ b/spec/inputs/test/advanced_macro_spec.yue | |||
| @@ -15,7 +15,10 @@ describe "advanced macro", -> | |||
| 15 | assert.same result, "hello world" | 15 | assert.same result, "hello world" |
| 16 | 16 | ||
| 17 | it "should work with conditional compilation", -> | 17 | it "should work with conditional compilation", -> |
| 18 | global debugMode = true | ||
| 19 | $ -> global debugMode = true | ||
| 18 | macro config = (debugging) -> | 20 | macro config = (debugging) -> |
| 21 | assert debugMode? | ||
| 19 | global debugMode = debugging == "true" | 22 | global debugMode = debugging == "true" |
| 20 | "" | 23 | "" |
| 21 | 24 | ||
| @@ -23,7 +26,7 @@ describe "advanced macro", -> | |||
| 23 | assert.is_true debugMode | 26 | assert.is_true debugMode |
| 24 | 27 | ||
| 25 | $config false | 28 | $config false |
| 26 | assert.is_false debugMode | 29 | assert.is_true debugMode |
| 27 | 30 | ||
| 28 | it "should support macro generating conditional code", -> | 31 | it "should support macro generating conditional code", -> |
| 29 | macro asserts = (cond) -> | 32 | macro asserts = (cond) -> |
| @@ -39,20 +42,24 @@ describe "advanced macro", -> | |||
| 39 | :code | 42 | :code |
| 40 | type: "lua" | 43 | type: "lua" |
| 41 | } | 44 | } |
| 42 | 45 | macro_test_var = 42 | |
| 43 | $luaCode "local macro_test_var = 42" | 46 | $luaCode [[local macro_test_var = 99]] |
| 44 | assert.same macro_test_var, 42 | 47 | assert.same macro_test_var, 42 |
| 45 | 48 | ||
| 46 | it "should support multi-line raw lua", -> | 49 | it "should support multi-line raw lua", -> |
| 47 | macro lua = (code) -> { | 50 | macro lua = (code) -> { |
| 48 | :code | 51 | :code |
| 49 | type: "lua" | 52 | type: "text" |
| 53 | locals: ["multiline_var1",] | ||
| 50 | } | 54 | } |
| 51 | 55 | ||
| 56 | multiline_var = "test" | ||
| 52 | $lua[==[ | 57 | $lua[==[ |
| 53 | local multiline_var = "test" | 58 | multiline_var = "test work" |
| 59 | local multiline_var1 = "test1" | ||
| 54 | ]==] | 60 | ]==] |
| 55 | assert.same multiline_var, "test" | 61 | assert.same multiline_var, "test work" |
| 62 | assert.same multiline_var1, "test1" | ||
| 56 | 63 | ||
| 57 | it "should export macro from module", -> | 64 | it "should export macro from module", -> |
| 58 | -- This test demonstrates macro export syntax | 65 | -- This test demonstrates macro export syntax |
| @@ -116,10 +123,14 @@ describe "advanced macro", -> | |||
| 116 | assert.same result, "Red" | 123 | assert.same result, "Red" |
| 117 | 124 | ||
| 118 | it "should handle complex macro logic", -> | 125 | it "should handle complex macro logic", -> |
| 119 | macro smart_print = (items) -> | 126 | my_print = (...) -> ... |
| 120 | "print(#{table.concat [item for item in *items], ', ')})" | 127 | macro smart_print = (...items) -> |
| 128 | "my_print(#{table.concat [item for item in *items], ', '})" | ||
| 121 | 129 | ||
| 122 | $smart_print {"hello", "world", 123} | 130 | a, b, c = $smart_print "hello", "world", 123 |
| 131 | assert.same a, "hello" | ||
| 132 | assert.same b, "world" | ||
| 133 | assert.same c, 123 | ||
| 123 | 134 | ||
| 124 | it "should work with table manipulation", -> | 135 | it "should work with table manipulation", -> |
| 125 | macro create_table = (...) -> | 136 | macro create_table = (...) -> |
| @@ -130,12 +141,7 @@ describe "advanced macro", -> | |||
| 130 | assert.same result, {"1", "2", "3"} | 141 | assert.same result, {"1", "2", "3"} |
| 131 | 142 | ||
| 132 | it "should support string concatenation in macro", -> | 143 | it "should support string concatenation in macro", -> |
| 133 | macro concat = (...) -> | 144 | macro concat = (...) -> table.concat {...}, " .. " |
| 134 | args = {...} | ||
| 135 | res = {} | ||
| 136 | for arg in *args | ||
| 137 | table.insert res, tostring arg | ||
| 138 | "'" .. table.concat(res, " .. ") .. "'" | ||
| 139 | 145 | ||
| 140 | result = $concat "hello", "world" | 146 | result = $concat "hello", "world" |
| 141 | assert.same result, "helloworld" | 147 | assert.same result, "helloworld" |
diff --git a/spec/inputs/test/close_attribute_spec.yue b/spec/inputs/test/close_attribute_spec.yue index 2354df7..ebda27e 100644 --- a/spec/inputs/test/close_attribute_spec.yue +++ b/spec/inputs/test/close_attribute_spec.yue | |||
| @@ -43,7 +43,7 @@ describe "close attribute", -> | |||
| 43 | closed = false | 43 | closed = false |
| 44 | obj = | 44 | obj = |
| 45 | value: 10 | 45 | value: 10 |
| 46 | close_method: <close>: => | 46 | <close>: => |
| 47 | closed = true | 47 | closed = true |
| 48 | 48 | ||
| 49 | do | 49 | do |
| @@ -82,9 +82,8 @@ describe "close attribute", -> | |||
| 82 | 82 | ||
| 83 | it "should work with table destructuring", -> | 83 | it "should work with table destructuring", -> |
| 84 | closed = false | 84 | closed = false |
| 85 | tb = {close: <close>: -> closed = true} | ||
| 86 | do | 85 | do |
| 87 | {:close} = tb | 86 | close tb = {<close>: -> closed = true} |
| 88 | assert.is_true closed | 87 | assert.is_true closed |
| 89 | 88 | ||
| 90 | it "should handle close with return value", -> | 89 | it "should handle close with return value", -> |
diff --git a/spec/inputs/test/const_attribute_spec.yue b/spec/inputs/test/const_attribute_spec.yue index e3cc638..24ced21 100644 --- a/spec/inputs/test/const_attribute_spec.yue +++ b/spec/inputs/test/const_attribute_spec.yue | |||
| @@ -13,7 +13,7 @@ describe "const attribute", -> | |||
| 13 | assert.same name, "test" | 13 | assert.same name, "test" |
| 14 | 14 | ||
| 15 | it "should support const with destructuring", -> | 15 | it "should support const with destructuring", -> |
| 16 | tb = {a: 1, b: 2, c: 3, d: 4} | 16 | tb = {a: 1, b: 2, 3, 4} |
| 17 | const {:a, :b, c, d} = tb | 17 | const {:a, :b, c, d} = tb |
| 18 | assert.same a, 1 | 18 | assert.same a, 1 |
| 19 | assert.same b, 2 | 19 | assert.same b, 2 |
| @@ -46,7 +46,7 @@ describe "const attribute", -> | |||
| 46 | 46 | ||
| 47 | it "should handle const functions", -> | 47 | it "should handle const functions", -> |
| 48 | const add = (a, b) -> a + b | 48 | const add = (a, b) -> a + b |
| 49 | assert.same add 5, 10, 15 | 49 | assert.same add(5, 10), 15 |
| 50 | 50 | ||
| 51 | it "should work with const tables", -> | 51 | it "should work with const tables", -> |
| 52 | const config = { | 52 | const config = { |
| @@ -74,28 +74,12 @@ describe "const attribute", -> | |||
| 74 | const calculated = 10 + 20 | 74 | const calculated = 10 + 20 |
| 75 | assert.same calculated, 30 | 75 | assert.same calculated, 30 |
| 76 | 76 | ||
| 77 | it "should support const with prefixed return", -> | ||
| 78 | getDefault: const "default" -> | ||
| 79 | return nil | ||
| 80 | |||
| 81 | result = getDefault! | ||
| 82 | assert.same result, nil | ||
| 83 | |||
| 84 | it "should work in table comprehension", -> | 77 | it "should work in table comprehension", -> |
| 85 | const multiplier = 2 | 78 | const multiplier = 2 |
| 86 | items = [1, 2, 3] | 79 | items = [1, 2, 3] |
| 87 | result = [item * multiplier for item in *items] | 80 | result = [item * multiplier for item in *items] |
| 88 | assert.same result, {2, 4, 6} | 81 | assert.same result, {2, 4, 6} |
| 89 | 82 | ||
| 90 | it "should handle const with fat arrow", -> | ||
| 91 | obj = | ||
| 92 | value: 100 | ||
| 93 | getValue: const => | ||
| 94 | @value | ||
| 95 | |||
| 96 | result = obj\getValue! | ||
| 97 | assert.same result, 100 | ||
| 98 | |||
| 99 | it "should work with complex expressions", -> | 83 | it "should work with complex expressions", -> |
| 100 | const complex = { | 84 | const complex = { |
| 101 | data: [1, 2, 3] | 85 | data: [1, 2, 3] |
diff --git a/spec/inputs/test/do_statement_spec.yue b/spec/inputs/test/do_statement_spec.yue index 0adad20..2611cba 100644 --- a/spec/inputs/test/do_statement_spec.yue +++ b/spec/inputs/test/do_statement_spec.yue | |||
| @@ -62,7 +62,7 @@ describe "do statement", -> | |||
| 62 | it "should support variable shadowing", -> | 62 | it "should support variable shadowing", -> |
| 63 | x = "outer" | 63 | x = "outer" |
| 64 | result = do | 64 | result = do |
| 65 | x = "inner" | 65 | local x = "inner" |
| 66 | x | 66 | x |
| 67 | assert.same result, "inner" | 67 | assert.same result, "inner" |
| 68 | assert.same x, "outer" | 68 | assert.same x, "outer" |
| @@ -74,7 +74,7 @@ describe "do statement", -> | |||
| 74 | 74 | ||
| 75 | result = do | 75 | result = do |
| 76 | with obj | 76 | with obj |
| 77 | \double! | 77 | break \double! |
| 78 | assert.same result, 20 | 78 | assert.same result, 20 |
| 79 | 79 | ||
| 80 | it "should handle comprehensions in do block", -> | 80 | it "should handle comprehensions in do block", -> |
| @@ -84,13 +84,13 @@ describe "do statement", -> | |||
| 84 | assert.same result, {2, 4, 6, 8, 10} | 84 | assert.same result, {2, 4, 6, 8, 10} |
| 85 | 85 | ||
| 86 | it "should work with try-catch", -> | 86 | it "should work with try-catch", -> |
| 87 | result = do | 87 | success, result = try |
| 88 | success = try | 88 | error "test error" |
| 89 | error "test error" | 89 | false |
| 90 | false | 90 | catch err |
| 91 | catch err | 91 | true |
| 92 | true | 92 | assert.is_false success |
| 93 | assert.is_true success | 93 | assert.is_true result |
| 94 | 94 | ||
| 95 | it "should support return statement", -> | 95 | it "should support return statement", -> |
| 96 | fn = -> | 96 | fn = -> |
| @@ -125,13 +125,15 @@ describe "do statement", -> | |||
| 125 | it "should support implicit return", -> | 125 | it "should support implicit return", -> |
| 126 | result = do | 126 | result = do |
| 127 | value = 42 | 127 | value = 42 |
| 128 | value | ||
| 128 | assert.same result, 42 | 129 | assert.same result, 42 |
| 129 | 130 | ||
| 130 | it "should handle empty do block", -> | 131 | it "should handle empty do block", -> |
| 131 | result = do | 132 | result = do nil |
| 132 | assert.same result, nil | 133 | assert.same result, nil |
| 133 | 134 | ||
| 134 | it "should work with backcalls", -> | 135 | it "should work with backcalls", -> |
| 136 | map = (f, items) -> [f item for item in *items] | ||
| 135 | result = do | 137 | result = do |
| 136 | items = [1, 2, 3] | 138 | items = [1, 2, 3] |
| 137 | (x) <- map _, items | 139 | (x) <- map _, items |
diff --git a/spec/inputs/test/functions_advanced_spec.yue b/spec/inputs/test/functions_advanced_spec.yue index d0e0cf5..32f0718 100644 --- a/spec/inputs/test/functions_advanced_spec.yue +++ b/spec/inputs/test/functions_advanced_spec.yue | |||
| @@ -19,32 +19,32 @@ describe "advanced functions", -> | |||
| 19 | x + y | 19 | x + y |
| 20 | 20 | ||
| 21 | assert.same fn!, 1200 | 21 | assert.same fn!, 1200 |
| 22 | assert.same fn(50), 1150 | 22 | assert.same fn(50), 1100 |
| 23 | 23 | ||
| 24 | it "should work with multi-line arguments", -> | 24 | it "should work with multi-line arguments", -> |
| 25 | my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | 25 | my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f |
| 26 | result = my_func 5, 4, 3, | 26 | result = my_func 5, 4, 3, |
| 27 | 8, 9, 10 | 27 | 8, 9, 10 |
| 28 | assert.same result, 39 | 28 | assert.same result, 39 |
| 29 | 29 | ||
| 30 | it "should support nested function calls", -> | 30 | it "should support nested function calls", -> |
| 31 | another_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | ||
| 32 | my_func = (a, b, c, d, e, f, g) -> a + b + c + d + e + f + g | ||
| 33 | |||
| 31 | result = my_func 5, 6, 7, | 34 | result = my_func 5, 6, 7, |
| 32 | 6, another_func 6, 7, 8, | 35 | 6, another_func 6, 7, 8, |
| 33 | 9, 1, 2, | 36 | 9, 1, 2, |
| 34 | 5, 4 | 37 | 5, 4 |
| 35 | 38 | ||
| 36 | another_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | 39 | assert.same result, 66 |
| 37 | my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | ||
| 38 | |||
| 39 | assert.same result, 52 | ||
| 40 | 40 | ||
| 41 | it "should handle implicit return", -> | 41 | it "should handle implicit return", -> |
| 42 | sum = (x, y) -> x + y | 42 | sum = (x, y) -> x + y |
| 43 | assert.same sum 10, 20, 30 | 43 | assert.same sum(10, 20), 30 |
| 44 | 44 | ||
| 45 | it "should work with explicit return", -> | 45 | it "should work with explicit return", -> |
| 46 | difference = (x, y) -> return x - y | 46 | difference = (x, y) -> return x - y |
| 47 | assert.same difference 20, 10, 10 | 47 | assert.same difference(20, 10), 10 |
| 48 | 48 | ||
| 49 | it "should support multiple return values", -> | 49 | it "should support multiple return values", -> |
| 50 | mystery = (x, y) -> x + y, x - y | 50 | mystery = (x, y) -> x + y, x - y |
| @@ -85,7 +85,7 @@ describe "advanced functions", -> | |||
| 85 | assert.same fn(1, 2, 3), 3 | 85 | assert.same fn(1, 2, 3), 3 |
| 86 | 86 | ||
| 87 | it "should support prefixed return", -> | 87 | it "should support prefixed return", -> |
| 88 | findValue: "not found" -> | 88 | findValue = (): "not found" -> |
| 89 | items = [1, 2, 3] | 89 | items = [1, 2, 3] |
| 90 | for item in *items | 90 | for item in *items |
| 91 | if item == 5 | 91 | if item == 5 |
| @@ -155,4 +155,4 @@ describe "advanced functions", -> | |||
| 155 | -> 0 | 155 | -> 0 |
| 156 | 156 | ||
| 157 | add = get_operation "add" | 157 | add = get_operation "add" |
| 158 | assert.same add 5, 3, 8 | 158 | assert.same add(5, 3), 8 |
diff --git a/spec/inputs/test/implicit_object_spec.yue b/spec/inputs/test/implicit_object_spec.yue index cea926e..a0bd331 100644 --- a/spec/inputs/test/implicit_object_spec.yue +++ b/spec/inputs/test/implicit_object_spec.yue | |||
| @@ -142,9 +142,9 @@ describe "implicit object", -> | |||
| 142 | it "should handle empty implicit object", -> | 142 | it "should handle empty implicit object", -> |
| 143 | tb = | 143 | tb = |
| 144 | items: | 144 | items: |
| 145 | - | 145 | - nil |
| 146 | 146 | ||
| 147 | assert.same tb.items, {nil} | 147 | assert.same tb, {items: [nil,]} |
| 148 | 148 | ||
| 149 | it "should work in function arguments", -> | 149 | it "should work in function arguments", -> |
| 150 | fn = (items) -> #items | 150 | fn = (items) -> #items |
diff --git a/spec/inputs/test/in_expression_spec.yue b/spec/inputs/test/in_expression_spec.yue index c1f4099..d9e3aec 100644 --- a/spec/inputs/test/in_expression_spec.yue +++ b/spec/inputs/test/in_expression_spec.yue | |||
| @@ -9,11 +9,11 @@ describe "in expression", -> | |||
| 9 | assert.is_true "b" in chars | 9 | assert.is_true "b" in chars |
| 10 | assert.is_false "z" in chars | 10 | assert.is_false "z" in chars |
| 11 | 11 | ||
| 12 | it "should check keys in table", -> | 12 | it "should check in table literal", -> |
| 13 | obj = {x: 1, y: 2, z: 3} | 13 | x = 1; y = 2; z = 3; w = 4 |
| 14 | assert.is_true "x" in obj | 14 | assert.is_true x in [x, y, z] |
| 15 | assert.is_true "y" in obj | 15 | assert.is_true y in [x, y, z] |
| 16 | assert.is_false "w" in obj | 16 | assert.is_false w in [x, y, z] |
| 17 | 17 | ||
| 18 | it "should work with mixed types", -> | 18 | it "should work with mixed types", -> |
| 19 | items = {1, "two", true, nil} | 19 | items = {1, "two", true, nil} |
| @@ -41,7 +41,8 @@ describe "in expression", -> | |||
| 41 | assert.is_false not (2 in items) | 41 | assert.is_false not (2 in items) |
| 42 | 42 | ||
| 43 | it "should work with nested tables", -> | 43 | it "should work with nested tables", -> |
| 44 | nested = {{1, 2}, {3, 4}, {5, 6}} | 44 | eq = (other) => @[1] == other[1] and @[2] == other[2] |
| 45 | nested = {{1, 2, :<eq>}, {3, 4, :<eq>}, {5, 6, :<eq>}} | ||
| 45 | assert.is_true {1, 2} in nested | 46 | assert.is_true {1, 2} in nested |
| 46 | assert.is_false {1, 3} in nested | 47 | assert.is_false {1, 3} in nested |
| 47 | 48 | ||
| @@ -60,7 +61,7 @@ describe "in expression", -> | |||
| 60 | it "should support table as value", -> | 61 | it "should support table as value", -> |
| 61 | key1 = {a: 1} | 62 | key1 = {a: 1} |
| 62 | key2 = {b: 2} | 63 | key2 = {b: 2} |
| 63 | tb = {[key1]: "first", [key2]: "second"} | 64 | tb = [key1, key2] |
| 64 | 65 | ||
| 65 | -- Note: this tests table reference equality | 66 | -- Note: this tests table reference equality |
| 66 | assert.is_true key1 in tb | 67 | assert.is_true key1 in tb |
| @@ -77,7 +78,7 @@ describe "in expression", -> | |||
| 77 | assert.is_true 1 in items | 78 | assert.is_true 1 in items |
| 78 | 79 | ||
| 79 | it "should work with string keys", -> | 80 | it "should work with string keys", -> |
| 80 | obj = {name: "test", value: 42} | 81 | obj = ["name", "value"] |
| 81 | assert.is_true "name" in obj | 82 | assert.is_true "name" in obj |
| 82 | assert.is_true "value" in obj | 83 | assert.is_true "value" in obj |
| 83 | assert.is_false "missing" in obj | 84 | assert.is_false "missing" in obj |
diff --git a/spec/inputs/test/multiline_args_spec.yue b/spec/inputs/test/multiline_args_spec.yue index bbb06f9..add9d2b 100644 --- a/spec/inputs/test/multiline_args_spec.yue +++ b/spec/inputs/test/multiline_args_spec.yue | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | describe "multiline arguments", -> | 1 | describe "multiline arguments", -> |
| 2 | it "should split arguments across lines", -> | 2 | it "should split arguments across lines", -> |
| 3 | sum = (a, b, c) -> a + b + c | 3 | sum = (a, b, c, d, e, f) -> a + b + c + d + e + f |
| 4 | result = sum 5, 4, 3, | 4 | result = sum 5, 4, 3, |
| 5 | 8, 9, 10 | 5 | 8, 9, 10 |
| 6 | assert.same result, 39 | 6 | assert.same result, 39 |
| @@ -42,46 +42,46 @@ describe "multiline arguments", -> | |||
| 42 | assert.same result, {1, 2, 3, 4, 9, 8, 9, 10} | 42 | assert.same result, {1, 2, 3, 4, 9, 8, 9, 10} |
| 43 | 43 | ||
| 44 | it "should handle deeply nested indentation", -> | 44 | it "should handle deeply nested indentation", -> |
| 45 | y = [ fn 1, 2, 3, | 45 | -- Create function first |
| 46 | 4, 5, | ||
| 47 | 5, 6, 7 | ||
| 48 | ] | ||
| 49 | |||
| 50 | -- Create the function first | ||
| 51 | fn = (a, b, c, d, e, f, g) -> a + b + c + d + e + f + g | 46 | fn = (a, b, c, d, e, f, g) -> a + b + c + d + e + f + g |
| 47 | y = { fn 1, 2, 3, | ||
| 48 | 4, | ||
| 49 | 5, 6, 7 | ||
| 50 | } | ||
| 52 | 51 | ||
| 53 | result = y[1] | 52 | result = y[1] |
| 54 | assert.same result, 22 | 53 | assert.same result, 28 |
| 55 | 54 | ||
| 56 | it "should work with conditional statements", -> | 55 | it "should work with conditional statements", -> |
| 57 | fn = (a, b, c, d, e) -> a + b + c + d + e | 56 | fn = (a, b, c, d, e, f) -> a + b + c + d + e + f |
| 58 | 57 | result1 = fn 1, 2, 3, | |
| 59 | result = if fn 1, 2, 3, | 58 | 4, 5, |
| 60 | "hello", | 59 | 6 |
| 61 | "world" | 60 | |
| 62 | "yes" | 61 | result = if result1 > 20 |
| 63 | else | 62 | "yes" |
| 64 | "no" | 63 | else |
| 64 | "no" | ||
| 65 | assert.same result, "yes" | 65 | assert.same result, "yes" |
| 66 | 66 | ||
| 67 | it "should support function expressions", -> | 67 | it "should support function expressions", -> |
| 68 | double = (x) -> x * 2 | 68 | doublePlus = (x, y) -> x * 2 + y |
| 69 | result = double 5, | 69 | result = doublePlus 5, |
| 70 | 10 | 70 | 10 |
| 71 | assert.same result, 20 | 71 | assert.same result, 20 |
| 72 | 72 | ||
| 73 | it "should handle chained function calls", -> | 73 | it "should handle chained function calls", -> |
| 74 | add = (a, b) -> a + b | 74 | add = (a, b, c, d) -> a + b + c + d |
| 75 | multiply = (a, b) -> a * b | 75 | multiply = (a, b, c, d) -> a * b * c * d |
| 76 | 76 | ||
| 77 | result = multiply add 1, 2, | 77 | result = multiply 1, 2, |
| 78 | add 3, 4 | 78 | 3, 4 |
| 79 | assert.same result, 21 | 79 | assert.same result, 24 |
| 80 | 80 | ||
| 81 | it "should work with method calls", -> | 81 | it "should work with method calls", -> |
| 82 | obj = | 82 | obj = |
| 83 | value: 10 | 83 | value: 10 |
| 84 | add: (a, b) => @value + a + b | 84 | add: (a, b, c) => @value + a + b + c |
| 85 | 85 | ||
| 86 | result = obj\add 5, 10, | 86 | result = obj\add 5, 10, |
| 87 | 15 | 87 | 15 |
| @@ -100,7 +100,7 @@ describe "multiline arguments", -> | |||
| 100 | assert.same result, 45 | 100 | assert.same result, 45 |
| 101 | 101 | ||
| 102 | it "should work with return statement", -> | 102 | it "should work with return statement", -> |
| 103 | fn = (a, b) -> a + b | 103 | fn = (a, b, c) -> a + b + c |
| 104 | get_value = -> | 104 | get_value = -> |
| 105 | return fn 10, 20, | 105 | return fn 10, 20, |
| 106 | 30 | 106 | 30 |
diff --git a/spec/inputs/test/named_varargs_spec.yue b/spec/inputs/test/named_varargs_spec.yue index a5ab2b1..29869b5 100644 --- a/spec/inputs/test/named_varargs_spec.yue +++ b/spec/inputs/test/named_varargs_spec.yue | |||
| @@ -97,12 +97,11 @@ describe "named varargs", -> | |||
| 97 | assert.same result, {"a", "b", "c"} | 97 | assert.same result, {"a", "b", "c"} |
| 98 | 98 | ||
| 99 | it "should support passing named varargs to another function", -> | 99 | it "should support passing named varargs to another function", -> |
| 100 | inner = (...) -> | ||
| 101 | {...} | ||
| 100 | outer = (...t) -> | 102 | outer = (...t) -> |
| 101 | inner (table.unpack t) | 103 | inner t[1], t[2], t[3] |
| 102 | 104 | ||
| 103 | inner = (a, b, c) -> | ||
| 104 | {a, b, c} | ||
| 105 | |||
| 106 | result = outer 1, 2, 3 | 105 | result = outer 1, 2, 3 |
| 107 | assert.same result, {1, 2, 3} | 106 | assert.same result, {1, 2, 3} |
| 108 | 107 | ||
diff --git a/spec/inputs/test/operator_advanced_spec.yue b/spec/inputs/test/operator_advanced_spec.yue index 8127fd4..8f88d17 100644 --- a/spec/inputs/test/operator_advanced_spec.yue +++ b/spec/inputs/test/operator_advanced_spec.yue | |||
| @@ -75,7 +75,7 @@ describe "advanced operators", -> | |||
| 75 | 75 | ||
| 76 | it "should handle compound bitwise xor", -> | 76 | it "should handle compound bitwise xor", -> |
| 77 | x = 12 -- 1100 in binary | 77 | x = 12 -- 1100 in binary |
| 78 | x ~= 10 -- 1010 in binary | 78 | x = x~10 -- 1010 in binary |
| 79 | assert.same x, 6 -- 0110 in binary | 79 | assert.same x, 6 -- 0110 in binary |
| 80 | 80 | ||
| 81 | it "should work with compound left shift", -> | 81 | it "should work with compound left shift", -> |
| @@ -121,10 +121,10 @@ describe "advanced operators", -> | |||
| 121 | it "should work with :: for method chaining", -> | 121 | it "should work with :: for method chaining", -> |
| 122 | obj = | 122 | obj = |
| 123 | value: 10 | 123 | value: 10 |
| 124 | add: (n) => @value += n | 124 | add: (n): @ => @value += n |
| 125 | get: => @value | 125 | get: => @value |
| 126 | 126 | ||
| 127 | result = obj::add 5::get! | 127 | result = obj::add(5)::get! |
| 128 | assert.same result, 15 | 128 | assert.same result, 15 |
| 129 | 129 | ||
| 130 | it "should handle complex expressions with precedence", -> | 130 | it "should handle complex expressions with precedence", -> |
diff --git a/spec/inputs/test/param_destructure_spec.yue b/spec/inputs/test/param_destructure_spec.yue index 4659031..5b9ef83 100644 --- a/spec/inputs/test/param_destructure_spec.yue +++ b/spec/inputs/test/param_destructure_spec.yue | |||
| @@ -38,10 +38,10 @@ describe "parameter destructuring", -> | |||
| 38 | assert.same result, {1, 2, 3} | 38 | assert.same result, {1, 2, 3} |
| 39 | 39 | ||
| 40 | it "should support mixed array and object", -> | 40 | it "should support mixed array and object", -> |
| 41 | f = ([first], {key: :value}) -> | 41 | f = ([first,], {key: :value}) -> |
| 42 | {first, value} | 42 | {first, value} |
| 43 | 43 | ||
| 44 | result = f [1], {key: "test"} | 44 | result = f [1,], {key: value: "test"} |
| 45 | assert.same result, {1, "test"} | 45 | assert.same result, {1, "test"} |
| 46 | 46 | ||
| 47 | it "should work with fat arrow", -> | 47 | it "should work with fat arrow", -> |
| @@ -54,7 +54,7 @@ describe "parameter destructuring", -> | |||
| 54 | assert.same result, 130 | 54 | assert.same result, 130 |
| 55 | 55 | ||
| 56 | it "should handle missing keys", -> | 56 | it "should handle missing keys", -> |
| 57 | f = (:a, :b = "default", :c = "missing") -> | 57 | f = ({:a, :b = "default", :c = "missing"}) -> |
| 58 | {a, b, c} | 58 | {a, b, c} |
| 59 | 59 | ||
| 60 | result = f a: 1 | 60 | result = f a: 1 |
| @@ -89,10 +89,10 @@ describe "parameter destructuring", -> | |||
| 89 | assert.same result, {1, {2, 3, 4}} | 89 | assert.same result, {1, {2, 3, 4}} |
| 90 | 90 | ||
| 91 | it "should support spreading", -> | 91 | it "should support spreading", -> |
| 92 | f = (...rest, :last) -> | 92 | f = ({...rest, :last}) -> |
| 93 | {rest, last} | 93 | {rest, last} |
| 94 | 94 | ||
| 95 | result = f 1, 2, 3, {last: "final"} | 95 | result = f {1, 2, 3, last: "final"} |
| 96 | assert.same result, {{1, 2, 3}, 'final'} | 96 | assert.same result, {{1, 2, 3}, 'final'} |
| 97 | 97 | ||
| 98 | it "should work with table comprehensions", -> | 98 | it "should work with table comprehensions", -> |
| @@ -103,8 +103,8 @@ describe "parameter destructuring", -> | |||
| 103 | assert.same result, {2, 4, 6} | 103 | assert.same result, {2, 4, 6} |
| 104 | 104 | ||
| 105 | it "should handle nil arguments", -> | 105 | it "should handle nil arguments", -> |
| 106 | f = (:a = "nil_a", :b = "nil_b") -> | 106 | f = ({:a = "nil_a", :b = "nil_b"}) -> |
| 107 | {a, b} | 107 | {a, b} |
| 108 | 108 | ||
| 109 | result = f nil, nil | 109 | result = f {} |
| 110 | assert.same result, {'nil_a', 'nil_b'} | 110 | assert.same result, {"nil_a", "nil_b"} |
diff --git a/spec/inputs/test/reverse_index_spec.yue b/spec/inputs/test/reverse_index_spec.yue index be67261..3c17d09 100644 --- a/spec/inputs/test/reverse_index_spec.yue +++ b/spec/inputs/test/reverse_index_spec.yue | |||
| @@ -42,10 +42,6 @@ describe "reverse index", -> | |||
| 42 | last = data.items.nested[#] | 42 | last = data.items.nested[#] |
| 43 | assert.same last, 3 | 43 | assert.same last, 3 |
| 44 | 44 | ||
| 45 | it "should work with string", -> | ||
| 46 | s = "hello" | ||
| 47 | assert.same s[#], "o" | ||
| 48 | |||
| 49 | it "should handle negative offsets", -> | 45 | it "should handle negative offsets", -> |
| 50 | tab = [1, 2, 3, 4, 5] | 46 | tab = [1, 2, 3, 4, 5] |
| 51 | assert.same tab[#-3], 2 | 47 | assert.same tab[#-3], 2 |
diff --git a/spec/inputs/test/slicing_spec.yue b/spec/inputs/test/slicing_spec.yue index b0a686b..2f5b1a7 100644 --- a/spec/inputs/test/slicing_spec.yue +++ b/spec/inputs/test/slicing_spec.yue | |||
| @@ -1,27 +1,27 @@ | |||
| 1 | describe "slicing", -> | 1 | describe "slicing", -> |
| 2 | it "should slice array with basic syntax", -> | 2 | it "should slice array with basic syntax", -> |
| 3 | items = [1, 2, 3, 4, 5] | 3 | items = [1, 2, 3, 4, 5] |
| 4 | result = items[1..3] | 4 | result = items[1, 3] |
| 5 | assert.same result, {1, 2, 3} | 5 | assert.same result, {1, 2, 3} |
| 6 | 6 | ||
| 7 | it "should slice from beginning", -> | 7 | it "should slice from beginning", -> |
| 8 | items = [1, 2, 3, 4, 5] | 8 | items = [1, 2, 3, 4, 5] |
| 9 | result = items[1..#items] | 9 | result = items[1, #items] |
| 10 | assert.same result, {1, 2, 3, 4, 5} | 10 | assert.same result, {1, 2, 3, 4, 5} |
| 11 | 11 | ||
| 12 | it "should slice to end", -> | 12 | it "should slice to end", -> |
| 13 | items = [1, 2, 3, 4, 5] | 13 | items = [1, 2, 3, 4, 5] |
| 14 | result = items[3..5] | 14 | result = items[3, 5] |
| 15 | assert.same result, {3, 4, 5} | 15 | assert.same result, {3, 4, 5} |
| 16 | 16 | ||
| 17 | it "should handle negative indices", -> | 17 | it "should handle negative indices", -> |
| 18 | items = [1, 2, 3, 4, 5] | 18 | items = [1, 2, 3, 4, 5] |
| 19 | result = items[#items-2..#items] | 19 | result = items[-3, -1] |
| 20 | assert.same result, {3, 4, 5} | 20 | assert.same result, {3, 4, 5} |
| 21 | 21 | ||
| 22 | it "should slice single element", -> | 22 | it "should slice single element", -> |
| 23 | items = [1, 2, 3, 4, 5] | 23 | items = [1, 2, 3, 4, 5] |
| 24 | result = items[2..2] | 24 | result = items[2, 2] |
| 25 | assert.same result, {2} | 25 | assert.same result, {2} |
| 26 | 26 | ||
| 27 | it "should work with strings", -> | 27 | it "should work with strings", -> |
| @@ -31,18 +31,18 @@ describe "slicing", -> | |||
| 31 | 31 | ||
| 32 | it "should handle out of bounds", -> | 32 | it "should handle out of bounds", -> |
| 33 | items = [1, 2, 3] | 33 | items = [1, 2, 3] |
| 34 | result = items[1..10] | 34 | result = items[1, 10] |
| 35 | assert.same result, {1, 2, 3} | 35 | assert.same result, {1, 2, 3} |
| 36 | 36 | ||
| 37 | it "should create new table", -> | 37 | it "should create new table", -> |
| 38 | original = [1, 2, 3, 4, 5] | 38 | original = [1, 2, 3, 4, 5] |
| 39 | sliced = original[2..4] | 39 | sliced = original[2, 4] |
| 40 | sliced[1] = 99 | 40 | sliced[1] = 99 |
| 41 | assert.same original[2], 2 -- original unchanged | 41 | assert.same original[2], 2 -- original unchanged |
| 42 | 42 | ||
| 43 | it "should work with nested arrays", -> | 43 | it "should work with nested arrays", -> |
| 44 | nested = [[1, 2], [3, 4], [5, 6]] | 44 | nested = [ [1, 2], [3, 4], [5, 6]] |
| 45 | result = nested[1..2] | 45 | result = nested[1, 2] |
| 46 | assert.same result, {{1, 2}, {3, 4}} | 46 | assert.same result, {{1, 2}, {3, 4}} |
| 47 | 47 | ||
| 48 | it "should slice with step simulation", -> | 48 | it "should slice with step simulation", -> |
| @@ -52,8 +52,8 @@ describe "slicing", -> | |||
| 52 | 52 | ||
| 53 | it "should handle empty slice range", -> | 53 | it "should handle empty slice range", -> |
| 54 | items = [1, 2, 3, 4, 5] | 54 | items = [1, 2, 3, 4, 5] |
| 55 | result = items[6..10] | 55 | result = items[6, 10] |
| 56 | assert.same result, nil | 56 | assert.same result, {} |
| 57 | 57 | ||
| 58 | it "should work with reverse indexing", -> | 58 | it "should work with reverse indexing", -> |
| 59 | items = [1, 2, 3, 4, 5] | 59 | items = [1, 2, 3, 4, 5] |
diff --git a/spec/inputs/test/stub_spec.yue b/spec/inputs/test/stub_spec.yue index 99345c7..835610b 100644 --- a/spec/inputs/test/stub_spec.yue +++ b/spec/inputs/test/stub_spec.yue | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | -- Define stub_fn as a helper function that returns an empty function | ||
| 2 | stub_fn = -> -> | ||
| 3 | |||
| 1 | describe "function stub", -> | 4 | describe "function stub", -> |
| 2 | it "should create empty function", -> | 5 | it "should create empty function", -> |
| 3 | stub_fn! | 6 | stub_fn! |
diff --git a/spec/inputs/test/table_append_spec.yue b/spec/inputs/test/table_append_spec.yue index ab3d6d2..51c7873 100644 --- a/spec/inputs/test/table_append_spec.yue +++ b/spec/inputs/test/table_append_spec.yue | |||
| @@ -30,12 +30,12 @@ describe "table append", -> | |||
| 30 | tab[] = ...tb2 | 30 | tab[] = ...tb2 |
| 31 | assert.same tab, {1, 2} | 31 | assert.same tab, {1, 2} |
| 32 | 32 | ||
| 33 | it "should append nil values", -> | 33 | it "should not append nil values", -> |
| 34 | tab = [] | 34 | tab = [] |
| 35 | tab[] = nil | 35 | tab[] = nil |
| 36 | tab[] = "value" | 36 | tab[] = "value" |
| 37 | assert.same tab[1], nil | 37 | assert.same tab[2], nil |
| 38 | assert.same tab[2], "value" | 38 | assert.same tab[1], "value" |
| 39 | 39 | ||
| 40 | it "should work in loop", -> | 40 | it "should work in loop", -> |
| 41 | tab = [] | 41 | tab = [] |
| @@ -73,5 +73,5 @@ describe "table append", -> | |||
| 73 | it "should append function results", -> | 73 | it "should append function results", -> |
| 74 | fn = -> 1, 2, 3 | 74 | fn = -> 1, 2, 3 |
| 75 | tab = [] | 75 | tab = [] |
| 76 | tab[] = fn! | 76 | tab[] = ...[fn!,] |
| 77 | assert.same tab, {1, 2, 3} | 77 | assert.same tab, {1, 2, 3} |
diff --git a/spec/inputs/test/table_comprehension_spec.yue b/spec/inputs/test/table_comprehension_spec.yue index f4d7cdb..caebdef 100644 --- a/spec/inputs/test/table_comprehension_spec.yue +++ b/spec/inputs/test/table_comprehension_spec.yue | |||
| @@ -112,7 +112,7 @@ describe "table comprehension", -> | |||
| 112 | assert.same result.e, nil | 112 | assert.same result.e, nil |
| 113 | 113 | ||
| 114 | it "should work with custom iterator", -> | 114 | it "should work with custom iterator", -> |
| 115 | custom_iter = -> -> | 115 | custom_iter = -> |
| 116 | state = 0 | 116 | state = 0 |
| 117 | -> | 117 | -> |
| 118 | state += 1 | 118 | state += 1 |
diff --git a/spec/inputs/test/tables_advanced_spec.yue b/spec/inputs/test/tables_advanced_spec.yue index c8cc7d5..82f4d4e 100644 --- a/spec/inputs/test/tables_advanced_spec.yue +++ b/spec/inputs/test/tables_advanced_spec.yue | |||
| @@ -35,8 +35,9 @@ describe "advanced tables", -> | |||
| 35 | assert.same tb[4], 4 | 35 | assert.same tb[4], 4 |
| 36 | 36 | ||
| 37 | it "should work with single line table literals", -> | 37 | it "should work with single line table literals", -> |
| 38 | my_function dance: "Tango", partner: "none" | 38 | tb = dance: "Tango", partner: "none" |
| 39 | assert.is_true true | 39 | assert.same tb.dance, "Tango" |
| 40 | assert.same tb.partner, "none" | ||
| 40 | 41 | ||
| 41 | it "should support nested tables", -> | 42 | it "should support nested tables", -> |
| 42 | tb = | 43 | tb = |
| @@ -90,10 +91,9 @@ describe "advanced tables", -> | |||
| 90 | assert.same merge.y, 1 | 91 | assert.same merge.y, 1 |
| 91 | 92 | ||
| 92 | it "should handle mixed spread", -> | 93 | it "should handle mixed spread", -> |
| 93 | parts = { | 94 | parts = |
| 94 | * "shoulders" | 95 | * "shoulders" |
| 95 | * "knees" | 96 | * "knees" |
| 96 | } | ||
| 97 | lyrics = | 97 | lyrics = |
| 98 | * "head" | 98 | * "head" |
| 99 | * ...parts | 99 | * ...parts |
| @@ -108,7 +108,7 @@ describe "advanced tables", -> | |||
| 108 | 108 | ||
| 109 | a = <>: mt, value: 1 | 109 | a = <>: mt, value: 1 |
| 110 | b = value: 2 | 110 | b = value: 2 |
| 111 | b.<>, mt | 111 | b.<> = mt |
| 112 | c = a + b | 112 | c = a + b |
| 113 | assert.same c.value, 3 | 113 | assert.same c.value, 3 |
| 114 | 114 | ||
| @@ -121,11 +121,12 @@ describe "advanced tables", -> | |||
| 121 | tb = { | 121 | tb = { |
| 122 | item: "test" | 122 | item: "test" |
| 123 | new: -> "created" | 123 | new: -> "created" |
| 124 | close: -> "closed" | 124 | <close>: -> "closed" |
| 125 | } | 125 | } |
| 126 | {:item, :new, :<close>} = tb | 126 | {:item, :new, :<close>} = tb |
| 127 | assert.same item, "test" | 127 | assert.same item, "test" |
| 128 | assert.same new!, "created" | 128 | assert.same new!, "created" |
| 129 | assert.same close!, "closed" | ||
| 129 | 130 | ||
| 130 | it "should work with string keys directly", -> | 131 | it "should work with string keys directly", -> |
| 131 | t = { | 132 | t = { |
diff --git a/spec/inputs/test/varargs_assignment_spec.yue b/spec/inputs/test/varargs_assignment_spec.yue index 1c3b627..dfd606b 100644 --- a/spec/inputs/test/varargs_assignment_spec.yue +++ b/spec/inputs/test/varargs_assignment_spec.yue | |||
| @@ -46,7 +46,7 @@ describe "varargs assignment", -> | |||
| 46 | assert.same select(3, ...), 2 | 46 | assert.same select(3, ...), 2 |
| 47 | 47 | ||
| 48 | it "should work with table.unpack", -> | 48 | it "should work with table.unpack", -> |
| 49 | tb = {a: 1, b: 2, c: 3} | 49 | tb = [1, 2, 3] |
| 50 | fn = -> table.unpack tb | 50 | fn = -> table.unpack tb |
| 51 | ... = fn! | 51 | ... = fn! |
| 52 | count = select '#', ... | 52 | count = select '#', ... |
diff --git a/spec/inputs/test/whitespace_spec.yue b/spec/inputs/test/whitespace_spec.yue index baf3fd5..43b7644 100644 --- a/spec/inputs/test/whitespace_spec.yue +++ b/spec/inputs/test/whitespace_spec.yue | |||
| @@ -8,13 +8,14 @@ describe "whitespace", -> | |||
| 8 | assert.same z, 30 | 8 | assert.same z, 30 |
| 9 | 9 | ||
| 10 | it "should work with semicolon in function", -> | 10 | it "should work with semicolon in function", -> |
| 11 | fn = -> a = 1; b = 2; a + b | 11 | fn = -> |
| 12 | a = 1; b = 2; a + b | ||
| 12 | assert.same fn!, 3 | 13 | assert.same fn!, 3 |
| 13 | 14 | ||
| 14 | it "should support multiline chaining", -> | 15 | it "should support multiline chaining", -> |
| 15 | obj = | 16 | obj = |
| 16 | value: 10 | 17 | value: 10 |
| 17 | add: (n) => @value += n | 18 | add: (n): @ => @value += n |
| 18 | get: => @value | 19 | get: => @value |
| 19 | 20 | ||
| 20 | result = obj | 21 | result = obj |
| @@ -26,7 +27,7 @@ describe "whitespace", -> | |||
| 26 | it "should handle multiline method calls", -> | 27 | it "should handle multiline method calls", -> |
| 27 | str = " hello " | 28 | str = " hello " |
| 28 | result = str | 29 | result = str |
| 29 | \trim! | 30 | \match "^%s*(.-)%s*$" |
| 30 | \upper! | 31 | \upper! |
| 31 | assert.same result, "HELLO" | 32 | assert.same result, "HELLO" |
| 32 | 33 | ||
| @@ -53,7 +54,7 @@ describe "whitespace", -> | |||
| 53 | 54 | ||
| 54 | it "should work with pipe in chaining", -> | 55 | it "should work with pipe in chaining", -> |
| 55 | result = [1, 2, 3] | 56 | result = [1, 2, 3] |
| 56 | |> [x * 2 for x in *] | 57 | |> ((tb) -> [x * 2 for x in *tb]) |
| 57 | |> table.concat | 58 | |> table.concat |
| 58 | assert.same result, "246" | 59 | assert.same result, "246" |
| 59 | 60 | ||
| @@ -68,7 +69,7 @@ describe "whitespace", -> | |||
| 68 | fn = -> | 69 | fn = -> |
| 69 | if true | 70 | if true |
| 70 | result = 10 | 71 | result = 10 |
| 71 | result | 72 | result |
| 72 | 73 | ||
| 73 | assert.same fn!, 10 | 74 | assert.same fn!, 10 |
| 74 | 75 | ||
| @@ -91,7 +92,7 @@ describe "whitespace", -> | |||
| 91 | 92 | ||
| 92 | it "should work in multiline function call", -> | 93 | it "should work in multiline function call", -> |
| 93 | sum = (a, b) -> a + b | 94 | sum = (a, b) -> a + b |
| 94 | result = sum 5, 10, | 95 | result = sum 5, sum 10, |
| 95 | sum 3, 7 | 96 | sum 3, 7 |
| 96 | assert.same result, 25 | 97 | assert.same result, 25 |
| 97 | 98 | ||
| @@ -105,13 +106,15 @@ describe "whitespace", -> | |||
| 105 | assert.same doubled, 10 | 106 | assert.same doubled, 10 |
| 106 | 107 | ||
| 107 | it "should handle complex chaining", -> | 108 | it "should handle complex chaining", -> |
| 108 | result = "hello" | 109 | result = ("hello") |
| 109 | \upper! | 110 | \upper! |
| 110 | \sub 1, 3 | 111 | \sub 1, 3 |
| 111 | \lower! | 112 | \lower! |
| 112 | assert.same result, "hel" | 113 | assert.same result, "hel" |
| 113 | 114 | ||
| 114 | it "should work with backcalls and whitespace", -> | 115 | it "should work with backcalls and whitespace", -> |
| 116 | readAsync = (file, callback) -> callback "data" | ||
| 117 | process = (data) -> true if data | ||
| 115 | results = do | 118 | results = do |
| 116 | data <- readAsync "data.txt" | 119 | data <- readAsync "data.txt" |
| 117 | process data | 120 | process data |
diff --git a/spec/inputs/test/with_statement_spec.yue b/spec/inputs/test/with_statement_spec.yue index c2f9b3b..71ac215 100644 --- a/spec/inputs/test/with_statement_spec.yue +++ b/spec/inputs/test/with_statement_spec.yue | |||
| @@ -27,7 +27,7 @@ describe "with statement", -> | |||
| 27 | obj = {x: 1} | 27 | obj = {x: 1} |
| 28 | with obj | 28 | with obj |
| 29 | .x = 10 | 29 | .x = 10 |
| 30 | with .nested = {y: 2} | 30 | with .nested := {y: 2} |
| 31 | .y = 20 | 31 | .y = 20 |
| 32 | assert.same obj.x, 10 | 32 | assert.same obj.x, 10 |
| 33 | assert.same obj.nested.y, 20 | 33 | assert.same obj.nested.y, 20 |
| @@ -35,7 +35,7 @@ describe "with statement", -> | |||
| 35 | it "should work in expressions", -> | 35 | it "should work in expressions", -> |
| 36 | obj = {value: 5} | 36 | obj = {value: 5} |
| 37 | result = with obj | 37 | result = with obj |
| 38 | .value * 2 | 38 | break .value * 2 |
| 39 | assert.same result, 10 | 39 | assert.same result, 10 |
| 40 | 40 | ||
| 41 | it "should support multiple statements", -> | 41 | it "should support multiple statements", -> |
| @@ -76,13 +76,13 @@ describe "with statement", -> | |||
| 76 | it "should support with in assignment", -> | 76 | it "should support with in assignment", -> |
| 77 | obj = {x: 5, y: 10} | 77 | obj = {x: 5, y: 10} |
| 78 | result = with obj | 78 | result = with obj |
| 79 | .x + .y | 79 | break .x + .y |
| 80 | assert.same result, 15 | 80 | assert.same result, 15 |
| 81 | 81 | ||
| 82 | it "should work with string methods", -> | 82 | it "should work with string methods", -> |
| 83 | s = "hello" | 83 | s = "hello" |
| 84 | result = with s | 84 | result = with s |
| 85 | \upper! | 85 | break \upper! |
| 86 | assert.same result, "HELLO" | 86 | assert.same result, "HELLO" |
| 87 | 87 | ||
| 88 | it "should handle metatable access", -> | 88 | it "should handle metatable access", -> |
| @@ -98,7 +98,7 @@ describe "with statement", -> | |||
| 98 | fn = -> | 98 | fn = -> |
| 99 | obj = {x: 10} | 99 | obj = {x: 10} |
| 100 | with obj | 100 | with obj |
| 101 | .x * 2 | 101 | break .x * 2 |
| 102 | 102 | ||
| 103 | result = fn! | 103 | result = fn! |
| 104 | assert.same result, 20 | 104 | assert.same result, 20 |
| @@ -107,19 +107,19 @@ describe "with statement", -> | |||
| 107 | get_value = -> | 107 | get_value = -> |
| 108 | obj = {value: 42} | 108 | obj = {value: 42} |
| 109 | with obj | 109 | with obj |
| 110 | .value | 110 | break .value |
| 111 | 111 | ||
| 112 | assert.same get_value!, 42 | 112 | assert.same get_value!, 42 |
| 113 | 113 | ||
| 114 | it "should work with existential operator", -> | 114 | it "should work with existential operator", -> |
| 115 | obj = {value: 10} | 115 | obj = {value: 10} |
| 116 | result = with obj | 116 | result = with obj |
| 117 | .value ? 0 | 117 | break .value ?? 0 |
| 118 | assert.same result, 10 | 118 | assert.same result, 10 |
| 119 | 119 | ||
| 120 | it "should handle nil object safely", -> | 120 | it "should handle nil object safely", -> |
| 121 | result = with nil | 121 | result = with? nil |
| 122 | .value | 122 | break .value |
| 123 | assert.same result, nil | 123 | assert.same result, nil |
| 124 | 124 | ||
| 125 | it "should work with method chaining", -> | 125 | it "should work with method chaining", -> |
| @@ -131,7 +131,7 @@ describe "with statement", -> | |||
| 131 | result = with obj | 131 | result = with obj |
| 132 | \add 10 | 132 | \add 10 |
| 133 | \add 5 | 133 | \add 5 |
| 134 | \get! | 134 | break \get! |
| 135 | assert.same result, 20 | 135 | assert.same result, 20 |
| 136 | 136 | ||
| 137 | it "should support nested property access", -> | 137 | it "should support nested property access", -> |
| @@ -141,5 +141,5 @@ describe "with statement", -> | |||
| 141 | level3: "deep" | 141 | level3: "deep" |
| 142 | 142 | ||
| 143 | result = with obj | 143 | result = with obj |
| 144 | .level1.level2.level3 | 144 | break .level1.level2.level3 |
| 145 | assert.same result, "deep" | 145 | assert.same result, "deep" |
diff --git a/spec/inputs/test/yaml_string_spec.yue b/spec/inputs/test/yaml_string_spec.yue index 1296340..0a7d61b 100644 --- a/spec/inputs/test/yaml_string_spec.yue +++ b/spec/inputs/test/yaml_string_spec.yue | |||
| @@ -3,15 +3,15 @@ describe "yaml string", -> | |||
| 3 | s = | | 3 | s = | |
| 4 | hello | 4 | hello |
| 5 | world | 5 | world |
| 6 | assert.is_true s\match "hello" | 6 | assert.is_true s\match("hello")? |
| 7 | assert.is_true s\match "world" | 7 | assert.is_true s\match("world")? |
| 8 | 8 | ||
| 9 | it "should preserve indentation", -> | 9 | it "should preserve indentation", -> |
| 10 | s = | | 10 | s = | |
| 11 | key1: value1 | 11 | key1: value1 |
| 12 | key2: value2 | 12 | key2: value2 |
| 13 | assert.is_true s\match "key1" | 13 | assert.is_true s\match("key1")? |
| 14 | assert.is_true s\match "key2" | 14 | assert.is_true s\match("key2")? |
| 15 | 15 | ||
| 16 | it "should support interpolation", -> | 16 | it "should support interpolation", -> |
| 17 | name = "test" | 17 | name = "test" |
| @@ -25,13 +25,13 @@ describe "yaml string", -> | |||
| 25 | point: | 25 | point: |
| 26 | x: #{x} | 26 | x: #{x} |
| 27 | y: #{y} | 27 | y: #{y} |
| 28 | assert.is_true s\match "x: 10" | 28 | assert.is_true s\match("x: 10")? |
| 29 | assert.is_true s\match "y: 20" | 29 | assert.is_true s\match("y: 20")? |
| 30 | 30 | ||
| 31 | it "should work with expressions", -> | 31 | it "should work with expressions", -> |
| 32 | s = | | 32 | s = | |
| 33 | result: #{1 + 2} | 33 | result: #{1 + 2} |
| 34 | assert.is_true s\match "result: 3" | 34 | assert.is_true s\match("result: 3")? |
| 35 | 35 | ||
| 36 | it "should support multiline with variables", -> | 36 | it "should support multiline with variables", -> |
| 37 | config = | | 37 | config = | |
| @@ -39,15 +39,17 @@ describe "yaml string", -> | |||
| 39 | host: localhost | 39 | host: localhost |
| 40 | port: 5432 | 40 | port: 5432 |
| 41 | name: mydb | 41 | name: mydb |
| 42 | assert.is_true config\match "database:" | 42 | assert.is_true config\match("database:")? |
| 43 | assert.is_true config\match "host:" | 43 | assert.is_true config\match("host:")? |
| 44 | 44 | ||
| 45 | it "should escape special characters", -> | 45 | it "should escape special characters", -> |
| 46 | Hello = "Hello" | ||
| 46 | s = | | 47 | s = | |
| 47 | path: "C:\Program Files\App" | 48 | path: "C:\Program Files\App" |
| 48 | note: 'He said: "#{Hello}!"' | 49 | note: 'He said: "#{Hello}!"' |
| 49 | assert.is_true s\match "path:" | 50 | assert.same s, [[ |
| 50 | assert.is_true s\match "note:" | 51 | path: "C:\Program Files\App" |
| 52 | note: 'He said: "Hello!"']] | ||
| 51 | 53 | ||
| 52 | it "should work in function", -> | 54 | it "should work in function", -> |
| 53 | fn = -> | 55 | fn = -> |
| @@ -57,8 +59,7 @@ describe "yaml string", -> | |||
| 57 | return str | 59 | return str |
| 58 | 60 | ||
| 59 | result = fn! | 61 | result = fn! |
| 60 | assert.is_true result\match "foo:" | 62 | assert.same result, "foo:\n\tbar: baz" |
| 61 | assert.is_true result\match "bar:" | ||
| 62 | 63 | ||
| 63 | it "should strip common leading whitespace", -> | 64 | it "should strip common leading whitespace", -> |
| 64 | fn = -> | 65 | fn = -> |
| @@ -68,40 +69,39 @@ describe "yaml string", -> | |||
| 68 | s | 69 | s |
| 69 | 70 | ||
| 70 | result = fn! | 71 | result = fn! |
| 71 | assert.is_true result\match "nested:" | 72 | assert.same result, "nested: |
| 72 | assert.is_true result\match "item:" | 73 | item: value" |
| 73 | 74 | ||
| 74 | it "should support empty lines", -> | 75 | it "should support empty lines", -> |
| 75 | s = | | 76 | s = | |
| 76 | line1 | 77 | line1 |
| 77 | 78 | ||
| 78 | line3 | 79 | line3 |
| 79 | assert.is_true s\match "line1" | 80 | assert.same s, "line1\nline3" |
| 80 | assert.is_true s\match "line3" | ||
| 81 | 81 | ||
| 82 | it "should work with table access in interpolation", -> | 82 | it "should work with table access in interpolation", -> |
| 83 | t = {value: 100} | 83 | t = {value: 100} |
| 84 | s = | | 84 | s = | |
| 85 | value: #{t.value} | 85 | value: #{t.value} |
| 86 | assert.is_true s\match "value: 100" | 86 | assert.same s, "value: 100" |
| 87 | 87 | ||
| 88 | it "should support function calls in interpolation", -> | 88 | it "should support function calls in interpolation", -> |
| 89 | s = | | 89 | s = | |
| 90 | result: #{(-> 42)!} | 90 | result: #{(-> 42)!} |
| 91 | assert.is_true s\match "result: 42" | 91 | assert.same s, "result: 42" |
| 92 | 92 | ||
| 93 | it "should handle quotes correctly", -> | 93 | it "should handle quotes correctly", -> |
| 94 | s = | | 94 | s = | |
| 95 | "quoted" | 95 | "quoted" |
| 96 | 'single quoted' | 96 | 'single quoted' |
| 97 | assert.is_true s\match '"quoted"' | 97 | assert.is_true s\match('"quoted"')? |
| 98 | assert.is_true s\match "'single quoted'" | 98 | assert.is_true s\match("'single quoted'")? |
| 99 | 99 | ||
| 100 | it "should work with multiple interpolations", -> | 100 | it "should work with multiple interpolations", -> |
| 101 | a, b, c = 1, 2, 3 | 101 | a, b, c = 1, 2, 3 |
| 102 | s = | | 102 | s = | |
| 103 | values: #{a}, #{b}, #{c} | 103 | values: #{a}, #{b}, #{c} |
| 104 | assert.is_true s\match "values: 1, 2, 3" | 104 | assert.same s, "values: 1, 2, 3" |
| 105 | 105 | ||
| 106 | it "should preserve newlines", -> | 106 | it "should preserve newlines", -> |
| 107 | s = | | 107 | s = | |
