Skip to content

Commit 96b3f18

Browse files
committed
refactor(tests): improve readability of test cases by formatting Lua code
1 parent c113e04 commit 96b3f18

File tree

1 file changed

+99
-13
lines changed

1 file changed

+99
-13
lines changed

tests/test.lua

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,13 @@ suite:describe("Loops", function()
513513
suite:it("works with a custom iterator", function()
514514
suite:compileAndRunChecked([[
515515
local sum = 0
516-
for v in (function() local n=0; return function() n=n+1; return n<=3 and n*3 or nil end end)() do
516+
for v in (function()
517+
local n=0;
518+
return function()
519+
n=n+1;
520+
return n<=3 and n*3 or nil
521+
end
522+
end)() do
517523
sum = sum + v
518524
end
519525
return sum
@@ -549,58 +555,138 @@ suite:describe("Scoping and Closures", function()
549555

550556
suite:describe("Upvalues", function()
551557
suite:it("captures variables from an outer function", function()
552-
suite:compileAndRunChecked([[local function outer() local x=5; return function() return x end end; return outer()()]])
558+
suite:compileAndRunChecked([[
559+
local function outer()
560+
local x=5;
561+
return function()
562+
return x
563+
end
564+
end;
565+
return outer()()
566+
]])
553567
end)
554568

555569
suite:it("modifies captured upvalues", function()
556-
suite:compileAndRunChecked([[local function outer() local x=5; return function() x=x+1; return x end end; local i=outer(); i(); return i()]])
570+
suite:compileAndRunChecked([[
571+
local function outer()
572+
local x=5;
573+
return function()
574+
x=x+1;
575+
return x
576+
end
577+
end;
578+
local i=outer();
579+
i();
580+
return i()
581+
]])
557582
end)
558583

559584
suite:it("handles multi-level closures", function()
560-
suite:compileAndRunChecked([[local function l1() local a=1; return function() local b=2; return function() return a+b end end end; return l1()()()]])
585+
suite:compileAndRunChecked([[
586+
local function l1()
587+
local a=1;
588+
return function()
589+
local b=2;
590+
return function()
591+
return a+b
592+
end
593+
end
594+
end;
595+
return l1()()()
596+
]])
561597
end)
562598
end)
563599
end)
564600

565601
suite:describe("Functions", function()
566602
suite:describe("Definitions", function()
567603
suite:it("handles anonymous function expressions", function()
568-
suite:compileAndRunChecked([[local f = function() return 42 end; return f()]])
604+
suite:compileAndRunChecked([[
605+
local f = function()
606+
return 42
607+
end;
608+
return f()
609+
]])
569610
end)
570611
suite:it("handles named function syntax sugar", function()
571612
suite:compileAndRunChecked([[function f() return 1 end; return f()]])
572613
end)
573614
suite:it("handles local function syntax for recursion", function()
574-
suite:compileAndRunChecked(
575-
[[local function fact(n) if n==0 then return 1 else return n*fact(n-1) end end; return fact(5)]])
615+
suite:compileAndRunChecked([[
616+
local function fact(n)
617+
if n==0 then
618+
return 1
619+
else
620+
return n*fact(n-1)
621+
end
622+
end;
623+
return fact(5)
624+
]])
576625
end)
577626
suite:it("handles table method definitions", function()
578-
suite:compileAndRunChecked([[local t={x=10}; function t:add(y) return self.x+y end; return t:add(5)]])
627+
suite:compileAndRunChecked([[
628+
local t={x=10};
629+
function t:add(y)
630+
return self.x+y
631+
end;
632+
return t:add(5)
633+
]])
579634
end)
580635
end)
581636

582637
suite:describe("Calls", function()
583638
suite:it("handles parenthesis-less calls with a string literal", function()
584-
suite:compileAndRunChecked([[local s=""; local function f(x) s=x end; f"hello"; return s]])
639+
suite:compileAndRunChecked([[
640+
local s="";
641+
local function f(x)
642+
s=x
643+
end;
644+
f"hello";
645+
return s
646+
]])
585647
end)
586648
suite:it("handles parenthesis-less calls with a table constructor", function()
587-
suite:compileAndRunChecked([[local t; local function f(x) t=x end; f{1,2}; return t[2] ]])
649+
suite:compileAndRunChecked([[
650+
local t;
651+
local function f(x)
652+
t=x
653+
end;
654+
f{1,2};
655+
return t[2]
656+
]])
588657
end)
589658
suite:it("handles method calls with the colon syntax", function()
590-
suite:compileAndRunChecked([[local t={x=10, f=function(self,y) return self.x+y end}; return t:f(5)]])
659+
suite:compileAndRunChecked([[
660+
local t={x=10, f=function(self,y)
661+
return self.x+y
662+
end};
663+
return t:f(5)
664+
]])
591665
end)
592666
end)
593667

594668
suite:describe("Varargs", function()
595669
suite:it("captures variable arguments", function()
596-
suite:compileAndRunChecked([[local f = function(...) local t={...}; return t[2] end; return f(1,2,3)]])
670+
suite:compileAndRunChecked([[
671+
local f = function(...)
672+
local t={...};
673+
return t[2]
674+
end;
675+
return f(1,2,3)
676+
]])
597677
end)
598678
end)
599679

600680
suite:describe("Tail Calls", function()
601681
suite:it("performs tail calls to avoid stack overflow", function()
602682
suite:compileAndRunChecked([[
603-
local function f(n) if n > 0 then return f(n - 1) else return 42 end end
683+
local function f(n)
684+
if n > 0 then
685+
return f(n - 1)
686+
else
687+
return 42
688+
end
689+
end
604690
return f(2000) -- Would normally cause a stack overflow
605691
]])
606692
end)

0 commit comments

Comments
 (0)