1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

First test batch and associated fixes

The test runner is also nicer to use.
This commit is contained in:
Étienne Fildadut 2023-12-28 16:51:18 +01:00
parent 10084dec23
commit 82ce53be83
154 changed files with 1586 additions and 78 deletions

View file

@ -23,7 +23,9 @@ local VariableMetadata = ast.abstract.Runtime {
end
end,
set = function(self, state, value)
assert(not self.symbol.constant, ("trying to change the value of constant %s"):format(self.symbol.string))
if self.symbol.constant then
error(("trying to change the value of constant %s"):format(self.symbol.string), 0)
end
if self.symbol.type_check then
local r = self.symbol.type_check:call(state, ArgumentTuple:new(value))
if not r:truthy() then error(("type check failure for %s; %s does not satisfy %s"):format(self.symbol.string, value, self.symbol.type_check)) end
@ -82,7 +84,7 @@ local Environment = ast.abstract.Runtime {
define = function(self, state, symbol, exp)
local name = symbol.string
if self:defined_in_current(state, symbol) then
error(name.." is already defined in the current scope")
error(name.." is already defined in the current scope", 0)
end
if (self.partial and not self.partial[name])
or (self.export ~= symbol.exported) then
@ -107,7 +109,7 @@ local Environment = ast.abstract.Runtime {
elseif Overloadable:issub(val) then
exp = Overload:new(exp, val)
elseif self:defined_in_current(state, symbol) then
error(("can't add an overload variant to non-overloadable variable %s defined in the same scope"):format(identifier))
error(("can't add an overload variant to non-overloadable variable %s defined in the same scope"):format(identifier), 0)
end
end

View file

@ -49,13 +49,13 @@ List = ast.abstract.Runtime {
get = function(self, state, index)
local list = self.branched:get(state)
if index < 0 then index = #list.list + 1 + index end
if index > #list.list or index == 0 then error("list index out of bounds") end
if index > #list.list or index == 0 then error("list index out of bounds", 0) end
return list.list[index]
end,
set = function(self, state, index, val)
local list = self:_prepare_branch(state)
if index < 0 then index = #list.list + 1 + index end
if index > #list.list or index == 0 then error("list index out of bounds") end
if index > #list.list+1 or index == 0 then error("list index out of bounds", 0) end
list.list[index] = val
end,
insert = function(self, state, val)

View file

@ -23,7 +23,8 @@ local TupleToStruct = ast.abstract.Node {
_eval = function(self, state)
local t = Struct:new()
for i, e in ipairs(self.tuple.list) do
local tuple = self.tuple:eval(state)
for i, e in ipairs(tuple.list) do
if Pair:is(e) then
t:set(e.name, e.value)
else

View file

@ -58,7 +58,7 @@ Tuple = ast.abstract.Node {
get = function(self, index)
if index < 0 then index = #self.list + 1 + index end
if index > #self.list or index == 0 then error("tuple index out of bounds") end
if index > #self.list or index == 0 then error("tuple index out of bounds", 0) end
return self.list[index]
end
}

21
run tests.sh Executable file
View file

@ -0,0 +1,21 @@
#!/bin/sh
# Run the test suite accross supported Lua versions.
# Note that the test suite require luafilesystem: `luarocks --lua-version=5.4 install luafilesystem`
echo "/----------------------------\\"
echo "| Running tests with Lua 5.4 |"
echo "\\----------------------------/"
lua5.4 test/run.lua
echo ""
echo "/----------------------------\\"
echo "| Running tests with Lua 5.3 |"
echo "\\----------------------------/"
lua5.3 test/run.lua
echo ""
echo "/---------------------------\\"
echo "| Running tests with LuaJIT |"
echo "\\---------------------------/"
luajit test/run.lua

View file

@ -21,9 +21,8 @@ return {
"_~_", "(condition, expression)", function(state, condition, expression)
ensure_if_variable(state)
if condition:truthy() then
local r = expression:call(state, ArgumentTuple:new())
set_if_variable(state, true)
return r
return expression:call(state, ArgumentTuple:new())
else
set_if_variable(state, false)
return Nil:new()
@ -37,9 +36,8 @@ return {
if last_if_success(state) then
return Nil:new()
else
local r = expression:call(state, ArgumentTuple:new())
set_if_variable(state, true)
return r
return expression:call(state, ArgumentTuple:new())
end
end
},

View file

@ -1,5 +1,5 @@
local ast = require("ast")
local Tuple, Table, Struct, ArgumentTuple = ast.Tuple, ast.Table, ast.Struct, ast.ArgumentTuple
local Tuple, Table, Struct, ArgumentTuple, Nil = ast.Tuple, ast.Table, ast.Struct, ast.ArgumentTuple, ast.Nil
local tag_manager = require("state.tag_manager")
@ -14,6 +14,8 @@ return {
tags_struct = tags
elseif Table:is(tags) then
tags_struct = tags:to_struct(state)
elseif Nil:is(tags) then
tags_struct = Struct:new()
else
tags_struct = Struct:from_tuple(Tuple:new(tags)):eval(state)
end

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"" {}"-3" {}""|
--- text ---
| {}"" {}"heh minus lol" {}""|
--- text ---
| {}"" {}"generic minus" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"3" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,15 @@
--# run #--
--- choice ---
> | {}"ye "|
=> | {}"ne "|
--- text ---
| {}"ok"|
--- choice ---
=> | {}"ho "|
> | {}"oh "|
--- text ---
| {}"plop"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,12 @@
--# run #--
--- choice ---
> | {}"ho "|
> | {}"neol "|
=> | {}"oh "|
> | {}"neol "|
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {1:1}"A"|
--- choice ---
=> | {}"Suprise choice! "|
> | {}"Press " {}"JOIN" {}" to jump. "|
> | {}"No "|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,12 @@
--# run #--
--- text ---
| {1:1}"a"|
--- choice ---
=> | {}"Press " {}"SPLIT" {}" to jump. "|
> | {}"No "|
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,19 @@
--# run #--
--- choice ---
=> | {}"Press " {1:1}"A" {}" to jump. "|
> | {}"No "|
--- text ---
| {}"ok"|
--- choice ---
=> | {}"Other "|
--- text ---
| {}"ok"|
| {1:1}"left"|
--- choice ---
=> | {}"Use " {}" joystick" {}" to move. "|
--- text ---
| {}"ko"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,18 @@
--# run #--
--- choice ---
=> | {1:42}"a "|
> | {}"c "|
--- text ---
| {1:42}"b"|
--- choice ---
=> | {1:42}"a "|
> | {"k":"v"}"d "|
--- text ---
| {1:42}"b"|
| {"k":"v"}"e"|
--- text ---
| {}"f"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,10 @@
--# run #--
--- choice ---
> | {}"ye "|
=> | {}"ne "|
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

11
test/results/closure.ans Normal file
View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"" {}"5" {}" = 5"|
| {}"" {}"8" {}" = 8"|
--- text ---
| {}"" {}"4" {}" = 4"|
| {}"" {}"7" {}" = 7"|
--- return ---
()
--# saved #--
{}

5
test/results/comment.ans Normal file
View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
8
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {}"ok"|
| {}"ok bis"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"a " {}"b" {}" c"|
--- text ---
| {}"a " {}"()" {}" c"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"ok"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- error ---
./state/State.lua:145: trying to change the value of constant a
↳ from test/tests/constant variable.ans:5:3 in assignment: a = 52
↳ from ? in block: ::a = 3…
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {}"" {}"\"Name: Darmanin\\\
Age: 38\"" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,4 @@
--# parse error #--
a is already defined in the current scope
↳ from test/tests/define override function.ans:4:4 in definition: :a = 2
↳ from ? in block: :a = ($() _)…

View file

@ -0,0 +1,5 @@
--# parse error #--
can't add an overload variant to non-overloadable variable a defined in the same scope
↳ from test/tests/define override variable.ans:3:1 in definition: :a = ($() _)
↳ from test/tests/define override variable.ans:3:1 in attach block: :a = ($() _)…
↳ from ? in block: :a = 2…

View file

@ -0,0 +1,4 @@
--# parse error #--
a is already defined in the current scope
↳ from test/tests/define override.ans:3:4 in definition: :a = 2
↳ from ? in block: :a = 5…

5
test/results/define.ans Normal file
View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
()
--# saved #--
{}

13
test/results/flush.ans Normal file
View file

@ -0,0 +1,13 @@
--# run #--
--- text ---
| {}"a"|
--- choice ---
=> | {}"b "|
--- text ---
| {}"c"|
--- choice ---
=> | {}"d "|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"ok" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- error ---
./state/State.lua:145: can't call closure ($(a, b) _): expected 2 arguments, received 1
↳ from test/tests/function args arity check fail.ans:4:2 in call: f("ok")
↳ from ? in block: :f = ($(a, b) _)…
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"o" {}"" {}"k" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,15 @@
--# run #--
--- text ---
| {}"" {}"5" {}""|
--- text ---
| {}"v=" {}"50" {}""|
--- text ---
| {}"" {}"50" {}""|
--- text ---
| {}"v2=" {}"ok" {}""|
--- text ---
| {}"" {}"3" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,15 @@
--# run #--
--- text ---
| {}"" {}"25" {}" = " {}"25" {}""|
--- text ---
| {}"" {}"4" {}" = " {}"4" {}""|
--- text ---
| {}"" {}"14" {}" == 14"|
--- text ---
| {}"" {}"32" {}" == 32"|
--- text ---
| {}"" {}"49" {}" == 49"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"a"|
--- text ---
| {}"x"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"5" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"5" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- error ---
./state/State.lua:145: identifier "b" is undefined in branch 0a138a38-3faa-4478-10f6f-1a9de1e0a8e1
↳ from test/tests/function scope wrong.ans:4:7 in identifier: b
↳ from test/tests/function scope wrong.ans:4:1 in text interpolation: | a: {b}|
↳ from test/tests/function scope wrong.ans:4:1 in translatable: | a: {b}|
↳ from ? in block: :a = ($() _)…
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"a: " {}"5" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"plopheh" {}""|
--- text ---
| {}"" {}"4" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- error ---
./state/State.lua:145: can't call overload overload<($(a::($(x) <lua function>)) _), ($(x::($(x) <lua function>)) _)>: more than one function match (5), matching functions were at least (specificity 1.3):
• (x::($(x) <lua function>))
• (a::($(x) <lua function>))
↳ from test/tests/function type dispatch ambigous.ans:7:3 in call: fn(5)
↳ from ? in block: :fn = ($(x::number) _)…
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"a"|
--- text ---
| {}"x"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"ok" {}""|
--- text ---
| {}"" {}"ok" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"o" {}"" {}"k" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"ok"|
--- text ---
| {}"ok2"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,15 @@
--# run #--
--- text ---
| {}"" {}"8" {}" = 8"|
--- text ---
| {}"" {}"12" {}" = 12"|
--- text ---
| {}"" {}"6.28" {}" = 2pi"|
--- text ---
| {}"" {}"0.125" {}" = 0.125"|
--- text ---
| {}"" {}"2.1" {}" = 2.1"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,33 @@
--# run #--
--- text ---
| {}"a"|
| {}"b"|
| {}"" {}"()" {}" = a b ()"|
--- text ---
| {}"b"|
| {}"" {}"()" {}" = b ()"|
--- text ---
| {}"a"|
| {}"a"|
| {}"" {}"1" {}" = a a 1"|
--- text ---
| {}"b"|
| {}"" {}"()" {}" = b ()"|
--- text ---
| {}"a"|
| {}"" {}"1" {}" = a 1"|
--- text ---
| {}"b"|
| {}"a"|
| {}"" {}"1" {}" = b a 1"|
--- text ---
| {}"a"|
| {}"" {}"1" {}" = a 1"|
--- text ---
| {}"b"|
| {}"b"|
| {}"" {}"()" {}" = b b ()"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,17 @@
--# run #--
--- text ---
| {}"" {}"*[1, 2]" {}""|
--- text ---
| {}"" {}"*[3, 2]" {}""|
--- text ---
| {}"" {}"*[3, 5]" {}""|
--- text ---
| {}"" {}"*[3, 12]" {}""|
--- text ---
| {}"" {}"*[3, 12, 99]" {}""|
--- error ---
./state/State.lua:145: list index out of bounds
↳ from test/tests/list assignement.ans:21:6 in call: x(5) = 0
↳ from ? in block: :x = *[1, 2]…
--# saved #--
{}

View file

@ -0,0 +1,17 @@
--# run #--
--- text ---
| {}"" {}"[1, 2, 3]" {}""|
--- text ---
| {}"" {}"1" {}" == " {}"1" {}""|
--- text ---
| {}"" {}"2" {}" == " {}"2" {}""|
--- text ---
| {}"" {}"3" {}" == " {}"3" {}""|
--- error ---
./state/State.lua:145: tuple index out of bounds
↳ from test/tests/list index.ans:11:4 in call: x(-4)
↳ from test/tests/list index.ans:11:1 in text interpolation: | {x(-4)}|
↳ from test/tests/list index.ans:11:1 in translatable: | {x(-4)}|
↳ from ? in block: :x = [1, 2, 3]…
--# saved #--
{}

View file

@ -0,0 +1,28 @@
--# run #--
--- text ---
| {}"" {}"1" {}"" {}"\
" {}""|
| {}"" {}"2" {}"" {}"\
" {}""|
| {}"" {}"3" {}"" {}"\
" {}""|
| {}"" {}"4" {}"" {}"\
" {}""|
| {}"" {}"5" {}"" {}"\
" {}""|
| {}"" {}"6" {}"" {}"\
" {}""|
| {}"" {}"7" {}"" {}"\
" {}""|
| {}"" {}"8" {}"" {}"\
" {}""|
| {}"" {}"9" {}"" {}"\
" {}""|
| {}"" {}"10" {}"" {}"\
" {}""|
--- text ---
| {}"" {}"11" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,23 @@
--# run #--
--- text ---
| {}"" {}"*{1:1, 2:2}" {}""|
--- text ---
| {}"" {}"()" {}""|
--- text ---
| {}"" {}"*{1:3, 2:2}" {}""|
--- text ---
| {}"" {}"()" {}""|
--- text ---
| {}"" {}"*{\"foo\":\"a\", 1:3, 2:2}" {}""|
--- text ---
| {}"" {}"()" {}""|
--- text ---
| {}"" {}"*{\"bar\":\"b\", \"foo\":\"a\", 1:3, 2:2}" {}""|
--- text ---
| {}"" {}"()" {}""|
--- text ---
| {}"" {}"*{\"bar\":\"b\", \"foo\":\"c\", 1:3, 2:2}" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"" {}"{\"ahah\":23, \"k\":23, 3:12}" {}" == " {}"{" {}"3=12, ahah=23, k=23}"|
--- text ---
| {}"" {}"12" {}" == 12"|
--- text ---
| {}"" {}"23" {}" == 23"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"abc" {}" = " {}"abc" {}" = " {}"abc" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"5" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,10 @@
--# run #--
--- text ---
| {}"yes"|
--- text ---
| {}"ye"|
| {}"da"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,20 @@
--# run #--
--- text ---
| {}"a"|
--- text ---
| {}"b"|
--- text ---
| {}"c"|
| {}"d"|
--- text ---
| {}"e"|
--- choice ---
=> | {}"f "|
--- text ---
| {}"g"|
--- choice ---
=> | {}"h "|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"" {}"abc" {}" = " {}"abc" {}" = " {}"abc" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"[\"test\":1, 3:\"p\", \"test\":\"foo\", \"ho\":\"ah\", \"test\":\"test\"]" {}""|
--- text ---
| {}"" {}"[\"name\":1, 3:\"p\", \"test\":\"foo\", \"ho\":\"ah\", \"name\":\"test\"]" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,10 @@
--# run #--
--- choice ---
=> | {}"a "|
--- text ---
| {}"x"|
| {}"Yes."|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,14 @@
--# run #--
--- text ---
| {}"expression " {}"a" {}""|
--- text ---
| {}"quote " {}"\"" {}""|
--- text ---
| {}"other codes " {}"\
" {}" " {}"\\" {}" " {}"\9" {}" " {}"{" {}"braces}"|
--- text ---
| {}"" {}"escaping expressions abc and {stuff} \\ and quotes \"" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {1:1}"foo"|
| {"a":[2, 3], "b":[1, 2], 1:1}"bar"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {1:1}"foo"|
| {"a":[2, 3], 1:1}"bar"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {1:1}"foo"|
| {1:1}"bar"|
--- return ---
()
--# saved #--
{}

8
test/results/tag.ans Normal file
View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {1:1}"foo"|
| {"a":[2, 3], 1:1}"bar"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"a"|
| {}"b c"|
--- text ---
| {}"a"|
| {}"b c"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"a"|
--- text ---
| {}"b c"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,16 @@
--# run #--
--- text ---
| {}"expression " {}"{" {}"a}"|
--- text ---
| {}"quote " {}"\"" {}""|
--- text ---
| {}"other codes " {}"\
" {}" " {}"\\" {}" " {}"\9" {}""|
--- text ---
| {}"decorators " {}"#" {}" tag " {}"~" {}" condition " {}"$" {}" fn"|
--- text ---
| {}"sub " {}"[" {}"text]"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"a: " {}"5" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,19 @@
--# run #--
--- text ---
| {1:1}"A"|
--- choice ---
=> | {}"Surprise choice! "|
--- text ---
| {}"ok"|
| {}"Press " {}"()" {}" to jump."|
--- text ---
| {1:1}"left"|
--- choice ---
=> | {}"Surprise choice! "|
--- text ---
| {}"ok2"|
| {}"Use " {}" joystick" {}" to move."|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,13 @@
--# run #--
--- text ---
| {1:1}"A"|
--- text ---
| {}"Press " {}"()" {}" to jump."|
--- text ---
| {1:1}"left"|
--- text ---
| {}"Use " {}" joystick" {}" to move."|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,10 @@
--# run #--
--- text ---
| {}"Press " {1:1}"A" {}" to jump."|
--- text ---
| {1:2}"left"|
| {}"Use " {}" joystick" {}" to move."|
--- return ---
()
--# saved #--
{}

7
test/results/text.ans Normal file
View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"a"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"" {}"-5" {}""|
--- text ---
| {}"" {}"minus lol" {}""|
--- text ---
| {}"" {}"generic minus" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,19 @@
--# run #--
--- text ---
| {}"Start with i=" {}"1" {}":"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"2" {}""|
--- text ---
| {}"" {}"3" {}""|
--- text ---
| {}"" {}"4" {}""|
--- text ---
| {}"Start with i=" {}"5" {}":"|
--- text ---
| {}"Loop not ran."|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,41 @@
--# run #--
--- text ---
| {}"" {}"0" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"2" {}""|
--- text ---
| {}"" {}"3" {}""|
--- text ---
| {}"" {}"4" {}""|
--- text ---
| {}"" {}"5" {}""|
--- text ---
| {}"" {}"6" {}""|
--- text ---
| {}"" {}"7" {}""|
--- text ---
| {}"" {}"8" {}""|
--- text ---
| {}"" {}"9" {}""|
--- text ---
| {}"" {}"10" {}""|
--- text ---
| {}"return in loop:"|
--- text ---
| {}"" {}"0" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"2" {}""|
--- text ---
| {}"" {}"3" {}""|
--- text ---
| {}"" {}"4" {}""|
--- text ---
| {}"" {}"5" {}""|
--- return ---
@()
--# saved #--
{}

View file

@ -1,8 +1,27 @@
-- Require LuaFileSystem: luarocks install luafilesystem
local lfs = require("lfs")
local anselme = require("anselme")
local persistent_manager = require("state.persistent_manager")
-- simple random to get the same result across lua versions
local prev = 0
local function badrandom(a, b)
prev = (4241 * prev + 11) % 6997
return a + prev % (b-a+1)
end
function math.random(a, b)
if not a and not b then
return badrandom(0, 999) / 1000
elseif not b then
return badrandom(1, a)
else
return badrandom(a, b)
end
end
-- run a test file and return the result
local function run(path)
local state = anselme:new()
state:load_stdlib()
@ -10,9 +29,13 @@ local function run(path)
local run_state = state:branch()
local f = assert(io.open(path, "r"))
local block = anselme.parse(f:read("*a"), path)
local s, block = pcall(anselme.parse, f:read("*a"), path)
f:close()
if not s then
return "--# parse error #--\n"..tostring(block)
end
run_state:run(block)
local out = { "--# run #--" }
@ -48,6 +71,29 @@ local function run(path)
return table.concat(out, "\n")
end
-- display an animated loading indicator
io.stdout:setvbuf("no")
local loading = {
loop = { "", "", "", "", "", "", "", "" },
loop_pos = 1,
erase_code = "",
write = function(self, message)
self:clear()
local str = self.loop[self.loop_pos].." "..message
self.erase_code = ("\b"):rep(#str)
io.write(str)
end,
clear = function(self)
io.write(self.erase_code)
end,
update = function(self)
self.loop_pos = self.loop_pos + 1
if self.loop_pos > #self.loop then self.loop_pos = 1 end
end
}
-- list tests
local tests = {}
for test in lfs.dir("test/tests/") do
if test:match("%.ans$") then
@ -55,89 +101,85 @@ for test in lfs.dir("test/tests/") do
end
end
if arg[1] == "help" then
print("usage:")
print("lua test/run.lua: run all the tests")
print("lua test/run.lua write: write missing result files")
print("lua test/run.lua help: display this message")
elseif arg[1] == "write" then
for _, test in ipairs(tests) do
local f = io.open(test:gsub("^test/tests/", "test/results/"), "r")
if f then
f:close()
else
-- run!
if not arg[1] or arg[1] == "update" then
local total, failure, errored, notfound = #tests, 0, 0, 0
for i, test in ipairs(tests) do
repeat
local rerun = false
-- load result
local f = io.open(test:gsub("^test/tests/", "test/results/"), "r")
local expected
if f then
expected = f:read("*a")
f:close()
end
-- run test
local result = run(test)
if result ~= expected then
loading:clear()
print("* "..test)
if expected then failure = failure + 1
else notfound = notfound + 1
end
if arg[1] == "update" then
local c = assert(io.open(test, "r"))
local code = c:read("*a")
c:close()
print(" Code:")
print(" "..code:gsub("\n", "\n "))
local s, result = pcall(run, test)
if not s then
print(" Unexpected error: "..tostring(result))
local r
repeat
io.write("Edit this file? (y/N) ")
r = io.read("*l"):lower()
until r == "y" or r == "n" or r == ""
if r == "y" then
os.execute(("micro %q"):format(test)) -- hardcoded but oh well
rerun = true
end
if expected then
print(" Expected result: \n "..expected:gsub("\n", "\n "))
print(" But received: \n "..result:gsub("\n", "\n "))
else
print(" Result:")
print(" "..result:gsub("\n", "\n "))
print(" No result file found, generated result: \n "..result:gsub("\n", "\n "))
end
if arg[1] == "update" then
local r
repeat
if expected then
io.write("Update this result? (y/N/e) ")
else
io.write("Write this result? (y/N/e) ")
end
r = io.read("*l"):lower()
until r == "y" or r == "n" or r == "e" or r == ""
if r == "y" then
local o = assert(io.open(test:gsub("^test/tests/", "test/results/"), "w"))
o:write(result)
o:close()
if expected then failure = failure - 1
else notfound = notfound - 1
end
elseif r == "e" then
os.execute(("micro %q"):format(test)) -- hardcoded but oh well
rerun = true
if expected then failure = failure - 1
else notfound = notfound - 1
end
end
end
print("")
end
until not rerun
end
end
elseif not arg[1] then
local total, failure, errored, notfound = #tests, 0, 0, 0
for _, test in ipairs(tests) do
local f = io.open(test:gsub("^test/tests/", "test/results/"), "r")
if f then
local s, result = pcall(run, test)
if not s then
errored = errored + 1
print("* "..test)
print(" Unexpected error: "..tostring(result))
else
local expected = f:read("*a")
if result ~= expected then
failure = failure + 1
print("* "..test)
print(" Expected: \n "..expected:gsub("\n", "\n "))
print(" But received: \n "..result:gsub("\n", "\n "))
print("")
end
end
f:close()
else
notfound = notfound + 1
print("* "..test)
print(" Result file not found.")
print("")
end
-- status
loading:write(("%s/%s tests ran"):format(i, #tests))
if i % 10 == 0 then loading:update() end
end
loading:clear()
print("#### Results ####")
print(("%s successes, %s failures, %s errors, %s missing result files, out of %s tests"):format(total-failure-notfound-errored, failure, errored, notfound, total))
local successes = total-failure-notfound-errored
print(("%s successes, %s failures, %s errors, %s missing result files, out of %s tests"):format(successes, failure, errored, notfound, total))
if successes < total then os.exit(1) end
else
print("unknown command, run `lua test/run.lua help` for usage")
print("usage:")
print("lua test/run.lua: run all the tests")
print("lua test/run.lua update: run all tests, optionally updating incorrect or missing results")
print("lua test/run.lua help: display this message")
end

View file

@ -0,0 +1,11 @@
:$ a - b
@"generic minus"
:$ a::string - b::string
@a + " minus " + b
| {2-5}
| {"heh"-"lol"}
| {[]-[]}

View file

@ -0,0 +1,7 @@
:c = 1
|{c}
c += 2
|{c}

View file

@ -0,0 +1,11 @@
:@choice = 2
| ye |>
| no
| ne |>
| ok
choice = 1
| ho |>
| plop
| oh |>
| plup

View file

@ -0,0 +1,11 @@
:$ f
| neol |>
| nah
| ho |>
|plop
f!
| oh |>
|ok
f!
:@choice=3

View file

@ -0,0 +1,11 @@
:@choice=1
:$ jump button
1 # |A
| Suprise choice! |> ()
@"JOIN"
| Press {jump button!} to jump. |>
|ok
| No |>
|ko

View file

@ -0,0 +1,11 @@
:@choice=1
:$ jump button
1 # | a
@"SPLIT"
| Press {jump button!} to jump. |>
| ok
| No |>
| ko

View file

@ -0,0 +1,17 @@
:$ jump button
@1 # | A
:$ move axis
1 # | left
@" joystick"
| Press {jump button!} to jump. |>
|ok
| No |>
|ko
:@choice = 1
| Other |>
|ok
| Use {move axis!} to move. |>
|ko

View file

@ -0,0 +1,16 @@
:choice = 1
:$ f
42 #
| a |>
| b
f!
| c |> ()
"k":"v" #
f!
| d |> ()
| e
| f

View file

@ -0,0 +1,5 @@
| ye |>
| no
| ne |>
| ok
:@choice = 2

12
test/tests/closure.ans Normal file
View file

@ -0,0 +1,12 @@
:$ f(x)
$(y) x + y
:add two = f(2)
:add five = f(5)
|{add two(3)} = 5
|{add five(3)} = 8
|{add two(2)} = 4
|{add five(2)} = 7

9
test/tests/comment.ans Normal file
View file

@ -0,0 +1,9 @@
((hey couic + 5))
((nested ((comments)) d))
2 ((end of line))
((start of line)) 3
5 + ((middle)) 3

View file

@ -0,0 +1,3 @@
() ~ |ko
1 ~ |ok
1 ~ |ok bis

View file

@ -0,0 +1,6 @@
:a = 5
a == 2 ~
|ko
~
|ok

View file

@ -0,0 +1,6 @@
:a = 5
a == 5 ~
|ok
~
|ko

View file

@ -0,0 +1,8 @@
:a = 5
a == 2 ~
|ko
~ () ~
|ko
~
|ok

View file

@ -0,0 +1,8 @@
:a = 5
a == 2 ~
|ko
~ 1 ~
|ok
~
|ko

View file

@ -0,0 +1,4 @@
:a = 5
a == 2 ~
|ko

View file

@ -0,0 +1,6 @@
:$ f
@|b
|a {5 ~ f!} c
|a {() ~ f!} c

View file

@ -0,0 +1,4 @@
:a = 5
a == 5 ~
|ok

Some files were not shown because too many files have changed in this diff Show more