NimYAML/test/tlex.nim

229 lines
8.3 KiB
Nim
Raw Normal View History

2017-02-06 19:39:04 +00:00
import ../yaml/private/lex
2016-09-10 08:30:40 +00:00
2016-09-10 10:38:03 +00:00
import unittest, strutils
2020-11-04 18:32:09 +00:00
const
tokensWithValue =
{Token.Plain, Token.SingleQuoted, Token.DoubleQuoted, Token.Literal,
2020-11-04 18:32:09 +00:00
Token.Folded, Token.Suffix, Token.VerbatimTag,
Token.UnknownDirective}
tokensWithFullLexeme =
{Token.DirectiveParam, Token.TagHandle}
tokensWithShortLexeme = {Token.Anchor, Token.Alias}
2016-09-10 08:30:40 +00:00
type
TokenWithValue = object
case kind: Token
2016-09-10 10:38:03 +00:00
of tokensWithValue:
2016-09-10 08:30:40 +00:00
value: string
2020-11-04 18:32:09 +00:00
of tokensWithFullLexeme:
lexeme: string
of tokensWithShortLexeme:
slexeme: string
of Indentation:
2016-09-10 11:38:42 +00:00
indentation: int
2016-09-10 08:30:40 +00:00
else: discard
proc actualRepr(lex: Lexer, t: Token): string =
result = $t
case t
of tokensWithValue + {Token.TagHandle}:
result.add("(" & escape(lex.evaluated) & ")")
of Indentation:
2020-11-04 18:32:09 +00:00
result.add("(" & $lex.currentIndentation() & ")")
else: discard
2016-09-10 08:30:40 +00:00
proc assertEquals(input: string, expected: varargs[TokenWithValue]) =
2016-09-10 15:33:58 +00:00
var
lex: Lexer
2016-09-10 15:33:58 +00:00
i = 0
lex.init(input)
2016-09-10 10:38:03 +00:00
for expectedToken in expected:
inc(i)
try:
lex.next()
doAssert lex.cur == expectedToken.kind, "Wrong token kind at #" & $i &
": Expected " & $expectedToken.kind & ", got " &
lex.actualRepr(lex.cur)
2016-09-11 09:28:05 +00:00
case expectedToken.kind
of tokensWithValue:
doAssert lex.evaluated == expectedToken.value, "Wrong token content at #" &
$i & ": Expected " & escape(expectedToken.value) &
", got " & escape(lex.evaluated)
2020-11-04 18:32:09 +00:00
of tokensWithFullLexeme:
doAssert lex.fullLexeme() == expectedToken.lexeme, "Wrong token lexeme at #" &
$i & ": Expected" & escape(expectedToken.lexeme) &
", got " & escape(lex.fullLexeme())
of tokensWithShortLexeme:
doAssert lex.shortLexeme() == expectedToken.slexeme, "Wrong token slexeme at #" &
$i & ": Expected" & escape(expectedToken.slexeme) &
", got " & escape(lex.shortLexeme())
of Indentation:
2020-11-04 18:32:09 +00:00
doAssert lex.currentIndentation() == expectedToken.indentation,
"Wrong indentation length at #" & $i & ": Expected " &
2020-11-04 18:32:09 +00:00
$expectedToken.indentation & ", got " & $lex.currentIndentation()
else: discard
except LexerError:
let e = (ref LexerError)(getCurrentException())
2020-11-04 18:32:09 +00:00
echo "Error at line", e.line, ", column", e.column, ":", e.msg
echo e.lineContent
assert false
2016-09-10 10:38:03 +00:00
2016-09-10 11:38:42 +00:00
proc i(indent: int): TokenWithValue =
TokenWithValue(kind: Token.Indentation, indentation: indent)
proc pl(v: string): TokenWithValue =
TokenWithValue(kind: Token.Plain, value: v)
proc sq(v: string): TokenWithValue =
TokenWithValue(kind: Token.SingleQuoted, value: v)
proc dq(v: string): TokenWithValue =
TokenWithValue(kind: Token.DoubleQuoted, value: v)
proc e(): TokenWithValue = TokenWithValue(kind: Token.StreamEnd)
proc mk(): TokenWithValue = TokenWithValue(kind: Token.MapKeyInd)
proc mv(): TokenWithValue = TokenWithValue(kind: Token.MapValueInd)
proc si(): TokenWithValue = TokenWithValue(kind: Token.SeqItemInd)
proc dy(): TokenWithValue = TokenWithValue(kind: Token.YamlDirective)
proc dt(): TokenWithValue = TokenWithValue(kind: Token.TagDirective)
proc du(v: string): TokenWithValue =
TokenWithValue(kind: Token.UnknownDirective, value: v)
proc dp(v: string): TokenWithValue =
2020-11-04 18:32:09 +00:00
TokenWithValue(kind: Token.DirectiveParam, lexeme: v)
proc th(v: string): TokenWithValue =
2020-11-04 18:32:09 +00:00
TokenWithValue(kind: Token.TagHandle, lexeme: v)
proc ts(v: string): TokenWithValue =
TokenWithValue(kind: Token.Suffix, value: v)
proc tv(v: string): TokenWithValue =
TokenWithValue(kind: Token.VerbatimTag, value: v)
proc dirE(): TokenWithValue = TokenWithValue(kind: Token.DirectivesEnd)
proc docE(): TokenWithValue = TokenWithValue(kind: Token.DocumentEnd)
proc ls(v: string): TokenWithValue = TokenWithValue(kind: Token.Literal, value: v)
proc fs(v: string): TokenWithValue = TokenWithValue(kind: Token.Folded, value: v)
proc ss(): TokenWithValue = TokenWithValue(kind: Token.SeqStart)
proc se(): TokenWithValue = TokenWithValue(kind: Token.SeqEnd)
proc ms(): TokenWithValue = TokenWithValue(kind: Token.MapStart)
proc me(): TokenWithValue = TokenWithValue(kind: Token.MapEnd)
proc sep(): TokenWithValue = TokenWithValue(kind: Token.SeqSep)
2020-11-04 18:32:09 +00:00
proc an(v: string): TokenWithValue = TokenWithValue(kind: Token.Anchor, slexeme: v)
proc al(v: string): TokenWithValue = TokenWithValue(kind: Token.Alias, slexeme: v)
2016-09-10 08:30:40 +00:00
suite "Lexer":
test "Empty document":
assertEquals("", e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Single-line scalar":
assertEquals("scalar", i(0), pl("scalar"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Multiline scalar":
assertEquals("scalar\l line two", i(0), pl("scalar line two"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Single-line mapping":
assertEquals("key: value", i(0), pl("key"), mv(), pl("value"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Multiline mapping":
assertEquals("key:\n value", i(0), pl("key"), mv(), i(2), pl("value"),
e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Explicit mapping":
assertEquals("? key\n: value", i(0), mk(), pl("key"), i(0), mv(),
pl("value"), e())
2016-09-11 09:28:05 +00:00
test "Sequence":
assertEquals("- a\n- b", i(0), si(), pl("a"), i(0), si(), pl("b"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Single-line single-quoted scalar":
assertEquals("'quoted scalar'", i(0), sq("quoted scalar"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Multiline single-quoted scalar":
assertEquals("'quoted\l multi line \l\lscalar'", i(0),
sq("quoted multi line\lscalar"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Single-line double-quoted scalar":
assertEquals("\"quoted scalar\"", i(0), dq("quoted scalar"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Multiline double-quoted scalar":
assertEquals("\"quoted\l multi line \l\lscalar\"", i(0),
dq("quoted multi line\lscalar"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 11:38:42 +00:00
test "Escape sequences":
assertEquals(""""\n\x31\u0032\U00000033"""", i(0), dq("\l123"), e())
2016-09-11 09:28:05 +00:00
test "Directives":
assertEquals("%YAML 1.2\n---\n%TAG\n...\n\n%TAG ! example.html",
2020-11-04 18:32:09 +00:00
dy(), dp("1.2"), dirE(), i(0), pl("%TAG"), docE(), dt(),
th("!"), ts("example.html"), e())
2016-09-11 09:28:05 +00:00
test "Markers and Unknown Directive":
2020-11-04 18:32:09 +00:00
assertEquals("---\n---\n...\n%UNKNOWN warbl", dirE(), dirE(),
docE(), du("UNKNOWN"), dp("warbl"), e())
2016-09-11 09:28:05 +00:00
test "Block scalar":
assertEquals("|\l a\l\l b\l # comment", i(0), ls("a\l\lb\l"), e())
2016-09-11 09:28:05 +00:00
test "Block Scalars":
assertEquals("one : >2-\l foo\l bar\ltwo: |+\l bar\l baz", i(0),
2020-11-04 18:32:09 +00:00
pl("one"), mv(), fs(" foo bar"), i(0), pl("two"), mv(),
ls("bar\l baz"), e())
2016-09-11 09:28:05 +00:00
2016-09-10 15:33:58 +00:00
test "Flow indicators":
assertEquals("bla]: {c: d, [e]: f}", i(0), pl("bla]"), mv(), ms(), pl("c"),
mv(), pl("d"), sep(), ss(), pl("e"), se(), mv(), pl("f"), me(), e())
test "Adjacent map values in flow style":
2020-11-04 18:32:09 +00:00
assertEquals("{\"foo\":bar, [1]\l :egg}", i(0), ms(), dq("foo"), mv(),
pl("bar"), sep(), ss(), pl("1"), se(), mv(), pl("egg"), me(), e())
2016-09-11 10:52:24 +00:00
test "Tag handles":
assertEquals("- !!str string\l- !local local\l- !e! e", i(0), si(),
th("!!"), ts("str"), pl("string"), i(0), si(), th("!"), ts("local"),
pl("local"), i(0), si(), th("!e!"), ts(""), pl("e"), e())
2016-09-11 10:52:24 +00:00
test "Literal tag handle":
assertEquals("!<tag:yaml.org,2002:str> string", i(0),
tv("tag:yaml.org,2002:str"), pl("string"), e())
2016-09-11 11:04:10 +00:00
test "Anchors and aliases":
assertEquals("&a foo: {&b b: *a, *b : c}", i(0), an("a"), pl("foo"), mv(),
ms(), an("b"), pl("b"), mv(), al("a"), sep(), al("b"), mv(), pl("c"),
me(), e())
2016-09-11 16:23:47 +00:00
test "Space at implicit key":
assertEquals("foo :\n bar", i(0), pl("foo"), mv(), i(2), pl("bar"), e())
test "inline anchor at implicit key":
assertEquals("top6: \l &anchor6 'key6' : scalar6", i(0), pl("top6"), mv(),
i(2), an("anchor6"), sq("key6"), mv(), pl("scalar6"), e())
test "adjacent anchors":
assertEquals("foo: &a\n &b bar", i(0), pl("foo"), mv(), an("a"), i(2),
an("b"), pl("bar"), e())
test "comment at empty key/value pair":
assertEquals(": # foo\nbar:", i(0), mv(), i(0), pl("bar"), mv(), e())
test "Map in Sequence":
assertEquals("""-
a: b
c: d
""", i(0), si(), i(2), pl("a"), mv(), pl("b"), i(2), pl("c"), mv(), pl("d"), e())
test "dir end after multiline scalar":
assertEquals("foo:\n bar\n baz\n---\nderp", i(0), pl("foo"), mv(), i(2),
pl("bar baz"), dirE(), i(0), pl("derp"), e())
2016-09-11 17:20:02 +00:00
test "Empty lines":
assertEquals("""block: foo
bar
baz
flow: {
foo
bar: baz
mi
}""", i(0), pl("block"), mv(), pl("foo\nbar\nbaz"),
i(0), pl("flow"), mv(), ms(), pl("foo\nbar"), mv(),
pl("baz\n\nmi"), me(), e())