aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/macro_spec.lua
blob: 9c505480df176149c971ea322c61c48f1c2b0904 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
return describe("macro", function()
	it("should define and call basic macro", function()
		local result = (5 * 2)
		return assert.same(result, 10)
	end)
	it("should maintain hygiene in macros", function()
		local a = 8
		local result = 2
		return assert.same(result, 2)
	end)
	it("should validate AST types", function()
		local result = {
			123,
			'xyz'
		}
		return assert.same(result, {
			123,
			'xyz'
		})
	end)
	it("should support simple code generation", function()
		local result = (10 + 1)
		return assert.same(result, 11)
	end)
	it("should support nested macro calls", function()
		local result = 7
		return assert.same(result, 7)
	end)
	it("should respect macro scope in do blocks", function()
		do
			local result = 'inner'
			assert.same(result, "inner")
		end
		local result = 'outer'
		return assert.same(result, "outer")
	end)
	it("should provide $LINE macro", function()
		local line_num = 42
		return assert.is_true(line_num > 0)
	end)
	it("should inject Lua code", function()
		local x = 0
		do
local function f(a)
				return a + 1
			end
			x = x + f(3)
		end
		return assert.same(x, 4)
	end)
	it("should work in conditional compilation", function()
		local result = "debug mode"
		return assert.same(result, "debug mode")
	end)
	it("should work with class system", function()
		local Thing
		do
			local _class_0
			local _base_0 = {
				value = 100,
				get_value = function(self)
					return self.value
				end
			}
			if _base_0.__index == nil then
				_base_0.__index = _base_0
			end
			_class_0 = setmetatable({
				__init = function() end,
				__base = _base_0,
				__name = "Thing"
			}, {
				__index = _base_0,
				__call = function(cls, ...)
					local _self_0 = setmetatable({ }, _base_0)
					cls.__init(_self_0, ...)
					return _self_0
				end
			})
			_base_0.__class = _class_0
			Thing = _class_0
		end
		local instance = Thing()
		return assert.same(instance:get_value(), 100)
	end)
	it("should handle macro in switch expressions", function()
		local result
		do
			local _exp_0 = "test"
			if "test" == _exp_0 then
				result = "matched"
			else
				result = "no match"
			end
		end
		return assert.same(result, "matched")
	end)
	it("should support macro in expression context", function()
		local result = 5 + (2 * 3)
		return assert.same(result, 11)
	end)
	it("should handle $is_ast for type checking", function()
		local result = 42
		return assert.same(result, 42)
	end)
	it("should work with string interpolation", function()
		local result = {
			["test"] = 123
		}
		return assert.same(result, {
			test = 123
		})
	end)
	it("should support function call syntax", function()
		local result = (5 + 10)
		return assert.same(result, 15)
	end)
	it("should handle empty macro return", function()
		local a = 1
		a = 2
		return assert.same(a, 2)
	end)
	it("should work with table literals", function()
		local point = {
			x = 10,
			y = 20
		}
		assert.same(point.x, 10)
		return assert.same(point.y, 20)
	end)
	it("should support conditional expressions in macro", function()
		local result = (5 + 1)
		return assert.same(result, 6)
	end)
	it("should work with comprehension", function()
		local result
		do
			local _accum_0 = { }
			local _len_0 = 1
			local _list_0 = {
				1,
				2,
				3
			}
			for _index_0 = 1, #_list_0 do
				local _ = _list_0[_index_0]
				_accum_0[_len_0] = _ * 2
				_len_0 = _len_0 + 1
			end
			result = _accum_0
		end
		return assert.same(result, {
			2,
			4,
			6
		})
	end)
	it("should support complex expression macros", function()
		local result = (1 + 2 * 3)
		return assert.same(result, 7)
	end)
	return it("should work with string literals", function()
		local result = ('Hello, ' .. "World")
		return assert.same(result, "Hello, World")
	end)
end)