-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilstests.lua
More file actions
77 lines (65 loc) · 2.37 KB
/
utilstests.lua
File metadata and controls
77 lines (65 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require("lexer")
require("parser")
require("busted.runner")
function CreateProgram(input)
local lexer = Lexer { input = input }
local parser = Parser { lexer = lexer }
local program = parser:parseProgram()
CheckParserErrors(parser)
return program
end
function CheckParserErrors(parser)
local errors = parser:getErrors()
if #errors ~= 0 then
error(string.format("parser has %s errors: %s\n", #errors, table.concat(errors, "\n")))
end
end
function CountStatement(i, program, assert)
local size = #program.statements
assert.are.same(i, size)
end
function AssertLetStatement(statement, expectedIdentifier, assert)
assert.are.same("let", statement:getTokenLiteral())
assert.are.same(expectedIdentifier, statement.name.value)
assert.are.same(expectedIdentifier, statement.name:getTokenLiteral())
end
function AssertLiteralExpression(value, expectedValue, assert)
local expectedType = type(expectedValue)
if expectedType == "boolean" then
AssertBoolean(value, expectedValue, assert)
elseif expectedType == "number" then
AssertIntegerLiteral(value, expectedValue, assert)
elseif expectedType == "string" then
AssertIdentifier(value, expectedValue, assert)
else
error(string.format("type of value not handled. got=%s", expectedType))
end
end
function AssertIntegerLiteral(expression, expectedValue, assert)
assert.are.same(expectedValue, expression.value)
assert.are.same(tostring(expectedValue), expression:getTokenLiteral())
end
AssertBoolean = AssertIntegerLiteral
function AssertIdentifier(expression, expectedValue, assert)
assert.are.same(expectedValue, expression.value)
assert.are.same(expectedValue, expression:getTokenLiteral())
end
function AssertInfixExpression(expression, leftValue, operator, rightValue, assert)
AssertLiteralExpression(expression.left, leftValue, assert)
assert.are.same(operator, expression.operator)
AssertLiteralExpression(expression.right, rightValue, assert)
end
function ForEachTests(array, params, body)
local switch = {
[2] = function(entry)
body(entry[1], entry[2])
end,
[3] = function(entry)
body(entry[1], entry[2], entry[3])
end,
[4] = function(entry)
body(entry[1], entry[2], entry[3], entry[4])
end
}
ForEach(array, switch[params])
end