1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 08:39:30 +00:00

Reuse a global state with standard library already loaded in tests, and associated branch handling fixes

This commit is contained in:
Étienne Fildadut 2024-01-08 22:23:50 +01:00
parent 8ff082625a
commit c027c87dc4
10 changed files with 80 additions and 60 deletions

View file

@ -5,7 +5,7 @@
--- text ---
| {}"" {}"42" {}"" |
--- error ---
identifier "z" is undefined in branch 1c25ebb8-5027-4ccf-105b9-370d83e78fd7
identifier "z" is undefined in branch test/tests/exported variable nested.ans - run
↳ from test/tests/exported variable nested.ans:12:3 in identifier: z
↳ from test/tests/exported variable nested.ans:12:1 in text interpolation: | {z} |
↳ from ? in block: :f = ($() _)…

View file

@ -7,7 +7,7 @@
type check failure for parameter tuple
• $(range::is range) (from stdlib/for.ans:19:1):
type check failure for parameter range
• $(s::is struct) (from stdlib/for.ans:3:14):
• $(s::is struct) (from stdlib/for.ans:2:1):
type check failure for parameter s
↳ from stdlib/for.ans:3:18 in call: iter(var)
↳ from stdlib/for.ans:3:12 in definition: :iterator = iter(var)

View file

@ -1,15 +1,15 @@
--# run #--
--- text ---
| {}"a" |
--- text ---
| {}"c" |
--- text ---
| {}"a" |
--- text ---
| {}"b" |
--- text ---
| {}"c" |
--- text ---
| {}"c" |
| {}"a" |
--- return ---
()
--# saved #--
{"a.checkpoint":false, "a.run":1, "b.checkpoint":false, "b.run":1, "c.checkpoint":false, "c.run":3}
{"a.checkpoint":false, "a.run":2, "b.checkpoint":false, "b.run":1, "c.checkpoint":false, "c.run":2}

View file

@ -1,6 +1,6 @@
--# run #--
--- error ---
identifier "b" is undefined in branch 1c25ebb8-5027-4ccf-105b9-370d83e78fd7
identifier "b" is undefined in branch test/tests/function scope wrong.ans - run
↳ 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 ? in block: :a = ($() _)…

View file

@ -7,17 +7,17 @@
expected 3 arguments, received 2
• $(s::is script, k::is string) (from stdlib/script.ans:41:1):
type check failure for parameter s
• $(c::is function, s::is symbol) = v (from test/tests/function separate variable from variants.ans:10:4):
• $(c::is function, s::is symbol) = v (from stdlib/for.ans:2:1):
expected 3 arguments, received 2
• $(c::is function, s::is string) = v (from test/tests/function separate variable from variants.ans:10:4):
• $(c::is function, s::is string) = v (from stdlib/for.ans:2:1):
expected 3 arguments, received 2
• $(c::is function, s::is string) (from test/tests/function separate variable from variants.ans:10:4):
• $(c::is function, s::is string) (from stdlib/for.ans:2:1):
type check failure for parameter c
• $(c::is environment, s::is symbol) = v (from test/tests/function separate variable from variants.ans:10:4):
• $(c::is environment, s::is symbol) = v (from stdlib/for.ans:2:1):
expected 3 arguments, received 2
• $(c::is environment, s::is string) = v (from test/tests/function separate variable from variants.ans:10:4):
• $(c::is environment, s::is string) = v (from stdlib/for.ans:2:1):
expected 3 arguments, received 2
• $(c::is environment, s::is string) (from test/tests/function separate variable from variants.ans:10:4):
• $(c::is environment, s::is string) (from stdlib/for.ans:2:1):
type check failure for parameter c
↳ from test/tests/function separate variable from variants.ans:10:4 in call: f . "a"
↳ from test/tests/function separate variable from variants.ans:10:1 in text interpolation: | {f . "a"} = 2 |

View file

@ -64,6 +64,15 @@ local function run_loop(run_state, write_output, interactive)
end
end
-- create execution state
local global_state = anselme:new()
global_state:load_stdlib()
global_state:define("interrupt", "(code::is string)", function(state, code) state:interrupt(code:to_lua(state), "interrupt") return ast.Nil:new() end, true)
global_state:define("interrupt", "()", function(state) state:interrupt() return ast.Nil:new() end, true)
global_state:define("wait", "(duration::is number)", function(duration) coroutine.yield("wait", duration) end)
global_state:define("serialize", "(value)", function(state, value) return ast.String:new(value:serialize(state)) end, true)
global_state:define("deserialize", "(str::is string)", function(state, str) return ast.abstract.Node:deserialize(state, str.string) end, true)
-- run a test file and return the result
local function run(path, interactive)
local out = { "--# run #--" }
@ -73,23 +82,17 @@ local function run(path, interactive)
end
math.randomseed()
local state = anselme:new()
state:load_stdlib()
state:define("interrupt", "(code::is string)", function(state, code) state:interrupt(code:to_lua(state), "interrupt") return ast.Nil:new() end, true)
state:define("interrupt", "()", function(state) state:interrupt() return ast.Nil:new() end, true)
state:define("wait", "(duration::is number)", function(duration) coroutine.yield("wait", duration) end)
state:define("run in new branch", "(code)", function(code)
local parallel_state = state:branch()
local state = global_state:branch(path)
state:define("run in new branch", "(code)", function(state, code)
local parallel_state = state.source_branch:branch()
write_output("--# parallel script #--")
parallel_state:run(code, "parallel")
parallel_state:run(code.string, "parallel")
run_loop(parallel_state, write_output, interactive)
write_output("--# main script #--")
end)
state:define("serialize", "(value)", function(state, value) return ast.String:new(value:serialize(state)) end, true)
state:define("deserialize", "(str::is string)", function(state, str) return ast.abstract.Node:deserialize(state, str.string) end, true)
return ast.Nil:new()
end, true)
local run_state = state:branch()
local run_state = state:branch(path.." - run")
local f = assert(io.open(path, "r"))
local s, block = pcall(anselme.parse, f:read("a"), path)
@ -106,7 +109,7 @@ local function run(path, interactive)
run_loop(run_state, write_output, interactive)
if state:defined("post run check") then
local post_run_state = state:branch()
local post_run_state = state:branch(path.." - post run check")
post_run_state:run("post run check!")
write_output("--# post run check #--")