aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/advanced_macro_spec.yue
blob: 3d7b10aa9d3d04a0207e0fe1b0d2550fb3793032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
describe "advanced macro", ->
	it "should evaluate macro at compile time", ->
		macro PI2 = -> math.pi * 2
		area = $PI2 * 5
		assert.is_true area > 0

	it "should support macro with arguments", ->
		macro add = (a, b) -> "#{a} + #{b}"
		result = $add 5, 10
		assert.same result, 15

	it "should handle string returning macro", ->
		macro HELLO = -> "'hello world'"
		result = $HELLO
		assert.same result, "hello world"

	it "should work with conditional compilation", ->
		macro config = (debugging) ->
			global debugMode = debugging == "true"
			""

		$config true
		assert.is_true debugMode

		$config false
		assert.is_false debugMode

	it "should support macro generating conditional code", ->
		macro asserts = (cond) ->
			debugMode and "assert #{cond}" or ""

		global debugMode = true
		x = 10
		$asserts x == 10  -- should assert
		assert.same x, 10

	it "should work with lua code insertion", ->
		macro luaCode = (code) -> {
			:code
			type: "lua"
		}

		$luaCode "local macro_test_var = 42"
		assert.same macro_test_var, 42

	it "should support multi-line raw lua", ->
		macro lua = (code) -> {
			:code
			type: "lua"
		}

		$lua[==[
			local multiline_var = "test"
		]==]
		assert.same multiline_var, "test"

	it "should export macro from module", ->
		-- This test demonstrates macro export syntax
		-- Actual testing would require separate files
		macro exported_macro = (x) -> "#{x} * 2"
		result = $exported_macro 5
		assert.same result, 10

	it "should work with builtin FILE macro", ->
		macro file_test = ->
			"$FILE"

		result = $file_test
		assert.is_true type(result) == "string"

	it "should work with builtin LINE macro", ->
		macro line_test = ->
			"$LINE"

		result = $line_test
		assert.is_true type(result) == "number"

	it "should support argument validation", ->
		macro expect_num = (val `Num) ->
			"#{val}"

		result = $expect_num 123
		assert.same result, 123

	it "should handle string argument validation", ->
		macro expect_str = (str `String) ->
			"#{str}"

		result = $expect_str "hello"
		assert.same result, "hello"

	it "should work with is_ast check", ->
		macro safe_add = (a, b) ->
			error "expected numbers" unless $is_ast Num, a
			error "expected numbers" unless $is_ast Num, b
			"#{a} + #{b}"

		result = $safe_add 10, 20
		assert.same result, 30

	it "should support macro generating macro", ->
		macro Enum = (...) ->
			items = {...}
			itemSet = {item, true for item in *items}
			(item) ->
				error "got \"#{item}\", expecting one of #{table.concat items, ', '}" unless itemSet[item]
				"\"#{item}\""

		macro Color = $Enum(
			Red
			Green
			Blue
		)

		result = $Color Red
		assert.same result, "Red"

	it "should handle complex macro logic", ->
		macro smart_print = (items) ->
			"print(#{table.concat [item for item in *items], ', ')})"

		$smart_print {"hello", "world", 123}

	it "should work with table manipulation", ->
		macro create_table = (...) ->
			items = {...}
			"{#{table.concat items, ', '}}"

		result = $create_table "1", "2", "3"
		assert.same result, {"1", "2", "3"}

	it "should support string concatenation in macro", ->
		macro concat = (...) ->
			args = {...}
			res = {}
			for arg in *args
				table.insert res, tostring arg
			"'" .. table.concat(res, " .. ") .. "'"

		result = $concat "hello", "world"
		assert.same result, "helloworld"