Skip to content

Commit e246e52

Browse files
committed
spec/: Modify tests to use new method chaining structure
1 parent ebd2880 commit e246e52

File tree

6 files changed

+79
-66
lines changed

6 files changed

+79
-66
lines changed

spec/compilers/source_spec.lua

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
local compiler = require("fusion.core.compilers.source")
22
local parser = require("fusion.core.parser")
3+
local lfs = require("lfs")
34

45
describe("compilers/source", function()
6+
local out_file
7+
after_each(function()
8+
if out_file then
9+
out_file:close()
10+
out_file = nil
11+
end
12+
end)
13+
for file in lfs.dir("spec/in") do
14+
if not file:match("^%.") then
15+
it("can compile file " .. file .. " to Lua source", function()
16+
local compiled = assert(compiler.read_file("spec/in/" .. file))
17+
out_file = assert(io.open("spec/out/source/" .. file:gsub("fuse",
18+
"lua")))
19+
assert.same(out_file:read("*a"), compiled)
20+
end)
21+
end
22+
end
523
it("can compile FusionScript code", function()
624
compiler.compile(parser:match("print('test');"), function(output)
725
assert.same("print(\"test\")", output)
826
end)
927
end)
10-
it("can compile FusionScript files", function()
11-
local input = [[
12-
print("test")
13-
]]
14-
assert.same(input, compiler.read_file("spec/in/basic.fuse"))
28+
it("can error out with bad AST", function()
29+
assert.errors(function()
30+
local c = compiler:new()
31+
c:transform('errors') -- gives type error
32+
end)
33+
assert.errors(function()
34+
local c = compiler:new()
35+
c:transform({'errors'}) -- gives error about being bad node
36+
end)
1537
end)
1638
it("can load FusionScript files", function()
1739
local old_print = print

spec/in/class.fuse

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class ExampleToo extends Example {
3434

3535
c = ExampleToo();
3636
c:print(); -- @b
37-
c:print<Example>(); -- @a
37+
Example.print(c);
3838

3939
-- superinitialization --
4040

4141
class ExampleThree extends ExampleToo {
4242
__init(a, b)=>
43-
self:__init<ExampleToo>(a, b);
43+
self.__super.__init(self, a, b);
4444
}
4545

4646
-- interface

spec/in/functions.fuse

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ a[b]();
1111

1212
a:b();
1313
a.b:c();
14-
a.b:c<d>();
15-
a:b<c>();
16-
17-
-- generated function loops --
14+
a.b:c();
15+
a:b();
1816

19-
print(line in io.lines("test"));
17+
-- chained function calls
2018

21-
print(line for line in io.lines("test"));
19+
a().b();
20+
a():b();
21+
@a().b:c();
2222

23-
print(k, v for k, v in pairs(x));
23+
a:b(as, df, gh):qwerty(potato);
24+
@a:b(c.d.e:f(h, i):j(k, l).m:n(o));
2425

2526
-- basic function definitions --
2627

spec/lexer_spec.lua

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -212,50 +212,44 @@ describe("lexer", function()
212212
})
213213
end)
214214
it("can parse a simple function", function()
215-
assert.same(lexer:match("func();"), {{type = "function_call",
215+
assert.same(lexer:match("func();"), {{type = "function_call", {
216216
{type = "variable", "func"}
217-
}})
218-
end)
219-
it("can parse a simple generated function loop", function()
220-
assert.same(lexer:match("func(a in b);"), {{type = "function_call",
221-
{type = "variable", "func"},
222-
generator = {
223-
{type = "variable", "b"},
224-
variable_list = {{type = "variable", "a"}}
225-
}
226-
}})
227-
end)
228-
it("can parse a complex generated function loop", function()
229-
assert.same(lexer:match("func(a for a in b);"), {{type = "function_call",
230-
{type = "variable", "func"},
231-
generator = {
232-
{type = "variable", "b"},
233-
expression_list = {{type = "variable", "a"}},
234-
variable_list = {{type = "variable", "a"}}
217+
}}})
218+
end)
219+
it("can parse chained functions", function()
220+
assert.same(lexer:match("func().two():three().four:five();"), {{
221+
type = "function_call", {
222+
{type = "variable", "func"}
223+
}, {
224+
{type = "variable", "two"}
225+
}, {
226+
has_self = "three"
227+
}, {
228+
{type = "variable", "four"},
229+
has_self = "five"
235230
}
236231
}})
237232
end)
238233
it("can parse arguments passed to a function", function()
239234
assert.same(lexer:match("func(test, test_two);"), {
240-
{type = "function_call",
235+
{type = "function_call", {
241236
{type = "variable", "func"},
242237
expression_list = {
243238
{type = "variable", "test"},
244239
{type = "variable", "test_two"}
245-
}
240+
}}
246241
}
247242
})
248243
end)
249244
it("can parse a complex function call", function()
250-
assert.same(lexer:match("table.instance:method<subclass>(argument);"), {
251-
{type = "function_call",
245+
assert.same(lexer:match("table.instance:method(argument);"), {
246+
{type = "function_call", {
252247
{type = "variable", "table", "instance"}, -- tables parse -this- way
253248
expression_list = {
254249
{type = "variable", "argument"}
255250
},
256-
has_self = "method",
257-
index_class = "subclass"
258-
}
251+
has_self = "method"
252+
}}
259253
})
260254
end)
261255
it("can parse a non-generated table", function()
@@ -300,13 +294,13 @@ describe("lexer", function()
300294
{type = "variable", "z"},
301295
operator = ".."
302296
},
303-
{type = "function_call",
297+
{type = "function_call", {
304298
{type = "variable", "y"},
305299
expression_list = {
306300
{type = "variable", "a"},
307301
{type = "variable", "b"}
308302
}
309-
}
303+
}}
310304
}
311305
}}
312306
}
@@ -326,21 +320,21 @@ describe("lexer", function()
326320
it("can parse while loops", function()
327321
assert.same(lexer:match("while true print('hi!');"), {
328322
{type = "while_loop",
329-
{type = "function_call",
323+
{type = "function_call", {
330324
{type = "variable", "print"},
331325
expression_list = {{type = "sqstring", "hi!"}}
332-
},
326+
}},
333327
condition = {type = "boolean", true}
334328
}
335329
})
336330
end)
337331
it("can parse numeric for loops", function()
338332
assert.same(lexer:match("for (i=1, 100, 5) print(i);"), {
339333
{type = "numeric_for_loop",
340-
{type = "function_call",
334+
{type = "function_call", {
341335
{type = "variable", "print"},
342336
expression_list = {{type = "variable", "i"}}
343-
},
337+
}},
344338
start = {type = "number", 1, base = "10"},
345339
stop = {type = "number", 100, base = "10"},
346340
step = {type = "number", 5, base = "10"},
@@ -352,10 +346,10 @@ describe("lexer", function()
352346
assert.same(lexer:match("for (i in x) print(i);"), {
353347
{type = "iterative_for_loop",
354348
{type = "variable", "x"},
355-
{type = "function_call",
349+
{type = "function_call", {
356350
{type = "variable", "print"},
357351
expression_list = {{type = "variable", "i"}}
358-
},
352+
}},
359353
variable_list = {{type = "variable", "i"}}
360354
}
361355
})
@@ -465,25 +459,25 @@ describe("lexer", function()
465459
it("can parse simple if statements", function()
466460
assert.same(lexer:match("if x print('test');"), {{type = "if",
467461
condition = {type = "variable", "x"},
468-
{type = "function_call",
462+
{type = "function_call", {
469463
{type = "variable", "print"},
470464
expression_list = {{type = "sqstring", "test"}}
471-
},
465+
}},
472466
["elseif"] = {}
473467
}})
474468
end)
475469
it("can parse complex if statements", function()
476470
assert.same(lexer:match("if x print('test'); else print(x);"), {
477471
{type = "if",
478472
condition = {type = "variable", "x"},
479-
{type = "function_call",
473+
{type = "function_call", {
480474
{type = "variable", "print"},
481475
expression_list = {{type = "sqstring", "test"}}
482-
},
483-
['else'] = {type = "function_call",
476+
}},
477+
['else'] = {type = "function_call", {
484478
{type = "variable", "print"},
485479
expression_list = {{type = "variable", "x"}}
486-
},
480+
}},
487481
["elseif"] = {}
488482
}
489483
})

spec/out/source/class.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ c:print()
3737
Example.print(c)
3838
ExampleThree = class({
3939
__init = (function(self, a, b)
40-
ExampleToo.__init(self, a, b)
40+
self.__super.__init(self, a, b)
4141
end);
4242
}, {extends = ExampleToo}, "ExampleThree")
4343
X = {}

spec/out/source/functions.lua

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ a.b()
44
a[b]()
55
a:b()
66
a.b:c()
7-
d.c(a.b)
8-
c.b(a)
9-
for line in io.lines("test") do
10-
print(line)
11-
end
12-
for line in io.lines("test") do
13-
print(line)
14-
end
15-
for k, v in pairs(x) do
16-
print(k, v)
17-
end
7+
a.b:c()
8+
a:b()
9+
a().b()
10+
a():b()
11+
self:a().b:c()
12+
a:b(as, df, gh):qwerty(potato)
13+
self.a:b(c.d.e:f(h, i):j(k, l).m:n(o))
1814
function a_1()
1915
return b
2016
end

0 commit comments

Comments
 (0)