diff options
Diffstat (limited to 'spec/inputs/test/advanced_macro_spec.yue')
| -rw-r--r-- | spec/inputs/test/advanced_macro_spec.yue | 141 |
1 files changed, 141 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" | ||
