Commit ed3bd970 authored by Craig Barnes's avatar Craig Barnes

Refactor code in test/errors.lua

parent 1917da96
Pipeline #7685563 passed with stage
in 1 minute and 10 seconds
local ltcn = require "ltcn"
local parse = ltcn.parse
local pcall, type, assert, error = pcall, type, assert, error
local _ENV = nil
do
local t, e = parse " {1, 2, 3, true, false, -0, 'etc'} x "
local function assertError(input, line, column)
local t, e = parse(input)
assert(t == nil)
assert(e == ":1:39: Syntax error: unexpected 'x', expecting 'EOF'")
assert(type(e) == "string")
if e:find(("^:%d:%d:"):format(line, column)) == nil then
local message = 'Expected error at %d:%d, got error:\n "%s"'
error(message:format(line, column, e), 2)
end
end
do
local t, e = parse " --[[\n cmt1 \n]]\n-- cmt2\n-- cmt3\n\n oops \n {}"
assert(t == nil)
assert(e == ":7:3: Syntax error: unexpected 'oops', expecting '{'")
end
do
local t, e = parse "{ [{'x', 'y', 'z'}] = 'xyz' }"
assert(t == nil)
assert(e == ":1:4: Syntax error: unexpected '{', expecting 'Boolean', 'Number', 'String'")
end
do
local t, e = parse "{a = true, locals = true, do = false}"
assert(t == nil)
assert(e == ":1:27: Syntax error: unexpected 'do', expecting 'Boolean', 'Name', 'Number', 'String', '[', '{', '}'")
end
do
local t, e = parse "{key = 'Hello\nWorld!'}"
assert(t == nil)
assert(e == ":2:0: Syntax error: unexpected '\\n', expecting '''")
end
do
local t, e = parse "{key = 'Hello\rWorld!'}"
assert(t == nil)
assert(e == ":1:14: Syntax error: unexpected '\\r', expecting '''")
end
do
local t, e = parse ""
assert(t == nil)
assert(e == ":1:1: Syntax error: unexpected 'EOF', expecting '{'")
end
do
local t, e = parse " \n --[=[ --]]\n --]=] { \n\n"
assert(t == nil)
assert(e == ":5:0: Syntax error: unexpected 'EOF', expecting 'Boolean', 'Name', 'Number', 'String', '[', '{', '}'")
end
do
-- Either side of a float can be omitted, but never both
local t, e = parse "{x = .}"
assert(t == nil)
assert(e == ":1:6: Syntax error: unexpected '.', expecting 'Boolean', 'Number', 'String', '{'")
end
do
local t, e = parse "{x = .e+2}"
assert(t == nil)
assert(e == ":1:6: Syntax error: unexpected '.', expecting 'Boolean', 'Number', 'String', '{'")
end
do
-- Decimal numbers can't have hexadecimal fractional part
local t, e = parse "{x = 918273645.f}"
assert(t == nil)
assert(e == ":1:16: Syntax error: unexpected 'f', expecting ',', ';', '}'")
end
do
local t, e = parse "{x = 009520000.000.222}"
assert(t == nil)
assert(e == ":1:19: Syntax error: unexpected '.222', expecting ',', ';', '}'")
end
do
local t, e = pcall(parse, {})
local function assertArgError(arg1, arg2)
local t, e = pcall(parse, arg1, arg2)
assert(t == false)
assert(e == "bad argument #1: string expected, got table")
end
do
local t, e = pcall(parse, "", true)
assert(t == false)
assert(e == "bad argument #2: string expected, got boolean")
end
assert(type(e) == "string")
end
assertError(" {1, 2, 3, true, false, -0, 'etc'} x ", 1, 39)
assertError(" --[[\n cmt1 \n]]\n-- cmt2\n-- cmt3\n\n oops \n {}", 7, 3)
assertError("{ [{'x', 'y', 'z'}] = 'xyz' }", 1, 4)
assertError("{a = true, locals = true, do = false}", 1, 27)
assertError("{key = 'Hello\nWorld!'}", 2, 0)
assertError("{key = 'Hello\rWorld!'}", 1, 14)
assertError("", 1, 1)
assertError(" \n --[=[ --]]\n --]=] { \n\n", 5, 0)
assertError("{x = .}", 1, 6)
assertError("{x = .e+2}", 1, 6)
assertError("{x = 918273645.f}", 1, 16)
assertError("{x = 009520000.000.222}", 1, 19)
-- TODO: assertError('{xyz = "\256"}', 1, 9)
assertArgError({})
assertArgError(0)
assertArgError("", false)
assertArgError("", 55)
assertArgError("", {"..."})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment