Skip to content

Commit d680222

Browse files
authored
Update forth tests (#585)
1 parent e4b4a2a commit d680222

File tree

3 files changed

+261
-154
lines changed

3 files changed

+261
-154
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local function map(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function stringify(x)
10+
return ("'%s'"):format(x)
11+
end
12+
13+
return {
14+
module_name = 'forth',
15+
16+
generate_test = function(case)
17+
if case.expected.error then
18+
local template = [[
19+
assert.has.error(function()
20+
forth.evaluate({ %s })
21+
end)]]
22+
23+
return template:format(table.concat(map(case.input.instructions, stringify), ' '), case.expected.error)
24+
elseif case.input.instructions then
25+
local template = [[
26+
assert.are.same({ %s }, forth.evaluate({ %s }))]]
27+
28+
return template:format(table.concat(map(case.expected, tostring), ', '),
29+
table.concat(map(case.input.instructions, stringify), ', '))
30+
else
31+
local template = [[
32+
assert.are.same({ %s }, forth.evaluate({ %s }))
33+
assert.are.same({ %s }, forth.evaluate({ %s }))]]
34+
35+
return template:format(table.concat(map(case.expected[1], tostring), ', '),
36+
table.concat(map(case.input.instructionsFirst, stringify), ', '),
37+
table.concat(map(case.expected[2], tostring), ', '),
38+
table.concat(map(case.input.instructionsSecond, stringify), ', '))
39+
end
40+
end
41+
}

exercises/practice/forth/.meta/tests.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ description = "addition -> errors if there is nothing on the stack"
2424
[06efb9a4-817a-435e-b509-06166993c1b8]
2525
description = "addition -> errors if there is only one value on the stack"
2626

27+
[1e07a098-c5fa-4c66-97b2-3c81205dbc2f]
28+
description = "addition -> more than two values on the stack"
29+
2730
[09687c99-7bbc-44af-8526-e402f997ccbf]
2831
description = "subtraction -> can subtract two numbers"
2932

@@ -33,6 +36,9 @@ description = "subtraction -> errors if there is nothing on the stack"
3336
[b3cee1b2-9159-418a-b00d-a1bb3765c23b]
3437
description = "subtraction -> errors if there is only one value on the stack"
3538

39+
[2c8cc5ed-da97-4cb1-8b98-fa7b526644f4]
40+
description = "subtraction -> more than two values on the stack"
41+
3642
[5df0ceb5-922e-401f-974d-8287427dbf21]
3743
description = "multiplication -> can multiply two numbers"
3844

@@ -42,6 +48,9 @@ description = "multiplication -> errors if there is nothing on the stack"
4248
[8ba4b432-9f94-41e0-8fae-3b3712bd51b3]
4349
description = "multiplication -> errors if there is only one value on the stack"
4450

51+
[5cd085b5-deb1-43cc-9c17-6b1c38bc9970]
52+
description = "multiplication -> more than two values on the stack"
53+
4554
[e74c2204-b057-4cff-9aa9-31c7c97a93f5]
4655
description = "division -> can divide two numbers"
4756

@@ -57,12 +66,21 @@ description = "division -> errors if there is nothing on the stack"
5766
[d5547f43-c2ff-4d5c-9cb0-2a4f6684c20d]
5867
description = "division -> errors if there is only one value on the stack"
5968

69+
[f224f3e0-b6b6-4864-81de-9769ecefa03f]
70+
description = "division -> more than two values on the stack"
71+
6072
[ee28d729-6692-4a30-b9be-0d830c52a68c]
6173
description = "combined arithmetic -> addition and subtraction"
6274

6375
[40b197da-fa4b-4aca-a50b-f000d19422c1]
6476
description = "combined arithmetic -> multiplication and division"
6577

78+
[f749b540-53aa-458e-87ec-a70797eddbcb]
79+
description = "combined arithmetic -> multiplication and addition"
80+
81+
[c8e5a4c2-f9bf-4805-9a35-3c3314e4989a]
82+
description = "combined arithmetic -> addition and multiplication"
83+
6684
[c5758235-6eef-4bf6-ab62-c878e50b9957]
6785
description = "dup -> copies a value on the stack"
6886

0 commit comments

Comments
 (0)