NimYAML/test/lexing.nim

198 lines
8.0 KiB
Nim
Raw Normal View History

import "../src/yaml/private/lexer"
2015-11-29 20:01:22 +00:00
import streams, unicode
import unittest
2015-12-11 21:55:21 +00:00
type BasicLexerToken = tuple[kind: YamlLexerToken, content: string]
2015-11-30 18:52:01 +00:00
template ensure(input: string, expected: openarray[BasicLexerToken]) =
var
i = 0
lex: YamlLexer
lex.open(newStringStream(input))
for token in lex.tokens:
if i >= expected.len:
echo "received more tokens than expected (next token = ",
2015-12-11 21:55:21 +00:00
token, ")"
fail()
break
2015-12-11 21:55:21 +00:00
if token != expected[i].kind:
if token == yamlError:
echo "got lexer error: " & lex.content
else:
2015-12-11 21:55:21 +00:00
echo "wrong token kind (expected ", expected[i], ", got ",
token, ")"
fail()
break
if not isNil(expected[i].content):
if lex.content != expected[i].content:
2015-12-11 21:55:21 +00:00
echo "wrong token content (", token, ": expected \"",
expected[i].content, "\", got \"", lex.content, "\")"
fail()
break
inc(i)
if i < expected.len:
echo "received less tokens than expected (first missing = ",
expected[i].kind, ")"
2015-12-11 21:55:21 +00:00
proc t(kind: YamlLexerToken, content: string): BasicLexerToken =
(kind: kind, content: content)
suite "Lexing":
test "Lexing: YAML Directive":
ensure("%YAML 1.2", [t(yamlYamlDirective, nil),
t(yamlVersionPart, "1"),
t(yamlVersionPart, "2"),
t(yamlStreamEnd, nil)])
test "Lexing: TAG Directive":
ensure("%TAG !t! tag:http://example.com/",
[t(yamlTagDirective, nil),
t(yamlTagHandle, "!t!"),
t(yamlTagURI, "tag:http://example.com/"),
t(yamlStreamEnd, nil)])
test "Lexing: Unknown Directive":
2015-11-29 15:50:27 +00:00
ensure("%FOO bar baz", [t(yamlUnknownDirective, "%FOO"),
t(yamlUnknownDirectiveParam, "bar"),
t(yamlUnknownDirectiveParam, "baz"),
t(yamlStreamEnd, nil)])
test "Lexing: Comments after Directives":
2015-11-29 15:50:27 +00:00
ensure("%YAML 1.2 # version\n# at line start\n # indented\n%FOO",
[t(yamlYamlDirective, nil),
t(yamlVersionPart, "1"),
t(yamlVersionPart, "2"),
2015-11-29 15:50:27 +00:00
t(yamlComment, " version"),
t(yamlComment, " at line start"),
t(yamlComment, " indented"),
t(yamlUnknownDirective, "%FOO"),
t(yamlStreamEnd, nil)])
test "Lexing: Directives End":
ensure("---", [t(yamlDirectivesEnd, nil),
t(yamlStreamEnd, nil)])
test "Lexing: Document End":
ensure("...", [t(yamlLineStart, nil),
t(yamlDocumentEnd, nil),
t(yamlStreamEnd, nil)])
test "Lexing: Directive after Document End":
2015-11-29 20:01:22 +00:00
ensure("content\n...\n%YAML 1.2",
[t(yamlLineStart, ""),
t(yamlScalarPart, "content"),
t(yamlLineStart, ""),
2015-11-29 20:01:22 +00:00
t(yamlDocumentEnd, nil),
t(yamlYamlDirective, nil),
t(yamlVersionPart, "1"),
t(yamlVersionPart, "2"),
2015-11-29 20:01:22 +00:00
t(yamlStreamEnd, nil)])
test "Lexing: Plain Scalar (alphanumeric)":
ensure("abA03rel4", [t(yamlLineStart, ""),
t(yamlScalarPart, "abA03rel4"),
t(yamlStreamEnd, nil)])
test "Lexing: Plain Scalar (with spaces)":
ensure("test content", [t(yamlLineStart, ""),
t(yamlScalarPart, "test content"),
t(yamlStreamEnd, nil)])
test "Lexing: Plain Scalar (with special chars)":
ensure(":test ?content -with #special !chars",
[t(yamlLineStart, nil),
t(yamlScalarPart, ":test ?content -with #special !chars"),
t(yamlStreamEnd, nil)])
test "Lexing: Plain Scalar (starting with %)":
2015-11-29 15:50:27 +00:00
ensure("---\n%test", [t(yamlDirectivesEnd, nil),
t(yamlLineStart, ""),
t(yamlScalarPart, "%test"),
2015-11-29 15:50:27 +00:00
t(yamlStreamEnd, nil)])
test "Lexing: Single Quoted Scalar":
ensure("'? test - content! '", [t(yamlLineStart, ""),
t(yamlScalar, "? test - content! "),
t(yamlStreamEnd, nil)])
test "Lexing: Single Quoted Scalar (escaped single quote inside)":
ensure("'test '' content'", [t(yamlLineStart, ""),
t(yamlScalar, "test ' content"),
2015-11-29 20:01:22 +00:00
t(yamlStreamEnd, nil)])
test "Lexing: Doubly Quoted Scalar":
ensure("\"test content\"", [t(yamlLineStart, ""),
2015-11-29 20:01:22 +00:00
t(yamlScalar, "test content"),
t(yamlStreamEnd, nil)])
test "Lexing: Doubly Quoted Scalar (escaping)":
ensure(""""\t\\\0\""""", [t(yamlLineStart, ""),
2015-11-29 20:01:22 +00:00
t(yamlScalar, "\t\\\0\""),
t(yamlStreamEnd, nil)])
test "Lexing: Doubly Quoted Scalar (unicode escaping)":
2015-11-29 20:01:22 +00:00
ensure(""""\x42\u4243\U00424344"""",
[t(yamlLineStart, ""),
2015-11-29 20:01:22 +00:00
t(yamlScalar, "\x42" & toUTF8(cast[Rune](0x4243)) &
toUTF8(cast[Rune](0x424344))),
t(yamlStreamEnd, nil)])
test "Lexing: Block Array":
ensure("""
- a
- b""", [t(yamlLineStart, ""), t(yamlDash, nil), t(yamlScalarPart, "a"),
t(yamlLineStart, ""), t(yamlDash, nil), t(yamlScalarPart, "b"),
t(yamlStreamEnd, nil)])
test "Lexing: Block Map with Implicit Keys":
ensure("""
foo: bar
herp: derp""", [t(yamlLineStart, ""), t(yamlScalarPart, "foo"),
t(yamlColon, nil), t(yamlScalarPart, "bar"),
t(yamlLineStart, ""), t(yamlScalarPart, "herp"),
t(yamlColon, nil), t(yamlScalarPart, "derp"),
t(yamlStreamEnd, nil)])
test "Lexing: Block Map with Explicit Keys":
ensure("""
? foo
: bar""", [t(yamlLineStart, ""), t(yamlQuestionmark, nil),
t(yamlScalarPart, "foo"), t(yamlLineStart, ""), t(yamlColon, nil),
t(yamlScalarPart, "bar"), t(yamlStreamEnd, nil)])
test "Lexing: Indentation":
ensure("""
foo:
bar:
- baz
- biz
herp: derp""",
[t(yamlLineStart, ""), t(yamlScalarPart, "foo"), t(yamlColon, nil),
t(yamlLineStart, " "), t(yamlScalarPart, "bar"), t(yamlColon, nil),
t(yamlLineStart, " "), t(yamlDash, nil), t(yamlScalarPart, "baz"),
t(yamlLineStart, " "), t(yamlDash, nil), t(yamlScalarPart, "biz"),
t(yamlLineStart, " "), t(yamlScalarPart, "herp"), t(yamlColon, nil),
t(yamlScalarPart, "derp"), t(yamlStreamEnd, nil)])
2015-11-29 21:43:10 +00:00
test "Lexing: Anchor":
ensure("foo: &bar", [t(yamlLineStart, ""), t(yamlScalarPart, "foo"),
2015-11-29 21:43:10 +00:00
t(yamlColon, nil), t(yamlAnchor, "bar"),
t(yamlStreamEnd, nil)])
test "Lexing: Alias":
ensure("foo: *bar", [t(yamlLineStart, ""), t(yamlScalarPart, "foo"),
2015-11-29 21:43:10 +00:00
t(yamlColon, nil), t(yamlAlias, "bar"),
2015-12-07 18:09:02 +00:00
t(yamlStreamEnd, nil)])
test "Lexing: Tag handle":
2015-12-07 18:09:02 +00:00
ensure("!t!str tagged", [t(yamlLineStart, ""), t(yamlTagHandle, "!t!"),
t(yamlTagSuffix, "str"),
t(yamlScalarPart, "tagged"),
t(yamlStreamEnd, nil)])
2015-12-07 18:09:02 +00:00
test "Lexing: Verbatim tag handle":
2015-12-07 18:09:02 +00:00
ensure("!<tag:http://example.com/str> tagged",
[t(yamlLineStart, ""),
t(yamlVerbatimTag, "tag:http://example.com/str"),
t(yamlScalarPart, "tagged"), t(yamlStreamEnd, nil)])