diff options
Diffstat (limited to 'spec/inputs/test')
25 files changed, 2744 insertions, 0 deletions
diff --git a/spec/inputs/test/advanced_macro_spec.yue b/spec/inputs/test/advanced_macro_spec.yue new file mode 100644 index 0000000..3d7b10a --- /dev/null +++ b/spec/inputs/test/advanced_macro_spec.yue | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | describe "advanced macro", -> | ||
| 2 | it "should evaluate macro at compile time", -> | ||
| 3 | macro PI2 = -> math.pi * 2 | ||
| 4 | area = $PI2 * 5 | ||
| 5 | assert.is_true area > 0 | ||
| 6 | |||
| 7 | it "should support macro with arguments", -> | ||
| 8 | macro add = (a, b) -> "#{a} + #{b}" | ||
| 9 | result = $add 5, 10 | ||
| 10 | assert.same result, 15 | ||
| 11 | |||
| 12 | it "should handle string returning macro", -> | ||
| 13 | macro HELLO = -> "'hello world'" | ||
| 14 | result = $HELLO | ||
| 15 | assert.same result, "hello world" | ||
| 16 | |||
| 17 | it "should work with conditional compilation", -> | ||
| 18 | macro config = (debugging) -> | ||
| 19 | global debugMode = debugging == "true" | ||
| 20 | "" | ||
| 21 | |||
| 22 | $config true | ||
| 23 | assert.is_true debugMode | ||
| 24 | |||
| 25 | $config false | ||
| 26 | assert.is_false debugMode | ||
| 27 | |||
| 28 | it "should support macro generating conditional code", -> | ||
| 29 | macro asserts = (cond) -> | ||
| 30 | debugMode and "assert #{cond}" or "" | ||
| 31 | |||
| 32 | global debugMode = true | ||
| 33 | x = 10 | ||
| 34 | $asserts x == 10 -- should assert | ||
| 35 | assert.same x, 10 | ||
| 36 | |||
| 37 | it "should work with lua code insertion", -> | ||
| 38 | macro luaCode = (code) -> { | ||
| 39 | :code | ||
| 40 | type: "lua" | ||
| 41 | } | ||
| 42 | |||
| 43 | $luaCode "local macro_test_var = 42" | ||
| 44 | assert.same macro_test_var, 42 | ||
| 45 | |||
| 46 | it "should support multi-line raw lua", -> | ||
| 47 | macro lua = (code) -> { | ||
| 48 | :code | ||
| 49 | type: "lua" | ||
| 50 | } | ||
| 51 | |||
| 52 | $lua[==[ | ||
| 53 | local multiline_var = "test" | ||
| 54 | ]==] | ||
| 55 | assert.same multiline_var, "test" | ||
| 56 | |||
| 57 | it "should export macro from module", -> | ||
| 58 | -- This test demonstrates macro export syntax | ||
| 59 | -- Actual testing would require separate files | ||
| 60 | macro exported_macro = (x) -> "#{x} * 2" | ||
| 61 | result = $exported_macro 5 | ||
| 62 | assert.same result, 10 | ||
| 63 | |||
| 64 | it "should work with builtin FILE macro", -> | ||
| 65 | macro file_test = -> | ||
| 66 | "$FILE" | ||
| 67 | |||
| 68 | result = $file_test | ||
| 69 | assert.is_true type(result) == "string" | ||
| 70 | |||
| 71 | it "should work with builtin LINE macro", -> | ||
| 72 | macro line_test = -> | ||
| 73 | "$LINE" | ||
| 74 | |||
| 75 | result = $line_test | ||
| 76 | assert.is_true type(result) == "number" | ||
| 77 | |||
| 78 | it "should support argument validation", -> | ||
| 79 | macro expect_num = (val `Num) -> | ||
| 80 | "#{val}" | ||
| 81 | |||
| 82 | result = $expect_num 123 | ||
| 83 | assert.same result, 123 | ||
| 84 | |||
| 85 | it "should handle string argument validation", -> | ||
| 86 | macro expect_str = (str `String) -> | ||
| 87 | "#{str}" | ||
| 88 | |||
| 89 | result = $expect_str "hello" | ||
| 90 | assert.same result, "hello" | ||
| 91 | |||
| 92 | it "should work with is_ast check", -> | ||
| 93 | macro safe_add = (a, b) -> | ||
| 94 | error "expected numbers" unless $is_ast Num, a | ||
| 95 | error "expected numbers" unless $is_ast Num, b | ||
| 96 | "#{a} + #{b}" | ||
| 97 | |||
| 98 | result = $safe_add 10, 20 | ||
| 99 | assert.same result, 30 | ||
| 100 | |||
| 101 | it "should support macro generating macro", -> | ||
| 102 | macro Enum = (...) -> | ||
| 103 | items = {...} | ||
| 104 | itemSet = {item, true for item in *items} | ||
| 105 | (item) -> | ||
| 106 | error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item] | ||
| 107 | "\"#{item}\"" | ||
| 108 | |||
| 109 | macro Color = $Enum( | ||
| 110 | Red | ||
| 111 | Green | ||
| 112 | Blue | ||
| 113 | ) | ||
| 114 | |||
| 115 | result = $Color Red | ||
| 116 | assert.same result, "Red" | ||
| 117 | |||
| 118 | it "should handle complex macro logic", -> | ||
| 119 | macro smart_print = (items) -> | ||
| 120 | "print(#{table.concat [item for item in *items], ', ')})" | ||
| 121 | |||
| 122 | $smart_print {"hello", "world", 123} | ||
| 123 | |||
| 124 | it "should work with table manipulation", -> | ||
| 125 | macro create_table = (...) -> | ||
| 126 | items = {...} | ||
| 127 | "{#{table.concat items, ', '}}" | ||
| 128 | |||
| 129 | result = $create_table "1", "2", "3" | ||
| 130 | assert.same result, {"1", "2", "3"} | ||
| 131 | |||
| 132 | it "should support string concatenation in macro", -> | ||
| 133 | macro concat = (...) -> | ||
| 134 | args = {...} | ||
| 135 | res = {} | ||
| 136 | for arg in *args | ||
| 137 | table.insert res, tostring arg | ||
| 138 | "'" .. table.concat(res, " .. ") .. "'" | ||
| 139 | |||
| 140 | result = $concat "hello", "world" | ||
| 141 | assert.same result, "helloworld" | ||
diff --git a/spec/inputs/test/chaining_comparison_spec.yue b/spec/inputs/test/chaining_comparison_spec.yue new file mode 100644 index 0000000..f86cf5f --- /dev/null +++ b/spec/inputs/test/chaining_comparison_spec.yue | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | describe "chaining comparison", -> | ||
| 2 | it "should support simple chaining", -> | ||
| 3 | assert.is_true 1 < 2 < 3 | ||
| 4 | assert.is_true 1 <= 2 <= 3 | ||
| 5 | assert.is_true 3 > 2 > 1 | ||
| 6 | assert.is_true 3 >= 2 >= 1 | ||
| 7 | |||
| 8 | it "should support complex chaining", -> | ||
| 9 | assert.is_true 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 ~= 5 | ||
| 10 | |||
| 11 | it "should work with variables", -> | ||
| 12 | a = 5 | ||
| 13 | assert.is_true 1 <= a <= 10 | ||
| 14 | assert.is_true a >= 3 | ||
| 15 | assert.is_true a <= 10 | ||
| 16 | |||
| 17 | it "should handle mixed comparisons", -> | ||
| 18 | x = 5 | ||
| 19 | assert.is_true 1 < x < 10 | ||
| 20 | assert.is_true 1 <= x <= 5 | ||
| 21 | |||
| 22 | it "should work with string comparisons", -> | ||
| 23 | assert.is_true "a" < "b" < "c" | ||
| 24 | assert.is_true "a" <= "b" <= "c" | ||
| 25 | |||
| 26 | it "should handle edge cases", -> | ||
| 27 | assert.is_true 0 <= 0 <= 0 | ||
| 28 | assert.is_true -5 < 0 < 5 | ||
| 29 | |||
| 30 | it "should work in expressions", -> | ||
| 31 | result = if 1 < 2 < 3 | ||
| 32 | "yes" | ||
| 33 | else | ||
| 34 | "no" | ||
| 35 | assert.same result, "yes" | ||
| 36 | |||
| 37 | it "should support != operator", -> | ||
| 38 | assert.is_true 1 != 2 != 3 | ||
| 39 | assert.is_true 1 ~= 2 ~= 3 | ||
| 40 | |||
| 41 | it "should handle boolean results", -> | ||
| 42 | assert.is_true 1 < 2 < 3 | ||
| 43 | assert.is_false 3 < 2 < 1 | ||
| 44 | |||
| 45 | it "should work with function calls", -> | ||
| 46 | v = (x) -> x | ||
| 47 | assert.is_true v(1) < v(2) < v(3) | ||
| 48 | |||
| 49 | it "should handle negation", -> | ||
| 50 | assert.is_true -10 < -5 < 0 | ||
| 51 | |||
| 52 | it "should support mixed operators", -> | ||
| 53 | assert.is_true 1 < 2 <= 2 < 3 | ||
| 54 | assert.is_true 3 > 2 >= 2 > 1 | ||
diff --git a/spec/inputs/test/close_attribute_spec.yue b/spec/inputs/test/close_attribute_spec.yue new file mode 100644 index 0000000..2354df7 --- /dev/null +++ b/spec/inputs/test/close_attribute_spec.yue | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | describe "close attribute", -> | ||
| 2 | it "should declare close variable", -> | ||
| 3 | closed = false | ||
| 4 | do | ||
| 5 | close _ = <close>: -> closed = true | ||
| 6 | assert.is_true closed | ||
| 7 | |||
| 8 | it "should work with metatable syntax", -> | ||
| 9 | called = false | ||
| 10 | do | ||
| 11 | close _ = <close>: -> called = true | ||
| 12 | assert.is_true called | ||
| 13 | |||
| 14 | it "should handle multiple close scopes", -> | ||
| 15 | order = [] | ||
| 16 | do | ||
| 17 | close first = <close>: -> table.insert order, "first" | ||
| 18 | close second = <close>: -> table.insert order, "second" | ||
| 19 | assert.same order, {"second", "first"} | ||
| 20 | |||
| 21 | it "should work with resources", -> | ||
| 22 | resource_opened = false | ||
| 23 | resource_closed = false | ||
| 24 | |||
| 25 | do | ||
| 26 | resource_opened = true | ||
| 27 | close _ = <close>: -> resource_closed = true | ||
| 28 | |||
| 29 | assert.is_true resource_opened | ||
| 30 | assert.is_true resource_closed | ||
| 31 | |||
| 32 | it "should support close in function", -> | ||
| 33 | closed = false | ||
| 34 | fn = -> | ||
| 35 | close _ = <close>: -> closed = true | ||
| 36 | return "result" | ||
| 37 | |||
| 38 | result = fn! | ||
| 39 | assert.same result, "result" | ||
| 40 | assert.is_true closed | ||
| 41 | |||
| 42 | it "should work with fat arrow", -> | ||
| 43 | closed = false | ||
| 44 | obj = | ||
| 45 | value: 10 | ||
| 46 | close_method: <close>: => | ||
| 47 | closed = true | ||
| 48 | |||
| 49 | do | ||
| 50 | close _ = obj | ||
| 51 | |||
| 52 | assert.is_true closed | ||
| 53 | |||
| 54 | it "should handle nested close scopes", -> | ||
| 55 | outer_closed = false | ||
| 56 | inner_closed = false | ||
| 57 | |||
| 58 | do | ||
| 59 | close outer = <close>: -> outer_closed = true | ||
| 60 | do | ||
| 61 | close inner = <close>: -> inner_closed = true | ||
| 62 | |||
| 63 | assert.is_true inner_closed | ||
| 64 | assert.is_true outer_closed | ||
| 65 | |||
| 66 | it "should work with conditional close", -> | ||
| 67 | closed = false | ||
| 68 | should_close = true | ||
| 69 | |||
| 70 | if should_close | ||
| 71 | close _ = <close>: -> closed = true | ||
| 72 | |||
| 73 | assert.is_true closed | ||
| 74 | |||
| 75 | it "should support close in loop", -> | ||
| 76 | closed_count = 0 | ||
| 77 | for i = 1, 3 | ||
| 78 | do | ||
| 79 | close _ = <close>: -> closed_count += 1 | ||
| 80 | |||
| 81 | assert.same closed_count, 3 | ||
| 82 | |||
| 83 | it "should work with table destructuring", -> | ||
| 84 | closed = false | ||
| 85 | tb = {close: <close>: -> closed = true} | ||
| 86 | do | ||
| 87 | {:close} = tb | ||
| 88 | assert.is_true closed | ||
| 89 | |||
| 90 | it "should handle close with return value", -> | ||
| 91 | closed = false | ||
| 92 | fn = -> | ||
| 93 | close _ = <close>: -> closed = true | ||
| 94 | return 42 | ||
| 95 | |||
| 96 | result = fn! | ||
| 97 | assert.same result, 42 | ||
| 98 | assert.is_true closed | ||
| 99 | |||
| 100 | it "should work with error handling", -> | ||
| 101 | closed = false | ||
| 102 | error_thrown = false | ||
| 103 | |||
| 104 | do | ||
| 105 | close _ = <close>: -> closed = true | ||
| 106 | error_thrown = true | ||
| 107 | |||
| 108 | assert.is_true closed | ||
| 109 | assert.is_true error_thrown | ||
| 110 | |||
| 111 | it "should support close in varargs function", -> | ||
| 112 | closed = false | ||
| 113 | fn = (...) -> | ||
| 114 | close _ = <close>: -> closed = true | ||
| 115 | {...} | ||
| 116 | |||
| 117 | result = fn 1, 2, 3 | ||
| 118 | assert.same result, {1, 2, 3} | ||
| 119 | assert.is_true closed | ||
| 120 | |||
| 121 | it "should work with multiple variables", -> | ||
| 122 | first_closed = false | ||
| 123 | second_closed = false | ||
| 124 | |||
| 125 | do | ||
| 126 | close first = <close>: -> first_closed = true | ||
| 127 | close second = <close>: -> second_closed = true | ||
| 128 | |||
| 129 | assert.is_true first_closed | ||
| 130 | assert.is_true second_closed | ||
| 131 | |||
| 132 | it "should handle close in try block", -> | ||
| 133 | closed = false | ||
| 134 | success = false | ||
| 135 | |||
| 136 | success = try | ||
| 137 | close _ = <close>: -> closed = true | ||
| 138 | true | ||
| 139 | catch err | ||
| 140 | false | ||
| 141 | |||
| 142 | assert.is_true success | ||
| 143 | assert.is_true closed | ||
diff --git a/spec/inputs/test/const_attribute_spec.yue b/spec/inputs/test/const_attribute_spec.yue new file mode 100644 index 0000000..e3cc638 --- /dev/null +++ b/spec/inputs/test/const_attribute_spec.yue | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | describe "const attribute", -> | ||
| 2 | it "should declare const variable", -> | ||
| 3 | const a = 123 | ||
| 4 | assert.same a, 123 | ||
| 5 | |||
| 6 | it "should prevent reassignment", -> | ||
| 7 | const b = 456 | ||
| 8 | -- b = 789 -- This should cause error | ||
| 9 | assert.same b, 456 | ||
| 10 | |||
| 11 | it "should work with strings", -> | ||
| 12 | const name = "test" | ||
| 13 | assert.same name, "test" | ||
| 14 | |||
| 15 | it "should support const with destructuring", -> | ||
| 16 | tb = {a: 1, b: 2, c: 3, d: 4} | ||
| 17 | const {:a, :b, c, d} = tb | ||
| 18 | assert.same a, 1 | ||
| 19 | assert.same b, 2 | ||
| 20 | assert.same c, 3 | ||
| 21 | assert.same d, 4 | ||
| 22 | |||
| 23 | it "should handle nested const", -> | ||
| 24 | const nested = { | ||
| 25 | inner: {value: 10} | ||
| 26 | } | ||
| 27 | assert.same nested.inner.value, 10 | ||
| 28 | |||
| 29 | it "should work with arrays", -> | ||
| 30 | const items = [1, 2, 3] | ||
| 31 | assert.same items[1], 1 | ||
| 32 | |||
| 33 | it "should support const in function scope", -> | ||
| 34 | fn = -> | ||
| 35 | const local_const = "local" | ||
| 36 | local_const | ||
| 37 | |||
| 38 | result = fn! | ||
| 39 | assert.same result, "local" | ||
| 40 | |||
| 41 | it "should work with multiple const declarations", -> | ||
| 42 | const x = 1 | ||
| 43 | const y = 2 | ||
| 44 | const z = 3 | ||
| 45 | assert.same x + y + z, 6 | ||
| 46 | |||
| 47 | it "should handle const functions", -> | ||
| 48 | const add = (a, b) -> a + b | ||
| 49 | assert.same add 5, 10, 15 | ||
| 50 | |||
| 51 | it "should work with const tables", -> | ||
| 52 | const config = { | ||
| 53 | host: "localhost" | ||
| 54 | port: 8080 | ||
| 55 | } | ||
| 56 | assert.same config.host, "localhost" | ||
| 57 | assert.same config.port, 8080 | ||
| 58 | |||
| 59 | it "should support global const", -> | ||
| 60 | global const GLOBAL_CONST = 999 | ||
| 61 | assert.same GLOBAL_CONST, 999 | ||
| 62 | |||
| 63 | it "should work with boolean const", -> | ||
| 64 | const flag = true | ||
| 65 | const another = false | ||
| 66 | assert.is_true flag | ||
| 67 | assert.is_false another | ||
| 68 | |||
| 69 | it "should handle nil const", -> | ||
| 70 | const nil_value = nil | ||
| 71 | assert.same nil_value, nil | ||
| 72 | |||
| 73 | it "should work with expressions", -> | ||
| 74 | const calculated = 10 + 20 | ||
| 75 | assert.same calculated, 30 | ||
| 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", -> | ||
| 85 | const multiplier = 2 | ||
| 86 | items = [1, 2, 3] | ||
| 87 | result = [item * multiplier for item in *items] | ||
| 88 | assert.same result, {2, 4, 6} | ||
| 89 | |||
| 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", -> | ||
| 100 | const complex = { | ||
| 101 | data: [1, 2, 3] | ||
| 102 | nested: { | ||
| 103 | key: "value" | ||
| 104 | } | ||
| 105 | } | ||
| 106 | assert.same complex.data[1], 1 | ||
| 107 | assert.same complex.nested.key, "value" | ||
diff --git a/spec/inputs/test/do_statement_spec.yue b/spec/inputs/test/do_statement_spec.yue new file mode 100644 index 0000000..0adad20 --- /dev/null +++ b/spec/inputs/test/do_statement_spec.yue | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | describe "do statement", -> | ||
| 2 | it "should create new scope", -> | ||
| 3 | x = 10 | ||
| 4 | do | ||
| 5 | local x = 20 | ||
| 6 | assert.same x, 20 | ||
| 7 | assert.same x, 10 | ||
| 8 | |||
| 9 | it "should return value from do block", -> | ||
| 10 | result = do | ||
| 11 | x = 5 | ||
| 12 | x * 2 | ||
| 13 | assert.same result, 10 | ||
| 14 | |||
| 15 | it "should work with multiple statements", -> | ||
| 16 | result = do | ||
| 17 | a = 1 | ||
| 18 | b = 2 | ||
| 19 | c = 3 | ||
| 20 | a + b + c | ||
| 21 | assert.same result, 6 | ||
| 22 | |||
| 23 | it "should handle nested do blocks", -> | ||
| 24 | result = do | ||
| 25 | x = 10 | ||
| 26 | y = do | ||
| 27 | z = 5 | ||
| 28 | z * 2 | ||
| 29 | x + y | ||
| 30 | assert.same result, 20 | ||
| 31 | |||
| 32 | it "should support conditional in do block", -> | ||
| 33 | result = do | ||
| 34 | value = 5 | ||
| 35 | if value > 3 | ||
| 36 | value * 2 | ||
| 37 | else | ||
| 38 | value | ||
| 39 | assert.same result, 10 | ||
| 40 | |||
| 41 | it "should work with loops in do block", -> | ||
| 42 | result = do | ||
| 43 | sum = 0 | ||
| 44 | for i = 1, 5 | ||
| 45 | sum += i | ||
| 46 | sum | ||
| 47 | assert.same result, 15 | ||
| 48 | |||
| 49 | it "should handle table operations", -> | ||
| 50 | result = do | ||
| 51 | tb = {1, 2, 3} | ||
| 52 | table.insert tb, 4 | ||
| 53 | #tb | ||
| 54 | assert.same result, 4 | ||
| 55 | |||
| 56 | it "should work with function definition", -> | ||
| 57 | result = do | ||
| 58 | fn = (x) -> x * 2 | ||
| 59 | fn 5 | ||
| 60 | assert.same result, 10 | ||
| 61 | |||
| 62 | it "should support variable shadowing", -> | ||
| 63 | x = "outer" | ||
| 64 | result = do | ||
| 65 | x = "inner" | ||
| 66 | x | ||
| 67 | assert.same result, "inner" | ||
| 68 | assert.same x, "outer" | ||
| 69 | |||
| 70 | it "should work with method calls", -> | ||
| 71 | obj = | ||
| 72 | value: 10 | ||
| 73 | double: => @value * 2 | ||
| 74 | |||
| 75 | result = do | ||
| 76 | with obj | ||
| 77 | \double! | ||
| 78 | assert.same result, 20 | ||
| 79 | |||
| 80 | it "should handle comprehensions in do block", -> | ||
| 81 | result = do | ||
| 82 | items = [1, 2, 3, 4, 5] | ||
| 83 | [item * 2 for item in *items] | ||
| 84 | assert.same result, {2, 4, 6, 8, 10} | ||
| 85 | |||
| 86 | it "should work with try-catch", -> | ||
| 87 | result = do | ||
| 88 | success = try | ||
| 89 | error "test error" | ||
| 90 | false | ||
| 91 | catch err | ||
| 92 | true | ||
| 93 | assert.is_true success | ||
| 94 | |||
| 95 | it "should support return statement", -> | ||
| 96 | fn = -> | ||
| 97 | do | ||
| 98 | x = 10 | ||
| 99 | return x * 2 | ||
| 100 | "never reached" | ||
| 101 | |||
| 102 | result = fn! | ||
| 103 | assert.same result, 20 | ||
| 104 | |||
| 105 | it "should work with assignment", -> | ||
| 106 | result = do | ||
| 107 | a, b, c = 1, 2, 3 | ||
| 108 | a + b + c | ||
| 109 | assert.same result, 6 | ||
| 110 | |||
| 111 | it "should handle destructuring", -> | ||
| 112 | result = do | ||
| 113 | tb = {x: 10, y: 20} | ||
| 114 | {:x, :y} = tb | ||
| 115 | x + y | ||
| 116 | assert.same result, 30 | ||
| 117 | |||
| 118 | it "should work with string interpolation", -> | ||
| 119 | name = "world" | ||
| 120 | result = do | ||
| 121 | greeting = "hello" | ||
| 122 | "#{greeting} #{name}" | ||
| 123 | assert.same result, "hello world" | ||
| 124 | |||
| 125 | it "should support implicit return", -> | ||
| 126 | result = do | ||
| 127 | value = 42 | ||
| 128 | assert.same result, 42 | ||
| 129 | |||
| 130 | it "should handle empty do block", -> | ||
| 131 | result = do | ||
| 132 | assert.same result, nil | ||
| 133 | |||
| 134 | it "should work with backcalls", -> | ||
| 135 | result = do | ||
| 136 | items = [1, 2, 3] | ||
| 137 | (x) <- map _, items | ||
| 138 | x * 2 | ||
| 139 | assert.same result, {2, 4, 6} | ||
diff --git a/spec/inputs/test/functions_advanced_spec.yue b/spec/inputs/test/functions_advanced_spec.yue new file mode 100644 index 0000000..d0e0cf5 --- /dev/null +++ b/spec/inputs/test/functions_advanced_spec.yue | |||
| @@ -0,0 +1,158 @@ | |||
| 1 | describe "advanced functions", -> | ||
| 2 | it "should support fat arrow with self", -> | ||
| 3 | obj = | ||
| 4 | value: 10 | ||
| 5 | getValue: => @value | ||
| 6 | |||
| 7 | assert.same obj\getValue!, 10 | ||
| 8 | |||
| 9 | it "should work with argument defaults", -> | ||
| 10 | fn = (name = "something", height = 100) -> | ||
| 11 | "#{name}, #{height}" | ||
| 12 | |||
| 13 | assert.same fn!, "something, 100" | ||
| 14 | assert.same fn("test"), "test, 100" | ||
| 15 | assert.same fn("test", 50), "test, 50" | ||
| 16 | |||
| 17 | it "should handle defaults with previous arguments", -> | ||
| 18 | fn = (x = 100, y = x + 1000) -> | ||
| 19 | x + y | ||
| 20 | |||
| 21 | assert.same fn!, 1200 | ||
| 22 | assert.same fn(50), 1150 | ||
| 23 | |||
| 24 | it "should work with multi-line arguments", -> | ||
| 25 | my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | ||
| 26 | result = my_func 5, 4, 3, | ||
| 27 | 8, 9, 10 | ||
| 28 | assert.same result, 39 | ||
| 29 | |||
| 30 | it "should support nested function calls", -> | ||
| 31 | result = my_func 5, 6, 7, | ||
| 32 | 6, another_func 6, 7, 8, | ||
| 33 | 9, 1, 2, | ||
| 34 | 5, 4 | ||
| 35 | |||
| 36 | another_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | ||
| 37 | my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f | ||
| 38 | |||
| 39 | assert.same result, 52 | ||
| 40 | |||
| 41 | it "should handle implicit return", -> | ||
| 42 | sum = (x, y) -> x + y | ||
| 43 | assert.same sum 10, 20, 30 | ||
| 44 | |||
| 45 | it "should work with explicit return", -> | ||
| 46 | difference = (x, y) -> return x - y | ||
| 47 | assert.same difference 20, 10, 10 | ||
| 48 | |||
| 49 | it "should support multiple return values", -> | ||
| 50 | mystery = (x, y) -> x + y, x - y | ||
| 51 | a, b = mystery 10, 20 | ||
| 52 | assert.same a, 30 | ||
| 53 | assert.same b, -10 | ||
| 54 | |||
| 55 | it "should work with function as argument", -> | ||
| 56 | apply = (fn, x, y) -> fn x, y | ||
| 57 | result = apply ((a, b) -> a + b), 5, 10 | ||
| 58 | assert.same result, 15 | ||
| 59 | |||
| 60 | it "should handle function returning function", -> | ||
| 61 | create_adder = (x) -> (y) -> x + y | ||
| 62 | add_five = create_adder 5 | ||
| 63 | assert.same add_five(10), 15 | ||
| 64 | |||
| 65 | it "should support immediately invoked function", -> | ||
| 66 | result = ((x) -> x * 2) 5 | ||
| 67 | assert.same result, 10 | ||
| 68 | |||
| 69 | it "should work with varargs", -> | ||
| 70 | sum_all = (...) -> | ||
| 71 | total = 0 | ||
| 72 | for i = 1, select '#', ... | ||
| 73 | total += select(i, ...) if type(select(i, ...)) == "number" | ||
| 74 | total | ||
| 75 | |||
| 76 | assert.same sum_all(1, 2, 3, 4, 5), 15 | ||
| 77 | |||
| 78 | it "should handle named varargs", -> | ||
| 79 | fn = (...t) -> | ||
| 80 | count = 0 | ||
| 81 | for i = 1, t.n | ||
| 82 | count += 1 | ||
| 83 | count | ||
| 84 | |||
| 85 | assert.same fn(1, 2, 3), 3 | ||
| 86 | |||
| 87 | it "should support prefixed return", -> | ||
| 88 | findValue: "not found" -> | ||
| 89 | items = [1, 2, 3] | ||
| 90 | for item in *items | ||
| 91 | if item == 5 | ||
| 92 | return item | ||
| 93 | |||
| 94 | result = findValue! | ||
| 95 | assert.same result, "not found" | ||
| 96 | |||
| 97 | it "should work with parameter destructuring", -> | ||
| 98 | fn = (:a, :b, :c) -> | ||
| 99 | a + b + c | ||
| 100 | |||
| 101 | assert.same fn(a: 1, b: 2, c: 3), 6 | ||
| 102 | |||
| 103 | it "should handle default values in destructuring", -> | ||
| 104 | fn = ({a: a1 = 123, :b = 'abc'}) -> | ||
| 105 | a1 .. " " .. b | ||
| 106 | |||
| 107 | assert.same fn{}, "123 abc" | ||
| 108 | assert.same fn({a: 456}), "456 abc" | ||
| 109 | |||
| 110 | it "should support empty function body", -> | ||
| 111 | empty_fn = -> | ||
| 112 | assert.same empty_fn!, nil | ||
| 113 | |||
| 114 | it "should work with function in table", -> | ||
| 115 | tb = | ||
| 116 | value: 10 | ||
| 117 | double: => @value * 2 | ||
| 118 | |||
| 119 | assert.same tb\double!, 20 | ||
| 120 | |||
| 121 | it "should handle function with no arguments", -> | ||
| 122 | fn = -> | ||
| 123 | "result" | ||
| 124 | |||
| 125 | assert.same fn!, "result" | ||
| 126 | assert.same fn(), "result" | ||
| 127 | |||
| 128 | it "should support calling function with !", -> | ||
| 129 | fn = -> 42 | ||
| 130 | assert.same fn!, 42 | ||
| 131 | |||
| 132 | it "should work with nested functions", -> | ||
| 133 | outer = (x) -> | ||
| 134 | inner = (y) -> x + y | ||
| 135 | inner | ||
| 136 | |||
| 137 | add_five = outer 5 | ||
| 138 | assert.same add_five(10), 15 | ||
| 139 | |||
| 140 | it "should handle function in expression", -> | ||
| 141 | result = if ((x) -> x > 10) 15 | ||
| 142 | "large" | ||
| 143 | else | ||
| 144 | "small" | ||
| 145 | assert.same result, "large" | ||
| 146 | |||
| 147 | it "should support function as return value", -> | ||
| 148 | get_operation = (op) -> | ||
| 149 | switch op | ||
| 150 | when "add" | ||
| 151 | (a, b) -> a + b | ||
| 152 | when "subtract" | ||
| 153 | (a, b) -> a - b | ||
| 154 | else | ||
| 155 | -> 0 | ||
| 156 | |||
| 157 | add = get_operation "add" | ||
| 158 | assert.same add 5, 3, 8 | ||
diff --git a/spec/inputs/test/if_assignment_spec.yue b/spec/inputs/test/if_assignment_spec.yue new file mode 100644 index 0000000..1ce028e --- /dev/null +++ b/spec/inputs/test/if_assignment_spec.yue | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | describe "if assignment", -> | ||
| 2 | it "should assign and check truthy value", -> | ||
| 3 | obj = find_user: (name) -> name == "valid" and {name: name} or nil | ||
| 4 | if user := obj\find_user "valid" | ||
| 5 | assert.same user.name, "valid" | ||
| 6 | |||
| 7 | it "should not enter block when nil", -> | ||
| 8 | obj = find_user: -> nil | ||
| 9 | if user := obj\find_user! | ||
| 10 | assert.is_true false -- should not reach | ||
| 11 | else | ||
| 12 | assert.is_true true | ||
| 13 | |||
| 14 | it "should work with elseif", -> | ||
| 15 | get_value = (key) -> | ||
| 16 | switch key | ||
| 17 | when "a" then 1 | ||
| 18 | when "b" then 2 | ||
| 19 | else nil | ||
| 20 | |||
| 21 | result = nil | ||
| 22 | if val := get_value "c" | ||
| 23 | result = "c: #{val}" | ||
| 24 | elseif val := get_value "b" | ||
| 25 | result = "b: #{val}" | ||
| 26 | else | ||
| 27 | result = "no match" | ||
| 28 | assert.same result, "b: 2" | ||
| 29 | |||
| 30 | it "should scope variable to if block", -> | ||
| 31 | if x := 10 | ||
| 32 | assert.same x, 10 | ||
| 33 | -- x should not be accessible here | ||
| 34 | assert.is_true true | ||
| 35 | |||
| 36 | it "should work with multiple return values", -> | ||
| 37 | fn = -> true, "success" | ||
| 38 | if success, result := fn! | ||
| 39 | assert.is_true success | ||
| 40 | assert.same result, "success" | ||
| 41 | |||
| 42 | it "should work with table destructuring", -> | ||
| 43 | get_point = -> {x: 10, y: 20} | ||
| 44 | if {:x, :y} := get_point! | ||
| 45 | assert.same x, 10 | ||
| 46 | assert.same y, 20 | ||
| 47 | |||
| 48 | it "should work with array destructuring", -> | ||
| 49 | get_coords = -> [1, 2, 3] | ||
| 50 | if [a, b, c] := get_coords! | ||
| 51 | assert.same a, 1 | ||
| 52 | assert.same b, 2 | ||
| 53 | assert.same c, 3 | ||
| 54 | |||
| 55 | it "should chain multiple assignments", -> | ||
| 56 | if a := 1 | ||
| 57 | if b := a + 1 | ||
| 58 | assert.same b, 2 | ||
| 59 | |||
| 60 | it "should work in expression context", -> | ||
| 61 | get_value = (x) -> if x > 0 then x else nil | ||
| 62 | result = if val := get_value 5 | ||
| 63 | val * 2 | ||
| 64 | else | ||
| 65 | 0 | ||
| 66 | assert.same result, 10 | ||
| 67 | |||
| 68 | it "should work with os.getenv", -> | ||
| 69 | -- test with environment variable | ||
| 70 | if path := os.getenv "PATH" | ||
| 71 | assert.is_true type(path) == "string" | ||
| 72 | else | ||
| 73 | assert.is_true true | ||
| 74 | |||
| 75 | it "should support table access", -> | ||
| 76 | tb = {key: "value"} | ||
| 77 | if val := tb.key | ||
| 78 | assert.same val, "value" | ||
| 79 | |||
| 80 | it "should work with function call results", -> | ||
| 81 | fn = -> "result" | ||
| 82 | if s := fn! | ||
| 83 | assert.same s, "result" | ||
| 84 | |||
| 85 | it "should handle false values", -> | ||
| 86 | if val := false | ||
| 87 | assert.is_true false -- should not enter | ||
| 88 | else | ||
| 89 | assert.is_true true | ||
diff --git a/spec/inputs/test/implicit_object_spec.yue b/spec/inputs/test/implicit_object_spec.yue new file mode 100644 index 0000000..cea926e --- /dev/null +++ b/spec/inputs/test/implicit_object_spec.yue | |||
| @@ -0,0 +1,164 @@ | |||
| 1 | describe "implicit object", -> | ||
| 2 | it "should create list with asterisk", -> | ||
| 3 | list = | ||
| 4 | * 1 | ||
| 5 | * 2 | ||
| 6 | * 3 | ||
| 7 | assert.same list, {1, 2, 3} | ||
| 8 | |||
| 9 | it "should create list with dash", -> | ||
| 10 | items = | ||
| 11 | - "a" | ||
| 12 | - "b" | ||
| 13 | - "c" | ||
| 14 | assert.same items, {"a", "b", "c"} | ||
| 15 | |||
| 16 | it "should work with function call", -> | ||
| 17 | results = [] | ||
| 18 | fn = | ||
| 19 | * 1 | ||
| 20 | * 2 | ||
| 21 | * 3 | ||
| 22 | |||
| 23 | for item in *fn | ||
| 24 | table.insert results, item | ||
| 25 | |||
| 26 | assert.same results, {1, 2, 3} | ||
| 27 | |||
| 28 | it "should support nested implicit objects", -> | ||
| 29 | tb = | ||
| 30 | name: "test" | ||
| 31 | |||
| 32 | values: | ||
| 33 | - "a" | ||
| 34 | - "b" | ||
| 35 | - "c" | ||
| 36 | |||
| 37 | objects: | ||
| 38 | - name: "first" | ||
| 39 | value: 1 | ||
| 40 | - name: "second" | ||
| 41 | value: 2 | ||
| 42 | |||
| 43 | assert.same tb.values, {"a", "b", "c"} | ||
| 44 | assert.same tb.objects[1].name, "first" | ||
| 45 | assert.same tb.objects[2].value, 2 | ||
| 46 | |||
| 47 | it "should work with return statement", -> | ||
| 48 | fn = -> | ||
| 49 | return | ||
| 50 | * 1 | ||
| 51 | * 2 | ||
| 52 | * 3 | ||
| 53 | |||
| 54 | assert.same fn!, {1, 2, 3} | ||
| 55 | |||
| 56 | it "should handle mixed content", -> | ||
| 57 | tb = | ||
| 58 | key: "value" | ||
| 59 | |||
| 60 | items: | ||
| 61 | - 1 | ||
| 62 | - 2 | ||
| 63 | |||
| 64 | other: "data" | ||
| 65 | |||
| 66 | assert.same tb.key, "value" | ||
| 67 | assert.same tb.items, {1, 2} | ||
| 68 | assert.same tb.other, "data" | ||
| 69 | |||
| 70 | it "should work in assignment", -> | ||
| 71 | list = | ||
| 72 | * "x" | ||
| 73 | * "y" | ||
| 74 | * "z" | ||
| 75 | |||
| 76 | assert.same list, {"x", "y", "z"} | ||
| 77 | |||
| 78 | it "should support nested structures with asterisk", -> | ||
| 79 | tb = | ||
| 80 | * 1 | ||
| 81 | * 2 | ||
| 82 | nested: | ||
| 83 | * 3 | ||
| 84 | * 4 | ||
| 85 | |||
| 86 | assert.same tb[1], 1 | ||
| 87 | assert.same tb[2], 2 | ||
| 88 | assert.same tb.nested, {3, 4} | ||
| 89 | |||
| 90 | it "should handle implicit object in tables", -> | ||
| 91 | tb = { | ||
| 92 | name: "test" | ||
| 93 | |||
| 94 | list: | ||
| 95 | - 1 | ||
| 96 | - 2 | ||
| 97 | |||
| 98 | value: 42 | ||
| 99 | } | ||
| 100 | |||
| 101 | assert.same tb.list, {1, 2} | ||
| 102 | |||
| 103 | it "should work with expressions", -> | ||
| 104 | x = 10 | ||
| 105 | list = | ||
| 106 | * x + 1 | ||
| 107 | * x + 2 | ||
| 108 | * x + 3 | ||
| 109 | |||
| 110 | assert.same list, {11, 12, 13} | ||
| 111 | |||
| 112 | it "should support method calls in implicit object", -> | ||
| 113 | tb = | ||
| 114 | name: "test" | ||
| 115 | items: | ||
| 116 | - name: "item1" | ||
| 117 | getName: => @name | ||
| 118 | - name: "item2" | ||
| 119 | getName: => @name | ||
| 120 | |||
| 121 | assert.same tb.items[1]\getName!, "item1" | ||
| 122 | assert.same tb.items[2]\getName!, "item2" | ||
| 123 | |||
| 124 | it "should work with complex nested structures", -> | ||
| 125 | config = | ||
| 126 | database: | ||
| 127 | host: "localhost" | ||
| 128 | ports: | ||
| 129 | - 8080 | ||
| 130 | - 8081 | ||
| 131 | - 8082 | ||
| 132 | |||
| 133 | servers: | ||
| 134 | - name: "server1" | ||
| 135 | port: 8080 | ||
| 136 | - name: "server2" | ||
| 137 | port: 8081 | ||
| 138 | |||
| 139 | assert.same config.database.ports, {8080, 8081, 8082} | ||
| 140 | assert.same config.servers[1].name, "server1" | ||
| 141 | |||
| 142 | it "should handle empty implicit object", -> | ||
| 143 | tb = | ||
| 144 | items: | ||
| 145 | - | ||
| 146 | |||
| 147 | assert.same tb.items, {nil} | ||
| 148 | |||
| 149 | it "should work in function arguments", -> | ||
| 150 | fn = (items) -> #items | ||
| 151 | result = fn | ||
| 152 | * 1 | ||
| 153 | * 2 | ||
| 154 | * 3 | ||
| 155 | assert.same result, 3 | ||
| 156 | |||
| 157 | it "should support mixed asterisk and dash", -> | ||
| 158 | tb = | ||
| 159 | values: | ||
| 160 | * 1 | ||
| 161 | - 2 | ||
| 162 | * 3 | ||
| 163 | |||
| 164 | assert.same tb.values, {1, 2, 3} | ||
diff --git a/spec/inputs/test/in_expression_spec.yue b/spec/inputs/test/in_expression_spec.yue new file mode 100644 index 0000000..c1f4099 --- /dev/null +++ b/spec/inputs/test/in_expression_spec.yue | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | describe "in expression", -> | ||
| 2 | it "should check value in table", -> | ||
| 3 | items = {1, 2, 3, 4, 5} | ||
| 4 | assert.is_true 3 in items | ||
| 5 | assert.is_false 10 in items | ||
| 6 | |||
| 7 | it "should work with strings", -> | ||
| 8 | chars = {"a", "b", "c"} | ||
| 9 | assert.is_true "b" in chars | ||
| 10 | assert.is_false "z" in chars | ||
| 11 | |||
| 12 | it "should check keys in table", -> | ||
| 13 | obj = {x: 1, y: 2, z: 3} | ||
| 14 | assert.is_true "x" in obj | ||
| 15 | assert.is_true "y" in obj | ||
| 16 | assert.is_false "w" in obj | ||
| 17 | |||
| 18 | it "should work with mixed types", -> | ||
| 19 | items = {1, "two", true, nil} | ||
| 20 | assert.is_true 1 in items | ||
| 21 | assert.is_true "two" in items | ||
| 22 | assert.is_true true in items | ||
| 23 | assert.is_false false in items | ||
| 24 | |||
| 25 | it "should handle empty table", -> | ||
| 26 | empty = {} | ||
| 27 | assert.is_false 1 in empty | ||
| 28 | assert.is_false "test" in empty | ||
| 29 | |||
| 30 | it "should work in conditional", -> | ||
| 31 | items = {1, 2, 3} | ||
| 32 | result = if 2 in items | ||
| 33 | "found" | ||
| 34 | else | ||
| 35 | "not found" | ||
| 36 | assert.same result, "found" | ||
| 37 | |||
| 38 | it "should support negation", -> | ||
| 39 | items = {1, 2, 3} | ||
| 40 | assert.is_true not (4 in items) | ||
| 41 | assert.is_false not (2 in items) | ||
| 42 | |||
| 43 | it "should work with nested tables", -> | ||
| 44 | nested = {{1, 2}, {3, 4}, {5, 6}} | ||
| 45 | assert.is_true {1, 2} in nested | ||
| 46 | assert.is_false {1, 3} in nested | ||
| 47 | |||
| 48 | it "should handle boolean values", -> | ||
| 49 | bools = {true, false} | ||
| 50 | assert.is_true true in bools | ||
| 51 | assert.is_true false in bools | ||
| 52 | |||
| 53 | it "should work in loop", -> | ||
| 54 | items = {1, 2, 3, 4, 5} | ||
| 55 | count = 0 | ||
| 56 | for i = 1, 10 | ||
| 57 | count += 1 if i in items | ||
| 58 | assert.same count, 5 | ||
| 59 | |||
| 60 | it "should support table as value", -> | ||
| 61 | key1 = {a: 1} | ||
| 62 | key2 = {b: 2} | ||
| 63 | tb = {[key1]: "first", [key2]: "second"} | ||
| 64 | |||
| 65 | -- Note: this tests table reference equality | ||
| 66 | assert.is_true key1 in tb | ||
| 67 | assert.is_true key2 in tb | ||
| 68 | |||
| 69 | it "should work with function results", -> | ||
| 70 | get_items = -> {1, 2, 3} | ||
| 71 | assert.is_true 2 in get_items! | ||
| 72 | assert.is_false 5 in get_items! | ||
| 73 | |||
| 74 | it "should handle nil in table", -> | ||
| 75 | items = {1, nil, 3} | ||
| 76 | assert.is_true nil in items | ||
| 77 | assert.is_true 1 in items | ||
| 78 | |||
| 79 | it "should work with string keys", -> | ||
| 80 | obj = {name: "test", value: 42} | ||
| 81 | assert.is_true "name" in obj | ||
| 82 | assert.is_true "value" in obj | ||
| 83 | assert.is_false "missing" in obj | ||
| 84 | |||
| 85 | it "should support complex expressions", -> | ||
| 86 | items = {1, 2, 3} | ||
| 87 | result = (2 in items) and "yes" or "no" | ||
| 88 | assert.same result, "yes" | ||
| 89 | |||
| 90 | it "should work in comprehension", -> | ||
| 91 | source = {1, 2, 3, 4, 5} | ||
| 92 | allowed = {2, 4} | ||
| 93 | result = [item for item in *source when item in allowed] | ||
| 94 | assert.same result, {2, 4} | ||
diff --git a/spec/inputs/test/multiline_args_spec.yue b/spec/inputs/test/multiline_args_spec.yue new file mode 100644 index 0000000..bbb06f9 --- /dev/null +++ b/spec/inputs/test/multiline_args_spec.yue | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | describe "multiline arguments", -> | ||
| 2 | it "should split arguments across lines", -> | ||
| 3 | sum = (a, b, c) -> a + b + c | ||
| 4 | result = sum 5, 4, 3, | ||
| 5 | 8, 9, 10 | ||
| 6 | assert.same result, 39 | ||
| 7 | |||
| 8 | it "should handle nested function calls", -> | ||
| 9 | outer = (a, b, c, d, e, f) -> a + b + c + d + e + f | ||
| 10 | result = outer 5, 6, 7, | ||
| 11 | 6, 2, 3 | ||
| 12 | assert.same result, 29 | ||
| 13 | |||
| 14 | it "should work with string arguments", -> | ||
| 15 | fn = (a, b, c, d) -> a .. b .. c .. d | ||
| 16 | result = fn "hello", | ||
| 17 | " ", "world", "!" | ||
| 18 | assert.same result, "hello world!" | ||
| 19 | |||
| 20 | it "should support table arguments", -> | ||
| 21 | fn = (a, b, c) -> {a, b, c} | ||
| 22 | result = fn {1, 2}, | ||
| 23 | {3, 4}, | ||
| 24 | {5, 6} | ||
| 25 | assert.same result, {{1, 2}, {3, 4}, {5, 6}} | ||
| 26 | |||
| 27 | it "should handle mixed types", -> | ||
| 28 | fn = (a, b, c, d) -> {a, b, c, d} | ||
| 29 | result = fn "text", | ||
| 30 | 123, | ||
| 31 | true, | ||
| 32 | nil | ||
| 33 | assert.same result, {"text", 123, true, nil} | ||
| 34 | |||
| 35 | it "should work in table literal", -> | ||
| 36 | fn = (a, b) -> a + b | ||
| 37 | result = [ | ||
| 38 | 1, 2, 3, 4, fn 4, 5, | ||
| 39 | 5, 6, | ||
| 40 | 8, 9, 10 | ||
| 41 | ] | ||
| 42 | assert.same result, {1, 2, 3, 4, 9, 8, 9, 10} | ||
| 43 | |||
| 44 | it "should handle deeply nested indentation", -> | ||
| 45 | y = [ fn 1, 2, 3, | ||
| 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 | ||
| 52 | |||
| 53 | result = y[1] | ||
| 54 | assert.same result, 22 | ||
| 55 | |||
| 56 | it "should work with conditional statements", -> | ||
| 57 | fn = (a, b, c, d, e) -> a + b + c + d + e | ||
| 58 | |||
| 59 | result = if fn 1, 2, 3, | ||
| 60 | "hello", | ||
| 61 | "world" | ||
| 62 | "yes" | ||
| 63 | else | ||
| 64 | "no" | ||
| 65 | assert.same result, "yes" | ||
| 66 | |||
| 67 | it "should support function expressions", -> | ||
| 68 | double = (x) -> x * 2 | ||
| 69 | result = double 5, | ||
| 70 | 10 | ||
| 71 | assert.same result, 20 | ||
| 72 | |||
| 73 | it "should handle chained function calls", -> | ||
| 74 | add = (a, b) -> a + b | ||
| 75 | multiply = (a, b) -> a * b | ||
| 76 | |||
| 77 | result = multiply add 1, 2, | ||
| 78 | add 3, 4 | ||
| 79 | assert.same result, 21 | ||
| 80 | |||
| 81 | it "should work with method calls", -> | ||
| 82 | obj = | ||
| 83 | value: 10 | ||
| 84 | add: (a, b) => @value + a + b | ||
| 85 | |||
| 86 | result = obj\add 5, 10, | ||
| 87 | 15 | ||
| 88 | assert.same result, 40 | ||
| 89 | |||
| 90 | it "should support many arguments", -> | ||
| 91 | sum_many = (...) -> | ||
| 92 | total = 0 | ||
| 93 | for i = 1, select '#', ... | ||
| 94 | total += select(i, ...) if type(select(i, ...)) == "number" | ||
| 95 | total | ||
| 96 | |||
| 97 | result = sum_many 1, 2, 3, | ||
| 98 | 4, 5, 6, | ||
| 99 | 7, 8, 9 | ||
| 100 | assert.same result, 45 | ||
| 101 | |||
| 102 | it "should work with return statement", -> | ||
| 103 | fn = (a, b) -> a + b | ||
| 104 | get_value = -> | ||
| 105 | return fn 10, 20, | ||
| 106 | 30 | ||
| 107 | |||
| 108 | result = get_value! | ||
| 109 | assert.same result, 60 | ||
| 110 | |||
| 111 | it "should handle default parameters", -> | ||
| 112 | fn = (a = 1, b = 2, c = 3) -> a + b + c | ||
| 113 | result = fn 10, | ||
| 114 | 20, | ||
| 115 | 30 | ||
| 116 | assert.same result, 60 | ||
| 117 | |||
| 118 | it "should work with varargs", -> | ||
| 119 | collect = (...) -> | ||
| 120 | {...} | ||
| 121 | |||
| 122 | result = collect 1, 2, | ||
| 123 | 3, 4, | ||
| 124 | 5, 6 | ||
| 125 | assert.same result, {1, 2, 3, 4, 5, 6} | ||
diff --git a/spec/inputs/test/named_varargs_spec.yue b/spec/inputs/test/named_varargs_spec.yue new file mode 100644 index 0000000..a5ab2b1 --- /dev/null +++ b/spec/inputs/test/named_varargs_spec.yue | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | describe "named varargs", -> | ||
| 2 | it "should store varargs in named table", -> | ||
| 3 | f = (...t) -> | ||
| 4 | assert.same t.n, 3 | ||
| 5 | assert.same t[1], 1 | ||
| 6 | assert.same t[2], 2 | ||
| 7 | assert.same t[3], 3 | ||
| 8 | |||
| 9 | f 1, 2, 3 | ||
| 10 | |||
| 11 | it "should handle string arguments", -> | ||
| 12 | f = (...args) -> | ||
| 13 | assert.same args.n, 3 | ||
| 14 | assert.same args[1], "a" | ||
| 15 | assert.same args[2], "b" | ||
| 16 | assert.same args[3], "c" | ||
| 17 | |||
| 18 | f "a", "b", "c" | ||
| 19 | |||
| 20 | it "should handle empty varargs", -> | ||
| 21 | f = (...t) -> | ||
| 22 | assert.same t.n, 0 | ||
| 23 | assert.same #t, 0 | ||
| 24 | |||
| 25 | f! | ||
| 26 | |||
| 27 | it "should preserve nil values", -> | ||
| 28 | f = (...args) -> | ||
| 29 | assert.same args.n, 5 | ||
| 30 | assert.same args[1], 1 | ||
| 31 | assert.same args[2], nil | ||
| 32 | assert.same args[3], 3 | ||
| 33 | assert.same args[4], nil | ||
| 34 | assert.same args[5], 5 | ||
| 35 | |||
| 36 | f 1, nil, 3, nil, 5 | ||
| 37 | |||
| 38 | it "should work with loop", -> | ||
| 39 | f = (...t) -> | ||
| 40 | sum = 0 | ||
| 41 | for i = 1, t.n | ||
| 42 | sum += t[i] if type(t[i]) == "number" | ||
| 43 | sum | ||
| 44 | |||
| 45 | result = f 1, 2, 3, 4, 5 | ||
| 46 | assert.same result, 15 | ||
| 47 | |||
| 48 | it "should handle mixed types", -> | ||
| 49 | f = (...args) -> | ||
| 50 | types = [type(args[i]) for i = 1, args.n] | ||
| 51 | types | ||
| 52 | |||
| 53 | result = f "string", 123, true, nil, {} | ||
| 54 | assert.same result, {"string", "number", "boolean", "nil", "table"} | ||
| 55 | |||
| 56 | it "should work with table access", -> | ||
| 57 | f = (...t) -> | ||
| 58 | first = t[1] | ||
| 59 | last = t[t.n] | ||
| 60 | {first, last} | ||
| 61 | |||
| 62 | result = f 1, 2, 3, 4, 5 | ||
| 63 | assert.same result, {1, 5} | ||
| 64 | |||
| 65 | it "should support select with named args", -> | ||
| 66 | f = (...args) -> | ||
| 67 | second = select 2, table.unpack args | ||
| 68 | second | ||
| 69 | |||
| 70 | result = f "a", "b", "c" | ||
| 71 | assert.same result, "b" | ||
| 72 | |||
| 73 | it "should work with pcall", -> | ||
| 74 | f = (...t) -> | ||
| 75 | success = true | ||
| 76 | for i = 1, t.n | ||
| 77 | if t[i] == nil | ||
| 78 | success = false | ||
| 79 | success | ||
| 80 | |||
| 81 | result = f 1, nil, 3 | ||
| 82 | assert.is_false result | ||
| 83 | |||
| 84 | it "should handle function results", -> | ||
| 85 | g = -> 1, 2, 3 | ||
| 86 | f = (...t) -> | ||
| 87 | t.n | ||
| 88 | |||
| 89 | result = f g! | ||
| 90 | assert.same result, 3 | ||
| 91 | |||
| 92 | it "should work with unpacking", -> | ||
| 93 | f = (...args) -> | ||
| 94 | {table.unpack args} | ||
| 95 | |||
| 96 | result = f "a", "b", "c" | ||
| 97 | assert.same result, {"a", "b", "c"} | ||
| 98 | |||
| 99 | it "should support passing named varargs to another function", -> | ||
| 100 | outer = (...t) -> | ||
| 101 | inner (table.unpack t) | ||
| 102 | |||
| 103 | inner = (a, b, c) -> | ||
| 104 | {a, b, c} | ||
| 105 | |||
| 106 | result = outer 1, 2, 3 | ||
| 107 | assert.same result, {1, 2, 3} | ||
| 108 | |||
| 109 | it "should work with default parameter", -> | ||
| 110 | f = (x = 10, ...t) -> | ||
| 111 | x + t[1] or 0 | ||
| 112 | |||
| 113 | result = f 5, 15 | ||
| 114 | assert.same result, 20 | ||
| 115 | |||
| 116 | it "should handle single argument", -> | ||
| 117 | f = (...t) -> | ||
| 118 | {t.n, t[1]} | ||
| 119 | |||
| 120 | result = f 42 | ||
| 121 | assert.same result, {1, 42} | ||
diff --git a/spec/inputs/test/operator_advanced_spec.yue b/spec/inputs/test/operator_advanced_spec.yue new file mode 100644 index 0000000..8127fd4 --- /dev/null +++ b/spec/inputs/test/operator_advanced_spec.yue | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | describe "advanced operators", -> | ||
| 2 | it "should support chaining comparisons with functions", -> | ||
| 3 | v = (x) -> x | ||
| 4 | assert.is_true v(1) < v(2) <= v(3) | ||
| 5 | |||
| 6 | it "should handle compound assignment with or", -> | ||
| 7 | x = nil | ||
| 8 | x or= "default" | ||
| 9 | assert.same x, "default" | ||
| 10 | |||
| 11 | it "should not overwrite existing value with or", -> | ||
| 12 | x = "existing" | ||
| 13 | x or= "default" | ||
| 14 | assert.same x, "existing" | ||
| 15 | |||
| 16 | it "should support compound string concatenation", -> | ||
| 17 | s = "hello" | ||
| 18 | s ..= " world" | ||
| 19 | assert.same s, "hello world" | ||
| 20 | |||
| 21 | it "should work with table appending", -> | ||
| 22 | tab = [1, 2] | ||
| 23 | tab[] = 3 | ||
| 24 | tab[] = 4 | ||
| 25 | assert.same tab, {1, 2, 3, 4} | ||
| 26 | |||
| 27 | it "should handle spread append", -> | ||
| 28 | tbA = [1, 2] | ||
| 29 | tbB = [3, 4] | ||
| 30 | tbA[] = ...tbB | ||
| 31 | assert.same tbA, {1, 2, 3, 4} | ||
| 32 | |||
| 33 | it "should support reverse indexing", -> | ||
| 34 | items = [1, 2, 3, 4, 5] | ||
| 35 | assert.same items[#], 5 | ||
| 36 | assert.same items[#-1], 4 | ||
| 37 | assert.same items[#-2], 3 | ||
| 38 | |||
| 39 | it "should work with nil coalescing assignment", -> | ||
| 40 | x = nil | ||
| 41 | x ??= "default" | ||
| 42 | assert.same x, "default" | ||
| 43 | |||
| 44 | it "should not assign with ??= when value exists", -> | ||
| 45 | x = "existing" | ||
| 46 | x ??= "default" | ||
| 47 | assert.same x, "existing" | ||
| 48 | |||
| 49 | it "should chain nil coalescing", -> | ||
| 50 | a = nil | ||
| 51 | b = nil | ||
| 52 | c = "value" | ||
| 53 | result = a ?? b ?? c | ||
| 54 | assert.same result, "value" | ||
| 55 | |||
| 56 | it "should support compound modulo", -> | ||
| 57 | x = 20 | ||
| 58 | x %= 3 | ||
| 59 | assert.same x, 2 | ||
| 60 | |||
| 61 | it "should handle compound exponentiation", -> | ||
| 62 | x = 2 | ||
| 63 | x ^= 3 | ||
| 64 | assert.same x, 8 | ||
| 65 | |||
| 66 | it "should work with compound bitwise and", -> | ||
| 67 | x = 15 -- 1111 in binary | ||
| 68 | x &= 7 -- 0111 in binary | ||
| 69 | assert.same x, 7 | ||
| 70 | |||
| 71 | it "should support compound bitwise or", -> | ||
| 72 | x = 8 -- 1000 in binary | ||
| 73 | x |= 3 -- 0011 in binary | ||
| 74 | assert.same x, 11 -- 1011 in binary | ||
| 75 | |||
| 76 | it "should handle compound bitwise xor", -> | ||
| 77 | x = 12 -- 1100 in binary | ||
| 78 | x ~= 10 -- 1010 in binary | ||
| 79 | assert.same x, 6 -- 0110 in binary | ||
| 80 | |||
| 81 | it "should work with compound left shift", -> | ||
| 82 | x = 2 | ||
| 83 | x <<= 3 | ||
| 84 | assert.same x, 16 | ||
| 85 | |||
| 86 | it "should support compound right shift", -> | ||
| 87 | x = 16 | ||
| 88 | x >>= 2 | ||
| 89 | assert.same x, 4 | ||
| 90 | |||
| 91 | it "should handle negation operator", -> | ||
| 92 | assert.same -10, -10 | ||
| 93 | assert.same --5, 5 | ||
| 94 | |||
| 95 | it "should work with length operator on tables", -> | ||
| 96 | tab = {1, 2, 3, 4, 5} | ||
| 97 | assert.same #tab, 5 | ||
| 98 | |||
| 99 | it "should support length on strings", -> | ||
| 100 | s = "hello" | ||
| 101 | assert.same #s, 5 | ||
| 102 | |||
| 103 | it "should handle chaining assignment", -> | ||
| 104 | a = b = c = d = 0 | ||
| 105 | assert.same a, 0 | ||
| 106 | assert.same b, 0 | ||
| 107 | assert.same c, 0 | ||
| 108 | assert.same d, 0 | ||
| 109 | |||
| 110 | it "should work with chaining assignment with functions", -> | ||
| 111 | f = -> 42 | ||
| 112 | x = y = z = f! | ||
| 113 | assert.same x, 42 | ||
| 114 | assert.same y, 42 | ||
| 115 | assert.same z, 42 | ||
| 116 | |||
| 117 | it "should support != as alias for ~=", -> | ||
| 118 | assert.is_true 1 != 2 | ||
| 119 | assert.is_false 1 != 1 | ||
| 120 | |||
| 121 | it "should work with :: for method chaining", -> | ||
| 122 | obj = | ||
| 123 | value: 10 | ||
| 124 | add: (n) => @value += n | ||
| 125 | get: => @value | ||
| 126 | |||
| 127 | result = obj::add 5::get! | ||
| 128 | assert.same result, 15 | ||
| 129 | |||
| 130 | it "should handle complex expressions with precedence", -> | ||
| 131 | result = 1 + 2 * 3 - 4 / 2 | ||
| 132 | assert.same result, 5 | ||
| 133 | |||
| 134 | it "should support mixed operator types", -> | ||
| 135 | result = 10 + 20 * 2 - 5 / 5 | ||
| 136 | assert.same result, 49 | ||
diff --git a/spec/inputs/test/param_destructure_spec.yue b/spec/inputs/test/param_destructure_spec.yue new file mode 100644 index 0000000..4659031 --- /dev/null +++ b/spec/inputs/test/param_destructure_spec.yue | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | describe "parameter destructuring", -> | ||
| 2 | it "should destructure simple object", -> | ||
| 3 | f = (:a, :b, :c) -> | ||
| 4 | {a, b, c} | ||
| 5 | |||
| 6 | result = f a: 1, b: "2", c: {} | ||
| 7 | assert.same result, {1, "2", {}} | ||
| 8 | |||
| 9 | it "should work with default values", -> | ||
| 10 | f = ({a: a1 = 123, :b = 'abc'}, c = {}) -> | ||
| 11 | {a1, b, c} | ||
| 12 | |||
| 13 | result1 = f {a: 0}, "test" | ||
| 14 | assert.same result1, {0, 'abc', 'test'} | ||
| 15 | |||
| 16 | result2 = f {} | ||
| 17 | assert.same result2, {123, 'abc', {}} | ||
| 18 | |||
| 19 | it "should destructure with mixed syntax", -> | ||
| 20 | f = (:a, b: b1, :c) -> | ||
| 21 | {a, b1, c} | ||
| 22 | |||
| 23 | result = f a: 1, b: 2, c: 3 | ||
| 24 | assert.same result, {1, 2, 3} | ||
| 25 | |||
| 26 | it "should work with nested destructuring", -> | ||
| 27 | f = ({nested: {:x, :y}}) -> | ||
| 28 | {x, y} | ||
| 29 | |||
| 30 | result = f nested: {x: 10, y: 20} | ||
| 31 | assert.same result, {10, 20} | ||
| 32 | |||
| 33 | it "should handle array parameters", -> | ||
| 34 | f = ([a, b, c]) -> | ||
| 35 | {a, b, c} | ||
| 36 | |||
| 37 | result = f [1, 2, 3] | ||
| 38 | assert.same result, {1, 2, 3} | ||
| 39 | |||
| 40 | it "should support mixed array and object", -> | ||
| 41 | f = ([first], {key: :value}) -> | ||
| 42 | {first, value} | ||
| 43 | |||
| 44 | result = f [1], {key: "test"} | ||
| 45 | assert.same result, {1, "test"} | ||
| 46 | |||
| 47 | it "should work with fat arrow", -> | ||
| 48 | obj = | ||
| 49 | value: 100 | ||
| 50 | f: ({:x, :y}) => | ||
| 51 | @value + x + y | ||
| 52 | |||
| 53 | result = obj\f {x: 10, y: 20} | ||
| 54 | assert.same result, 130 | ||
| 55 | |||
| 56 | it "should handle missing keys", -> | ||
| 57 | f = (:a, :b = "default", :c = "missing") -> | ||
| 58 | {a, b, c} | ||
| 59 | |||
| 60 | result = f a: 1 | ||
| 61 | assert.same result, {1, 'default', 'missing'} | ||
| 62 | |||
| 63 | it "should work with complex defaults", -> | ||
| 64 | f = ({a: a1 = 100, b: b1 = a1 + 1000}) -> | ||
| 65 | a1 + b1 | ||
| 66 | |||
| 67 | result = f {} | ||
| 68 | assert.same result, 1200 | ||
| 69 | |||
| 70 | it "should support deep nesting", -> | ||
| 71 | f = ({data: {nested: {:value}}}) -> | ||
| 72 | value | ||
| 73 | |||
| 74 | result = f data: {nested: {value: 42}} | ||
| 75 | assert.same result, 42 | ||
| 76 | |||
| 77 | it "should work with multiple parameters", -> | ||
| 78 | f = (:x, :y, :z, extra = "default") -> | ||
| 79 | {x, y, z, extra} | ||
| 80 | |||
| 81 | result = f x: 1, y: 2, z: 3 | ||
| 82 | assert.same result, {1, 2, 3, 'default'} | ||
| 83 | |||
| 84 | it "should handle array destructuring in parameters", -> | ||
| 85 | f = ([first, ...rest]) -> | ||
| 86 | {first, rest} | ||
| 87 | |||
| 88 | result = f [1, 2, 3, 4] | ||
| 89 | assert.same result, {1, {2, 3, 4}} | ||
| 90 | |||
| 91 | it "should support spreading", -> | ||
| 92 | f = (...rest, :last) -> | ||
| 93 | {rest, last} | ||
| 94 | |||
| 95 | result = f 1, 2, 3, {last: "final"} | ||
| 96 | assert.same result, {{1, 2, 3}, 'final'} | ||
| 97 | |||
| 98 | it "should work with table comprehensions", -> | ||
| 99 | f = ({:items}) -> | ||
| 100 | [item * 2 for item in *items] | ||
| 101 | |||
| 102 | result = f items: {1, 2, 3} | ||
| 103 | assert.same result, {2, 4, 6} | ||
| 104 | |||
| 105 | it "should handle nil arguments", -> | ||
| 106 | f = (:a = "nil_a", :b = "nil_b") -> | ||
| 107 | {a, b} | ||
| 108 | |||
| 109 | result = f nil, nil | ||
| 110 | assert.same result, {'nil_a', 'nil_b'} | ||
diff --git a/spec/inputs/test/prefixed_return_spec.yue b/spec/inputs/test/prefixed_return_spec.yue new file mode 100644 index 0000000..027cd60 --- /dev/null +++ b/spec/inputs/test/prefixed_return_spec.yue | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | describe "prefixed return", -> | ||
| 2 | it "should return prefixed value with no explicit return", -> | ||
| 3 | findFirstEven = (list): nil -> | ||
| 4 | for item in *list | ||
| 5 | if type(item) == "table" | ||
| 6 | for sub in *item | ||
| 7 | if sub % 2 == 0 | ||
| 8 | return sub | ||
| 9 | |||
| 10 | result = findFirstEven {1, 3, {4, 6}, 5} | ||
| 11 | assert.same result, 4 | ||
| 12 | |||
| 13 | it "should return prefixed nil when not found", -> | ||
| 14 | findValue = (list): nil -> | ||
| 15 | for item in *list | ||
| 16 | if item == 999 | ||
| 17 | return item | ||
| 18 | |||
| 19 | result = findValue {1, 2, 3} | ||
| 20 | assert.same result, nil | ||
| 21 | |||
| 22 | it "should return prefixed string", -> | ||
| 23 | findName = (items): "not found" -> | ||
| 24 | for item in *items | ||
| 25 | if item.name == "target" | ||
| 26 | return item.name | ||
| 27 | |||
| 28 | result = findName [{name: "a"}, {name: "b"}] | ||
| 29 | assert.same result, "not found" | ||
| 30 | |||
| 31 | it "should return prefixed number", -> | ||
| 32 | calculateSum = (): 0 -> | ||
| 33 | -- no explicit return | ||
| 34 | total = 0 | ||
| 35 | |||
| 36 | result = calculateSum! | ||
| 37 | assert.same result, 0 | ||
| 38 | |||
| 39 | it "should work with nested logic", -> | ||
| 40 | findNested = (data): "missing" -> | ||
| 41 | for category in *data | ||
| 42 | if type(category) == "table" | ||
| 43 | for item in *category | ||
| 44 | if item == "target" | ||
| 45 | return "found" | ||
| 46 | |||
| 47 | result = findNested {{1, 2}, {"target", 3}} | ||
| 48 | assert.same result, "found" | ||
diff --git a/spec/inputs/test/reverse_index_spec.yue b/spec/inputs/test/reverse_index_spec.yue new file mode 100644 index 0000000..be67261 --- /dev/null +++ b/spec/inputs/test/reverse_index_spec.yue | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | describe "reverse index", -> | ||
| 2 | it "should get last element", -> | ||
| 3 | data = {items: [1, 2, 3, 4, 5]} | ||
| 4 | last = data.items[#] | ||
| 5 | assert.same last, 5 | ||
| 6 | |||
| 7 | it "should get second last element", -> | ||
| 8 | data = {items: [1, 2, 3, 4, 5]} | ||
| 9 | second_last = data.items[#-1] | ||
| 10 | assert.same second_last, 4 | ||
| 11 | |||
| 12 | it "should get third last element", -> | ||
| 13 | data = {items: [1, 2, 3, 4, 5]} | ||
| 14 | third_last = data.items[#-2] | ||
| 15 | assert.same third_last, 3 | ||
| 16 | |||
| 17 | it "should set last element", -> | ||
| 18 | data = {items: [1, 2, 3, 4, 5]} | ||
| 19 | data.items[#] = 10 | ||
| 20 | assert.same data.items[5], 10 | ||
| 21 | |||
| 22 | it "should set second last element", -> | ||
| 23 | data = {items: [1, 2, 3, 4, 5]} | ||
| 24 | data.items[#-1] = 20 | ||
| 25 | assert.same data.items[4], 20 | ||
| 26 | |||
| 27 | it "should work with single element", -> | ||
| 28 | tab = {42} | ||
| 29 | assert.same tab[#], 42 | ||
| 30 | |||
| 31 | it "should work with empty table", -> | ||
| 32 | tab = [] | ||
| 33 | assert.same tab[#], nil | ||
| 34 | |||
| 35 | it "should work in expressions", -> | ||
| 36 | tab = [1, 2, 3, 4, 5] | ||
| 37 | result = tab[#] + tab[#-1] | ||
| 38 | assert.same result, 9 | ||
| 39 | |||
| 40 | it "should support chaining", -> | ||
| 41 | data = {items: {nested: [1, 2, 3]}} | ||
| 42 | last = data.items.nested[#] | ||
| 43 | assert.same last, 3 | ||
| 44 | |||
| 45 | it "should work with string", -> | ||
| 46 | s = "hello" | ||
| 47 | assert.same s[#], "o" | ||
| 48 | |||
| 49 | it "should handle negative offsets", -> | ||
| 50 | tab = [1, 2, 3, 4, 5] | ||
| 51 | assert.same tab[#-3], 2 | ||
| 52 | assert.same tab[#-4], 1 | ||
| 53 | |||
| 54 | it "should work in loops", -> | ||
| 55 | tab = [1, 2, 3, 4, 5] | ||
| 56 | results = [] | ||
| 57 | for i = 0, 2 | ||
| 58 | table.insert results, tab[#-i] | ||
| 59 | assert.same results, {5, 4, 3} | ||
diff --git a/spec/inputs/test/slicing_spec.yue b/spec/inputs/test/slicing_spec.yue new file mode 100644 index 0000000..b0a686b --- /dev/null +++ b/spec/inputs/test/slicing_spec.yue | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | describe "slicing", -> | ||
| 2 | it "should slice array with basic syntax", -> | ||
| 3 | items = [1, 2, 3, 4, 5] | ||
| 4 | result = items[1..3] | ||
| 5 | assert.same result, {1, 2, 3} | ||
| 6 | |||
| 7 | it "should slice from beginning", -> | ||
| 8 | items = [1, 2, 3, 4, 5] | ||
| 9 | result = items[1..#items] | ||
| 10 | assert.same result, {1, 2, 3, 4, 5} | ||
| 11 | |||
| 12 | it "should slice to end", -> | ||
| 13 | items = [1, 2, 3, 4, 5] | ||
| 14 | result = items[3..5] | ||
| 15 | assert.same result, {3, 4, 5} | ||
| 16 | |||
| 17 | it "should handle negative indices", -> | ||
| 18 | items = [1, 2, 3, 4, 5] | ||
| 19 | result = items[#items-2..#items] | ||
| 20 | assert.same result, {3, 4, 5} | ||
| 21 | |||
| 22 | it "should slice single element", -> | ||
| 23 | items = [1, 2, 3, 4, 5] | ||
| 24 | result = items[2..2] | ||
| 25 | assert.same result, {2} | ||
| 26 | |||
| 27 | it "should work with strings", -> | ||
| 28 | s = "hello" | ||
| 29 | result = s\sub 1, 3 | ||
| 30 | assert.same result, "hel" | ||
| 31 | |||
| 32 | it "should handle out of bounds", -> | ||
| 33 | items = [1, 2, 3] | ||
| 34 | result = items[1..10] | ||
| 35 | assert.same result, {1, 2, 3} | ||
| 36 | |||
| 37 | it "should create new table", -> | ||
| 38 | original = [1, 2, 3, 4, 5] | ||
| 39 | sliced = original[2..4] | ||
| 40 | sliced[1] = 99 | ||
| 41 | assert.same original[2], 2 -- original unchanged | ||
| 42 | |||
| 43 | it "should work with nested arrays", -> | ||
| 44 | nested = [[1, 2], [3, 4], [5, 6]] | ||
| 45 | result = nested[1..2] | ||
| 46 | assert.same result, {{1, 2}, {3, 4}} | ||
| 47 | |||
| 48 | it "should slice with step simulation", -> | ||
| 49 | items = [1, 2, 3, 4, 5] | ||
| 50 | result = [items[i] for i = 1, #items, 2] | ||
| 51 | assert.same result, {1, 3, 5} | ||
| 52 | |||
| 53 | it "should handle empty slice range", -> | ||
| 54 | items = [1, 2, 3, 4, 5] | ||
| 55 | result = items[6..10] | ||
| 56 | assert.same result, nil | ||
| 57 | |||
| 58 | it "should work with reverse indexing", -> | ||
| 59 | items = [1, 2, 3, 4, 5] | ||
| 60 | last = items[#] | ||
| 61 | second_last = items[#-1] | ||
| 62 | assert.same last, 5 | ||
| 63 | assert.same second_last, 4 | ||
| 64 | |||
| 65 | it "should support slice in assignment", -> | ||
| 66 | items = [1, 2, 3, 4, 5] | ||
| 67 | [a, b, c] = [items[i] for i in *{1, 2, 3}] | ||
| 68 | assert.same a, 1 | ||
| 69 | assert.same b, 2 | ||
| 70 | assert.same c, 3 | ||
| 71 | |||
| 72 | it "should work with table comprehensions", -> | ||
| 73 | items = [1, 2, 3, 4, 5] | ||
| 74 | result = {i, items[i] for i = 2, 4} | ||
| 75 | assert.same result[2], 2 | ||
| 76 | assert.same result[3], 3 | ||
| 77 | assert.same result[4], 4 | ||
diff --git a/spec/inputs/test/stub_spec.yue b/spec/inputs/test/stub_spec.yue new file mode 100644 index 0000000..99345c7 --- /dev/null +++ b/spec/inputs/test/stub_spec.yue | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | describe "function stub", -> | ||
| 2 | it "should create empty function", -> | ||
| 3 | stub_fn! | ||
| 4 | assert.is_true true | ||
| 5 | |||
| 6 | it "should support stub in table", -> | ||
| 7 | obj = { | ||
| 8 | stub: stub_fn! | ||
| 9 | } | ||
| 10 | assert.is_true true | ||
| 11 | |||
| 12 | it "should work with method stub", -> | ||
| 13 | obj = | ||
| 14 | method: stub_fn! | ||
| 15 | assert.is_true true | ||
| 16 | |||
| 17 | it "should handle stub in assignment", -> | ||
| 18 | my_func = stub_fn! | ||
| 19 | assert.is_true true | ||
| 20 | |||
| 21 | it "should support stub in return", -> | ||
| 22 | get_stub = -> stub_fn! | ||
| 23 | fn = get_stub! | ||
| 24 | assert.is_true true | ||
| 25 | |||
| 26 | it "should work in conditional", -> | ||
| 27 | if stub_fn! | ||
| 28 | assert.is_true true | ||
| 29 | |||
| 30 | it "should support stub as callback", -> | ||
| 31 | call_fn = (fn) -> fn! | ||
| 32 | result = call_fn stub_fn! | ||
| 33 | assert.is_true true | ||
| 34 | |||
| 35 | it "should handle stub in table literal", -> | ||
| 36 | tb = { | ||
| 37 | on_click: stub_fn! | ||
| 38 | on_hover: stub_fn! | ||
| 39 | } | ||
| 40 | assert.is_true true | ||
| 41 | |||
| 42 | it "should work with fat arrow stub", -> | ||
| 43 | obj = | ||
| 44 | value: 10 | ||
| 45 | method: stub_fn! | ||
| 46 | |||
| 47 | result = obj\method! | ||
| 48 | assert.is_true true | ||
| 49 | |||
| 50 | it "should support stub in array", -> | ||
| 51 | callbacks = [stub_fn!, stub_fn!, stub_fn!] | ||
| 52 | assert.same #callbacks, 3 | ||
| 53 | |||
| 54 | it "should handle stub in expression", -> | ||
| 55 | result = stub_fn! and true or false | ||
| 56 | assert.is_true result | ||
| 57 | |||
| 58 | it "should work with chained stub calls", -> | ||
| 59 | stub_fn! | ||
| 60 | stub_fn! | ||
| 61 | stub_fn! | ||
| 62 | assert.is_true true | ||
| 63 | |||
| 64 | it "should support stub in comprehension", -> | ||
| 65 | result = [stub_fn! for i = 1, 3] | ||
| 66 | assert.same #result, 3 | ||
| 67 | |||
| 68 | it "should handle stub in switch", -> | ||
| 69 | value = "test" | ||
| 70 | result = switch value | ||
| 71 | when "test" | ||
| 72 | stub_fn! | ||
| 73 | "matched" | ||
| 74 | else | ||
| 75 | "not matched" | ||
| 76 | assert.same result, "matched" | ||
| 77 | |||
| 78 | it "should work in with statement", -> | ||
| 79 | obj = {stub: stub_fn!} | ||
| 80 | with obj | ||
| 81 | .stub! | ||
| 82 | assert.is_true true | ||
| 83 | |||
| 84 | it "should support stub as argument default", -> | ||
| 85 | fn = (callback = stub_fn!) -> | ||
| 86 | callback! | ||
| 87 | |||
| 88 | result = fn! | ||
| 89 | assert.is_true true | ||
| 90 | |||
| 91 | it "should handle stub in varargs", -> | ||
| 92 | collect = (...) -> | ||
| 93 | {...} | ||
| 94 | |||
| 95 | result = collect stub_fn!, stub_fn! | ||
| 96 | assert.same #result, 2 | ||
| 97 | |||
| 98 | it "should work in do block", -> | ||
| 99 | do | ||
| 100 | stub_fn! | ||
| 101 | assert.is_true true | ||
| 102 | |||
| 103 | it "should support stub in try block", -> | ||
| 104 | success = try | ||
| 105 | stub_fn! | ||
| 106 | true | ||
| 107 | catch err | ||
| 108 | false | ||
| 109 | assert.is_true success | ||
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} | ||
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 @@ | |||
| 1 | describe "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 | ||
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 | ||
diff --git a/spec/inputs/test/varargs_assignment_spec.yue b/spec/inputs/test/varargs_assignment_spec.yue new file mode 100644 index 0000000..1c3b627 --- /dev/null +++ b/spec/inputs/test/varargs_assignment_spec.yue | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | describe "varargs assignment", -> | ||
| 2 | it "should assign varargs from function", -> | ||
| 3 | list = [1, 2, 3, 4, 5] | ||
| 4 | fn = (ok) -> ok, table.unpack list | ||
| 5 | ok, ... = fn true | ||
| 6 | count = select '#', ... | ||
| 7 | assert.same count, 5 | ||
| 8 | assert.same ok, true | ||
| 9 | |||
| 10 | it "should access varargs elements", -> | ||
| 11 | list = [10, 20, 30] | ||
| 12 | fn = -> table.unpack list | ||
| 13 | ... = fn! | ||
| 14 | first = select 1, ... | ||
| 15 | second = select 2, ... | ||
| 16 | third = select 3, ... | ||
| 17 | assert.same first, 10 | ||
| 18 | assert.same second, 20 | ||
| 19 | assert.same third, 30 | ||
| 20 | |||
| 21 | it "should work with pcall", -> | ||
| 22 | fn = -> 1, 2, 3 | ||
| 23 | success, ... = pcall fn | ||
| 24 | assert.is_true success | ||
| 25 | assert.same select('#', ...), 3 | ||
| 26 | |||
| 27 | it "should handle empty varargs", -> | ||
| 28 | fn = -> | ||
| 29 | ... = fn! | ||
| 30 | count = select '#', ... | ||
| 31 | assert.same count, 0 | ||
| 32 | |||
| 33 | it "should work with mixed return values", -> | ||
| 34 | fn = -> "first", nil, "third", false | ||
| 35 | a, ... = fn! | ||
| 36 | assert.same a, "first" | ||
| 37 | assert.same select('#', ...), 3 | ||
| 38 | |||
| 39 | it "should preserve nil values in varargs", -> | ||
| 40 | fn = -> 1, nil, 2, nil, 3 | ||
| 41 | ... = fn! | ||
| 42 | count = select '#', ... | ||
| 43 | assert.same count, 5 | ||
| 44 | assert.same select(1, ...), 1 | ||
| 45 | assert.same select(2, ...), nil | ||
| 46 | assert.same select(3, ...), 2 | ||
| 47 | |||
| 48 | it "should work with table.unpack", -> | ||
| 49 | tb = {a: 1, b: 2, c: 3} | ||
| 50 | fn = -> table.unpack tb | ||
| 51 | ... = fn! | ||
| 52 | count = select '#', ... | ||
| 53 | assert.same count, 3 | ||
| 54 | |||
| 55 | it "should chain varargs assignment", -> | ||
| 56 | fn1 = -> 1, 2, 3 | ||
| 57 | fn2 = -> table.unpack {4, 5, 6} | ||
| 58 | a, ... = fn1! | ||
| 59 | b, ... = fn2! | ||
| 60 | assert.same a, 1 | ||
| 61 | assert.same b, 4 | ||
| 62 | assert.same select('#', ...), 2 | ||
| 63 | |||
| 64 | it "should work in expressions", -> | ||
| 65 | sum = (...) -> | ||
| 66 | total = 0 | ||
| 67 | for i = 1, select '#', ... | ||
| 68 | total += select i, ... if type(select(i, ...)) == "number" | ||
| 69 | total | ||
| 70 | |||
| 71 | fn = -> 1, 2, 3, 4, 5 | ||
| 72 | ... = fn! | ||
| 73 | result = sum ... | ||
| 74 | assert.same result, 15 | ||
| 75 | |||
| 76 | it "should work with string.format", -> | ||
| 77 | ... = "hello", 123, true | ||
| 78 | result = string.format "str: %s, num: %d, bool: %s", ... | ||
| 79 | assert.same result, "str: hello, num: 123, bool: true" | ||
| 80 | |||
| 81 | it "should handle single return value", -> | ||
| 82 | fn = -> 42 | ||
| 83 | ... = fn! | ||
| 84 | count = select '#', ... | ||
| 85 | assert.same count, 1 | ||
| 86 | assert.same select(1, ...), 42 | ||
| 87 | |||
| 88 | it "should work with nested functions", -> | ||
| 89 | outer = -> 1, 2, 3 | ||
| 90 | inner = -> 4, 5 | ||
| 91 | a, b, ... = outer! | ||
| 92 | c, d = inner! | ||
| 93 | assert.same a, 1 | ||
| 94 | assert.same b, 2 | ||
| 95 | assert.same c, 4 | ||
| 96 | assert.same d, 5 | ||
diff --git a/spec/inputs/test/while_assignment_spec.yue b/spec/inputs/test/while_assignment_spec.yue new file mode 100644 index 0000000..1c98e58 --- /dev/null +++ b/spec/inputs/test/while_assignment_spec.yue | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | describe "while assignment", -> | ||
| 2 | it "should loop while value is truthy", -> | ||
| 3 | counter = 0 | ||
| 4 | get_next = -> | ||
| 5 | if counter < 3 | ||
| 6 | counter += 1 | ||
| 7 | counter | ||
| 8 | else | ||
| 9 | nil | ||
| 10 | results = {} | ||
| 11 | while val := get_next! | ||
| 12 | table.insert results, val | ||
| 13 | assert.same results, {1, 2, 3} | ||
| 14 | |||
| 15 | it "should work with function results", -> | ||
| 16 | counter = 0 | ||
| 17 | fn = -> | ||
| 18 | counter += 1 | ||
| 19 | if counter <= 3 | ||
| 20 | counter * 10 | ||
| 21 | else | ||
| 22 | nil | ||
| 23 | |||
| 24 | sum = 0 | ||
| 25 | while val := fn! | ||
| 26 | sum += val | ||
| 27 | assert.same sum, 60 -- (10+20+30) | ||
| 28 | |||
| 29 | it "should exit immediately on nil", -> | ||
| 30 | get_val = -> nil | ||
| 31 | counter = 0 | ||
| 32 | while val := get_val! | ||
| 33 | counter += 1 | ||
| 34 | assert.same counter, 0 | ||
| 35 | |||
| 36 | it "should support break in loop", -> | ||
| 37 | items = {1, 2, 3, 4, 5} | ||
| 38 | sum = 0 | ||
| 39 | for item in *items | ||
| 40 | sum += item | ||
| 41 | break if sum > 6 | ||
| 42 | assert.same sum, 10 | ||
diff --git a/spec/inputs/test/whitespace_spec.yue b/spec/inputs/test/whitespace_spec.yue new file mode 100644 index 0000000..baf3fd5 --- /dev/null +++ b/spec/inputs/test/whitespace_spec.yue | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | describe "whitespace", -> | ||
| 2 | it "should support semicolon statement separator", -> | ||
| 3 | a = 1; b = 2; result = a + b | ||
| 4 | assert.same result, 3 | ||
| 5 | |||
| 6 | it "should handle multiple statements on one line", -> | ||
| 7 | x = 10; y = 20; z = x + y | ||
| 8 | assert.same z, 30 | ||
| 9 | |||
| 10 | it "should work with semicolon in function", -> | ||
| 11 | fn = -> a = 1; b = 2; a + b | ||
| 12 | assert.same fn!, 3 | ||
| 13 | |||
| 14 | it "should support multiline chaining", -> | ||
| 15 | obj = | ||
| 16 | value: 10 | ||
| 17 | add: (n) => @value += n | ||
| 18 | get: => @value | ||
| 19 | |||
| 20 | result = obj | ||
| 21 | \add 5 | ||
| 22 | \add 10 | ||
| 23 | \get! | ||
| 24 | assert.same result, 25 | ||
| 25 | |||
| 26 | it "should handle multiline method calls", -> | ||
| 27 | str = " hello " | ||
| 28 | result = str | ||
| 29 | \trim! | ||
| 30 | \upper! | ||
| 31 | assert.same result, "HELLO" | ||
| 32 | |||
| 33 | it "should work with nested chaining", -> | ||
| 34 | obj = | ||
| 35 | level1: | ||
| 36 | level2: | ||
| 37 | level3: => "deep" | ||
| 38 | |||
| 39 | result = obj | ||
| 40 | .level1 | ||
| 41 | .level2 | ||
| 42 | \level3! | ||
| 43 | assert.same result, "deep" | ||
| 44 | |||
| 45 | it "should support chaining with conditionals", -> | ||
| 46 | obj = | ||
| 47 | value: 10 | ||
| 48 | isPositive: => @value > 0 | ||
| 49 | |||
| 50 | result = obj | ||
| 51 | \isPositive! | ||
| 52 | assert.is_true result | ||
| 53 | |||
| 54 | it "should work with pipe in chaining", -> | ||
| 55 | result = [1, 2, 3] | ||
| 56 | |> [x * 2 for x in *] | ||
| 57 | |> table.concat | ||
| 58 | assert.same result, "246" | ||
| 59 | |||
| 60 | it "should handle mixed separators", -> | ||
| 61 | a = 1; b = 2 | ||
| 62 | c = 3 | ||
| 63 | d = 4 | ||
| 64 | result = a + b + c + d | ||
| 65 | assert.same result, 10 | ||
| 66 | |||
| 67 | it "should support indentation with spaces", -> | ||
| 68 | fn = -> | ||
| 69 | if true | ||
| 70 | result = 10 | ||
| 71 | result | ||
| 72 | |||
| 73 | assert.same fn!, 10 | ||
| 74 | |||
| 75 | it "should work with consistent indentation", -> | ||
| 76 | tb = { | ||
| 77 | a: 1 | ||
| 78 | b: 2 | ||
| 79 | nested: | ||
| 80 | c: 3 | ||
| 81 | d: 4 | ||
| 82 | } | ||
| 83 | assert.same tb.a, 1 | ||
| 84 | assert.same tb.nested.c, 3 | ||
| 85 | |||
| 86 | it "should handle semicolon with comments", -> | ||
| 87 | a = 1; -- comment | ||
| 88 | b = 2; -- another comment | ||
| 89 | result = a + b | ||
| 90 | assert.same result, 3 | ||
| 91 | |||
| 92 | it "should work in multiline function call", -> | ||
| 93 | sum = (a, b) -> a + b | ||
| 94 | result = sum 5, 10, | ||
| 95 | sum 3, 7 | ||
| 96 | assert.same result, 25 | ||
| 97 | |||
| 98 | it "should support chaining in assignment", -> | ||
| 99 | obj = | ||
| 100 | value: 5 | ||
| 101 | double: => @value * 2 | ||
| 102 | |||
| 103 | doubled = obj | ||
| 104 | \double! | ||
| 105 | assert.same doubled, 10 | ||
| 106 | |||
| 107 | it "should handle complex chaining", -> | ||
| 108 | result = "hello" | ||
| 109 | \upper! | ||
| 110 | \sub 1, 3 | ||
| 111 | \lower! | ||
| 112 | assert.same result, "hel" | ||
| 113 | |||
| 114 | it "should work with backcalls and whitespace", -> | ||
| 115 | results = do | ||
| 116 | data <- readAsync "data.txt" | ||
| 117 | process data | ||
| 118 | assert.is_true true | ||
diff --git a/spec/inputs/test/with_statement_spec.yue b/spec/inputs/test/with_statement_spec.yue new file mode 100644 index 0000000..c2f9b3b --- /dev/null +++ b/spec/inputs/test/with_statement_spec.yue | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | describe "with statement", -> | ||
| 2 | it "should access properties with dot", -> | ||
| 3 | obj = {x: 10, y: 20} | ||
| 4 | result = nil | ||
| 5 | with obj | ||
| 6 | result = .x + .y | ||
| 7 | assert.same result, 30 | ||
| 8 | |||
| 9 | it "should chain property access", -> | ||
| 10 | obj = {nested: {value: 42}} | ||
| 11 | result = nil | ||
| 12 | with obj | ||
| 13 | result = .nested.value | ||
| 14 | assert.same result, 42 | ||
| 15 | |||
| 16 | it "should work with method calls", -> | ||
| 17 | obj = | ||
| 18 | value: 10 | ||
| 19 | double: => @value * 2 | ||
| 20 | |||
| 21 | result = nil | ||
| 22 | with obj | ||
| 23 | result = \double! | ||
| 24 | assert.same result, 20 | ||
| 25 | |||
| 26 | it "should handle nested with statements", -> | ||
| 27 | obj = {x: 1} | ||
| 28 | with obj | ||
| 29 | .x = 10 | ||
| 30 | with .nested = {y: 2} | ||
| 31 | .y = 20 | ||
| 32 | assert.same obj.x, 10 | ||
| 33 | assert.same obj.nested.y, 20 | ||
| 34 | |||
| 35 | it "should work in expressions", -> | ||
| 36 | obj = {value: 5} | ||
| 37 | result = with obj | ||
| 38 | .value * 2 | ||
| 39 | assert.same result, 10 | ||
| 40 | |||
| 41 | it "should support multiple statements", -> | ||
| 42 | obj = {a: 1, b: 2} | ||
| 43 | sum = nil | ||
| 44 | product = nil | ||
| 45 | with obj | ||
| 46 | sum = .a + .b | ||
| 47 | product = .a * .b | ||
| 48 | assert.same sum, 3 | ||
| 49 | assert.same product, 2 | ||
| 50 | |||
| 51 | it "should work with table manipulation", -> | ||
| 52 | obj = {items: [1, 2, 3]} | ||
| 53 | with obj | ||
| 54 | table.insert .items, 4 | ||
| 55 | assert.same #obj.items, 4 | ||
| 56 | |||
| 57 | it "should handle conditional inside with", -> | ||
| 58 | obj = {value: 10} | ||
| 59 | result = nil | ||
| 60 | with obj | ||
| 61 | if .value > 5 | ||
| 62 | result = "large" | ||
| 63 | else | ||
| 64 | result = "small" | ||
| 65 | assert.same result, "large" | ||
| 66 | |||
| 67 | it "should work with loops", -> | ||
| 68 | obj = {items: [1, 2, 3]} | ||
| 69 | sum = nil | ||
| 70 | with obj | ||
| 71 | sum = 0 | ||
| 72 | for item in *.items | ||
| 73 | sum += item | ||
| 74 | assert.same sum, 6 | ||
| 75 | |||
| 76 | it "should support with in assignment", -> | ||
| 77 | obj = {x: 5, y: 10} | ||
| 78 | result = with obj | ||
| 79 | .x + .y | ||
| 80 | assert.same result, 15 | ||
| 81 | |||
| 82 | it "should work with string methods", -> | ||
| 83 | s = "hello" | ||
| 84 | result = with s | ||
| 85 | \upper! | ||
| 86 | assert.same result, "HELLO" | ||
| 87 | |||
| 88 | it "should handle metatable access", -> | ||
| 89 | obj = setmetatable {value: 10}, { | ||
| 90 | __index: {extra: 5} | ||
| 91 | } | ||
| 92 | sum = nil | ||
| 93 | with obj | ||
| 94 | sum = .value + .<index>.extra | ||
| 95 | assert.same sum, 15 | ||
| 96 | |||
| 97 | it "should work in function", -> | ||
| 98 | fn = -> | ||
| 99 | obj = {x: 10} | ||
| 100 | with obj | ||
| 101 | .x * 2 | ||
| 102 | |||
| 103 | result = fn! | ||
| 104 | assert.same result, 20 | ||
| 105 | |||
| 106 | it "should support with in return", -> | ||
| 107 | get_value = -> | ||
| 108 | obj = {value: 42} | ||
| 109 | with obj | ||
| 110 | .value | ||
| 111 | |||
| 112 | assert.same get_value!, 42 | ||
| 113 | |||
| 114 | it "should work with existential operator", -> | ||
| 115 | obj = {value: 10} | ||
| 116 | result = with obj | ||
| 117 | .value ? 0 | ||
| 118 | assert.same result, 10 | ||
| 119 | |||
| 120 | it "should handle nil object safely", -> | ||
| 121 | result = with nil | ||
| 122 | .value | ||
| 123 | assert.same result, nil | ||
| 124 | |||
| 125 | it "should work with method chaining", -> | ||
| 126 | obj = | ||
| 127 | value: 5 | ||
| 128 | add: (n) => @value += n | ||
| 129 | get: => @value | ||
| 130 | |||
| 131 | result = with obj | ||
| 132 | \add 10 | ||
| 133 | \add 5 | ||
| 134 | \get! | ||
| 135 | assert.same result, 20 | ||
| 136 | |||
| 137 | it "should support nested property access", -> | ||
| 138 | obj = | ||
| 139 | level1: | ||
| 140 | level2: | ||
| 141 | level3: "deep" | ||
| 142 | |||
| 143 | result = with obj | ||
| 144 | .level1.level2.level3 | ||
| 145 | assert.same result, "deep" | ||
diff --git a/spec/inputs/test/yaml_string_spec.yue b/spec/inputs/test/yaml_string_spec.yue new file mode 100644 index 0000000..1296340 --- /dev/null +++ b/spec/inputs/test/yaml_string_spec.yue | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | describe "yaml string", -> | ||
| 2 | it "should create basic yaml string", -> | ||
| 3 | s = | | ||
| 4 | hello | ||
| 5 | world | ||
| 6 | assert.is_true s\match "hello" | ||
| 7 | assert.is_true s\match "world" | ||
| 8 | |||
| 9 | it "should preserve indentation", -> | ||
| 10 | s = | | ||
| 11 | key1: value1 | ||
| 12 | key2: value2 | ||
| 13 | assert.is_true s\match "key1" | ||
| 14 | assert.is_true s\match "key2" | ||
| 15 | |||
| 16 | it "should support interpolation", -> | ||
| 17 | name = "test" | ||
| 18 | s = | | ||
| 19 | hello #{name} | ||
| 20 | assert.same s, "hello test" | ||
| 21 | |||
| 22 | it "should handle complex interpolation", -> | ||
| 23 | x, y = 10, 20 | ||
| 24 | s = | | ||
| 25 | point: | ||
| 26 | x: #{x} | ||
| 27 | y: #{y} | ||
| 28 | assert.is_true s\match "x: 10" | ||
| 29 | assert.is_true s\match "y: 20" | ||
| 30 | |||
| 31 | it "should work with expressions", -> | ||
| 32 | s = | | ||
| 33 | result: #{1 + 2} | ||
| 34 | assert.is_true s\match "result: 3" | ||
| 35 | |||
| 36 | it "should support multiline with variables", -> | ||
| 37 | config = | | ||
| 38 | database: | ||
| 39 | host: localhost | ||
| 40 | port: 5432 | ||
| 41 | name: mydb | ||
| 42 | assert.is_true config\match "database:" | ||
| 43 | assert.is_true config\match "host:" | ||
| 44 | |||
| 45 | it "should escape special characters", -> | ||
| 46 | s = | | ||
| 47 | path: "C:\Program Files\App" | ||
| 48 | note: 'He said: "#{Hello}!"' | ||
| 49 | assert.is_true s\match "path:" | ||
| 50 | assert.is_true s\match "note:" | ||
| 51 | |||
| 52 | it "should work in function", -> | ||
| 53 | fn = -> | ||
| 54 | str = | | ||
| 55 | foo: | ||
| 56 | bar: baz | ||
| 57 | return str | ||
| 58 | |||
| 59 | result = fn! | ||
| 60 | assert.is_true result\match "foo:" | ||
| 61 | assert.is_true result\match "bar:" | ||
| 62 | |||
| 63 | it "should strip common leading whitespace", -> | ||
| 64 | fn = -> | ||
| 65 | s = | | ||
| 66 | nested: | ||
| 67 | item: value | ||
| 68 | s | ||
| 69 | |||
| 70 | result = fn! | ||
| 71 | assert.is_true result\match "nested:" | ||
| 72 | assert.is_true result\match "item:" | ||
| 73 | |||
| 74 | it "should support empty lines", -> | ||
| 75 | s = | | ||
| 76 | line1 | ||
| 77 | |||
| 78 | line3 | ||
| 79 | assert.is_true s\match "line1" | ||
| 80 | assert.is_true s\match "line3" | ||
| 81 | |||
| 82 | it "should work with table access in interpolation", -> | ||
| 83 | t = {value: 100} | ||
| 84 | s = | | ||
| 85 | value: #{t.value} | ||
| 86 | assert.is_true s\match "value: 100" | ||
| 87 | |||
| 88 | it "should support function calls in interpolation", -> | ||
| 89 | s = | | ||
| 90 | result: #{(-> 42)!} | ||
| 91 | assert.is_true s\match "result: 42" | ||
| 92 | |||
| 93 | it "should handle quotes correctly", -> | ||
| 94 | s = | | ||
| 95 | "quoted" | ||
| 96 | 'single quoted' | ||
| 97 | assert.is_true s\match '"quoted"' | ||
| 98 | assert.is_true s\match "'single quoted'" | ||
| 99 | |||
| 100 | it "should work with multiple interpolations", -> | ||
| 101 | a, b, c = 1, 2, 3 | ||
| 102 | s = | | ||
| 103 | values: #{a}, #{b}, #{c} | ||
| 104 | assert.is_true s\match "values: 1, 2, 3" | ||
| 105 | |||
| 106 | it "should preserve newlines", -> | ||
| 107 | s = | | ||
| 108 | first line | ||
| 109 | second line | ||
| 110 | third line | ||
| 111 | lines = [line for line in s\gmatch "[^\n]+"] | ||
| 112 | assert.same #lines, 3 | ||
