-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
186 lines (156 loc) · 5.23 KB
/
test.lua
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
local f = require("f-string")
local returncode = 0
local function test(name, fn)
io.write("Test: " .. name .. " ... ")
local ok, result = pcall(fn)
if ok then
print("PASS")
return true
else
print("FAIL: " .. tostring(result))
returncode = 1
return false
end
end
local function assert_eq(actual, expected)
if actual ~= expected then
error(string.format("Expected %q but got %q", expected, actual))
end
end
print("\n=== F-STRING TEST SUITE ===\n")
test("Basic string interpolation", function()
local name = "Lua"
local result = f"Hello, {name}!"
assert_eq(result, "Hello, Lua!")
end)
test("Multiple variables", function()
local name = "world"
local greeting = "Hello"
local result = f"{greeting}, {name}!"
assert_eq(result, "Hello, world!")
end)
test("Expressions", function()
local a = 5
local b = 3
local result = f"The sum of {a} and {b} is {a + b}"
assert_eq(result, "The sum of 5 and 3 is 8")
end)
test("Format specifiers", function()
local pi = math.pi
local result = f"Pi is approximately {pi:%.2f}"
assert_eq(result, "Pi is approximately 3.14")
end)
test("Escaping with double braces", function()
local result = f"A set is represented as {{elements}}"
assert_eq(result, "A set is represented as {elements}")
end)
test("Debug equals sign", function()
local x = 42
local result = f"{x = }"
assert_eq(result, "x = 42")
end)
test("Debug equals with format", function()
local amount = 123.456
local result = f"{amount = :%.1f}"
assert_eq(result, "amount = 123.5")
end)
test("Nested expressions", function()
local a = 2
local b = 3
local result = f"Result: {a * (b + 1)}"
assert_eq(result, "Result: 8")
end)
test("Local variable scope", function()
local a = "outer"
do
local a = "inner"
local b = "local"
local result = f"A is {a} and B is {b}"
assert_eq(result, "A is inner and B is local")
end
end)
test("Global variable access", function()
_G.globalVar = "I am global"
local result = f"Global: {globalVar}"
assert_eq(result, "Global: I am global")
end)
test("String literals in expressions", function()
local result = f"This is a {'nested'} string with {'multiple'} parts"
assert_eq(result, "This is a nested string with multiple parts")
end)
test("Complex string expressions", function()
local prefix = "Lua"
local result = f"{prefix .. ' is ' .. 'awesome'}"
assert_eq(result, "Lua is awesome")
end)
test("Error detection for invalid syntax", function()
local success = pcall(function() return f"Unmatched {brace" end)
assert_eq(success, false)
end)
test("Special characters", function()
local result = f"New\nline and {1+1} equals two"
assert_eq(result, "New\nline and 2 equals two")
end)
test("Empty expressions", function()
local empty = ""
local result = f"An empty string: '{empty}'"
assert_eq(result, "An empty string: ''")
end)
test("Nested f-strings", function()
local a = 1
local b = 2
local inner = f"{a} + {b}"
local result = f"Calculation: {inner} = {a + b}"
assert_eq(result, "Calculation: 1 + 2 = 3")
end)
test("Function calls in expressions", function()
local function double(x) return x * 2 end
local value = 5
local result = f"Double of {value} is {double(value)}"
assert_eq(result, "Double of 5 is 10")
end)
test("Table access in expressions", function()
local person = { name = "Alice", age = 30 }
local result = f"{person.name} is {person.age} years old"
assert_eq(result, "Alice is 30 years old")
end)
test("Complex variable shadowing", function()
local x = "global scope"
local function outer()
local x = "outer scope"
local function inner()
-- only works if we use x outside of the f-string :(
if x then end
assert_eq(f"Current x: {x}", "Current x: outer scope")
local x = "inner scope"
local result = f"Current x: {x}"
assert_eq(result, "Current x: inner scope")
-- Access variable from outer scope using upvalue
local y = "inner y"
do
local y = "block y"
-- This tests that the implementation prioritizes the innermost scope
local result = f"From block - x: {x}, y: {y}"
assert_eq(result, "From block - x: inner scope, y: block y")
end
-- This tests that after leaving a block, we get variables from the enclosing scope
local result2 = f"After block - x: {x}, y: {y}"
assert_eq(result2, "After block - x: inner scope, y: inner y")
end
inner()
-- Test that after inner function returns, we get the outer scope's value
local result = f"In outer - x: {x}"
assert_eq(result, "In outer - x: outer scope")
end
outer()
-- Test that at the top level we get the global scope's value
local result = f"At top level - x: {x}"
assert_eq(result, "At top level - x: global scope")
end)
test("Setting values in f-strings", function()
local x = 10
f"{(function() x = 20 end)()}"
assert_eq(x, 20)
end)
print("\n=== TEST SUITE COMPLETE ===\n")
os.exit(returncode)