NimYAML/test/parsing.nim

308 lines
15 KiB
Nim
Raw Normal View History

import "../yaml"
import unittest
proc startDoc(): YamlStreamEvent =
result.kind = yamlStartDocument
proc endDoc(): YamlStreamEvent =
result.kind = yamlEndDocument
proc scalar(content: string, typeHint: YamlTypeHint,
2015-12-27 22:40:27 +00:00
tag: TagId = yTagQuestionMark, anchor: AnchorId = yAnchorNone):
YamlStreamEvent =
result.kind = yamlScalar
result.scalarAnchor = anchor
result.scalarTag = tag
result.scalarContent = content
result.scalarType = typeHint
proc scalar(content: string,
2015-12-27 22:40:27 +00:00
tag: TagId = yTagQuestionMark, anchor: AnchorId = yAnchorNone):
YamlStreamEvent =
result = scalar(content, yTypeUnknown, tag, anchor)
2015-12-27 22:40:27 +00:00
proc startSequence(tag: TagId = yTagQuestionMark,
anchor: AnchorId = yAnchorNone):
YamlStreamEvent =
result.kind = yamlStartSequence
result.seqAnchor = anchor
result.seqTag = tag
proc endSequence(): YamlStreamEvent =
result.kind = yamlEndSequence
2015-12-27 22:40:27 +00:00
proc startMap(tag: TagId = yTagQuestionMark, anchor: AnchorId = yAnchorNone):
YamlStreamEvent =
result.kind = yamlStartMap
result.mapAnchor = anchor
result.mapTag = tag
proc endMap(): YamlStreamEvent =
result.kind = yamlEndMap
proc alias(target: AnchorId): YamlStreamEvent =
result.kind = yamlAlias
result.aliasTarget = target
proc printDifference(expected, actual: YamlStreamEvent) =
if expected.kind != actual.kind:
echo "expected " & $expected.kind & ", got " & $actual.kind
if actual.kind == yamlError:
echo "Error message: (line: ", actual.line, ", column: ",
actual.column, ") ", actual.description
elif actual.kind == yamlWarning:
echo "Warning message: " & actual.description
else:
case expected.kind
of yamlScalar:
if expected.scalarTag != actual.scalarTag:
2015-12-21 20:58:28 +00:00
echo "[\"", actual.scalarContent, "\".tag] expected tag ",
expected.scalarTag, ", got ", actual.scalarTag
elif expected.scalarAnchor != actual.scalarAnchor:
echo "[scalar] expected anchor ", expected.scalarAnchor,
", got ", actual.scalarAnchor
elif expected.scalarContent != actual.scalarContent:
2015-12-17 20:44:41 +00:00
let msg = "[scalar] expected content \"" &
expected.scalarContent & "\", got \"" &
actual.scalarContent & "\" "
2015-12-22 21:28:27 +00:00
if expected.scalarContent.len != actual.scalarContent.len:
echo msg, "(length does not match)"
else:
for i in 0..expected.scalarContent.high:
if expected.scalarContent[i] != actual.scalarContent[i]:
echo msg, "(first different char at pos ", i,
": expected ",
cast[int](expected.scalarContent[i]),
", got ",
cast[int](actual.scalarContent[i]), ")"
break
elif expected.scalarType != actual.scalarType:
echo "[scalar] expected type hint ", expected.scalarType,
", got ", actual.scalarType
else:
echo "[scalar] Unknown difference"
of yamlStartMap:
if expected.mapTag != actual.mapTag:
echo "[map.tag] expected ", expected.mapTag, ", got ",
actual.mapTag
else:
echo "[map.tag] Unknown difference"
of yamlStartSequence:
if expected.seqTag != actual.seqTag:
echo "[seq.tag] expected ", expected.seqTag, ", got ",
actual.seqTag
of yamlAlias:
if expected.aliasTarget != actual.aliasTarget:
echo "[alias] expected ", expected.aliasTarget, ", got ",
actual.aliasTarget
else:
echo "[alias] Unknown difference"
else:
echo "Unknown difference in event kind " & $expected.kind
template ensure(input: string, expected: varargs[YamlStreamEvent]) {.dirty.} =
2015-12-23 11:35:07 +00:00
var
parser = newParser(tagLib)
2015-12-23 11:35:07 +00:00
i = 0
events = parser.parse(newStringStream(input))
2015-12-23 11:35:07 +00:00
for token in events():
if i >= expected.len:
echo "received more tokens than expected (next token = ",
token.kind, ")"
fail()
break
if token != expected[i]:
echo "at token #" & $i & ":"
printDifference(expected[i], token)
fail()
break
i.inc()
suite "Parsing":
2015-12-21 22:10:42 +00:00
setup:
var tagLib = coreTagLibrary()
teardown:
discard
2015-12-21 22:10:42 +00:00
test "Parsing: Simple Scalar":
ensure("Scalar", startDoc(), scalar("Scalar"), endDoc())
test "Parsing: Simple Sequence":
ensure("- off", startDoc(), startSequence(),
scalar("off", yTypeBoolFalse), endSequence(), endDoc())
test "Parsing: Simple Map":
ensure("42: value\nkey2: -7.5", startDoc(), startMap(),
scalar("42", yTypeInteger), scalar("value"), scalar("key2"),
scalar("-7.5", yTypeFloat), endMap(), endDoc())
2015-12-10 22:02:06 +00:00
test "Parsing: Explicit Map":
ensure("? null\n: value\n? ON\n: value2", startDoc(), startMap(),
scalar("null", yTypeNull), scalar("value"),
scalar("ON", yTypeBoolTrue), scalar("value2"),
2015-12-10 22:02:06 +00:00
endMap(), endDoc())
test "Parsing: Mixed Map (explicit to implicit)":
ensure("? a\n: 13\n1.5: d", startDoc(), startMap(), scalar("a"),
scalar("13", yTypeInteger), scalar("1.5", yTypeFloat),
scalar("d"), endMap(), endDoc())
test "Parsing: Mixed Map (implicit to explicit)":
ensure("a: 4.2\n? 23\n: d", startDoc(), startMap(), scalar("a"),
scalar("4.2", yTypeFloat), scalar("23", yTypeInteger),
scalar("d"), endMap(), endDoc())
2015-12-22 21:28:27 +00:00
test "Parsing: Missing values in map":
ensure("? a\n? b\nc:", startDoc(), startMap(), scalar("a"), scalar(""),
scalar("b"), scalar(""), scalar("c"), scalar(""), endMap(),
endDoc())
test "Parsing: Missing keys in map":
ensure(": a\n: b", startDoc(), startMap(), scalar(""), scalar("a"),
scalar(""), scalar("b"), endMap(), endDoc())
test "Parsing: Multiline scalars in explicit map":
ensure("? a\n true\n: null\n d\n? e\n 42", startDoc(), startMap(),
scalar("a true"), scalar("null d"), scalar("e 42"), scalar(""),
2015-12-22 21:28:27 +00:00
endMap(), endDoc())
test "Parsing: Map in Sequence":
2015-12-24 14:21:49 +00:00
ensure(" - key: value\n key2: value2\n -\n key3: value3",
startDoc(), startSequence(), startMap(), scalar("key"),
scalar("value"), scalar("key2"), scalar("value2"), endMap(),
startMap(), scalar("key3"), scalar("value3"), endMap(),
endSequence(), endDoc())
test "Parsing: Sequence in Map":
ensure("key:\n - item1\n - item2", startDoc(), startMap(),
scalar("key"), startSequence(), scalar("item1"), scalar("item2"),
endSequence(), endMap(), endDoc())
test "Parsing: Sequence in Sequence":
ensure("- - l1_i1\n - l1_i2\n- l2_i1", startDoc(), startSequence(),
startSequence(), scalar("l1_i1"), scalar("l1_i2"), endSequence(),
scalar("l2_i1"), endSequence(), endDoc())
test "Parsing: Flow Sequence":
ensure("[2, b]", startDoc(), startSequence(), scalar("2", yTypeInteger),
scalar("b"), endSequence(), endDoc())
test "Parsing: Flow Map":
ensure("{a: Y, 1.337: d}", startDoc(), startMap(), scalar("a"),
scalar("Y", yTypeBoolTrue), scalar("1.337", yTypeFloat),
scalar("d"), endMap(), endDoc())
test "Parsing: Flow Sequence in Flow Sequence":
ensure("[a, [b, c]]", startDoc(), startSequence(), scalar("a"),
startSequence(), scalar("b"), scalar("c"), endSequence(),
endSequence(), endDoc())
test "Parsing: Flow Sequence in Flow Map":
ensure("{a: [b, c], [d, e]: f}", startDoc(), startMap(), scalar("a"),
startSequence(), scalar("b"), scalar("c"), endSequence(),
startSequence(), scalar("d"), scalar("e"), endSequence(),
scalar("f"), endMap(), endDoc())
test "Parsing: Flow Sequence in Map":
ensure("a: [b, c]", startDoc(), startMap(), scalar("a"),
startSequence(), scalar("b"), scalar("c"), endSequence(),
endMap(), endDoc())
test "Parsing: Flow Map in Sequence":
ensure("- {a: b}", startDoc(), startSequence(), startMap(), scalar("a"),
scalar("b"), endMap(), endSequence(), endDoc())
test "Parsing: Multiline scalar (top level)":
ensure("a\nb \n c\nd", startDoc(), scalar("a b c d"), endDoc())
test "Parsing: Multiline scalar (in map)":
ensure("a: b\n c\nd:\n e\n f", startDoc(), startMap(), scalar("a"),
2015-12-17 20:44:41 +00:00
scalar("b c"), scalar("d"), scalar("e f"), endMap(), endDoc())
test "Parsing: Block scalar (literal)":
ensure("a: |\x0A ab\x0A \x0A cd\x0A ef\x0A \x0A", startDoc(),
startMap(), scalar("a"), scalar("ab\x0A\x0Acd\x0Aef\x0A"),
endMap(), endDoc())
test "Parsing: Block scalar (folded)":
ensure("a: >\x0A ab\x0A cd\x0A \x0Aef\x0A\x0A\x0Agh\x0A", startDoc(),
startMap(), scalar("a"), scalar("ab cd\x0Aef\x0Agh\x0A"),
endMap(), endDoc())
test "Parsing: Block scalar (keep)":
ensure("a: |+\x0A ab\x0A \x0A \x0A", startDoc(), startMap(),
scalar("a"), scalar("ab\x0A\x0A \x0A"), endMap(), endDoc())
test "Parsing: Block scalar (strip)":
ensure("a: |-\x0A ab\x0A \x0A \x0A", startDoc(), startMap(),
2015-12-21 20:58:28 +00:00
scalar("a"), scalar("ab"), endMap(), endDoc())
test "Parsing: non-specific tags of quoted strings":
ensure("\"a\"", startDoc(),
2015-12-27 22:40:27 +00:00
scalar("a", yTypeString, yTagExclamationMark), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: explicit non-specific tag":
2015-12-27 22:40:27 +00:00
ensure("! a", startDoc(), scalar("a", yTagExclamationMark), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: secondary tag handle resolution":
2015-12-27 22:40:27 +00:00
ensure("!!str a", startDoc(), scalar("a", yTagString), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: resolving custom tag handles":
let fooId = tagLib.registerUri("tag:example.com,2015:foo")
2015-12-21 22:10:42 +00:00
ensure("%TAG !t! tag:example.com,2015:\n---\n!t!foo a", startDoc(),
scalar("a", fooId), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: tags in sequence":
ensure(" - !!str a\n - b\n - !!int c\n - d", startDoc(),
2015-12-27 22:40:27 +00:00
startSequence(), scalar("a", yTagString), scalar("b"),
scalar("c", yTagInteger), scalar("d"), endSequence(), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: tags in implicit map":
ensure("!!str a: b\nc: !!int d\ne: !!str f\ng: h", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
scalar("a", yTagString), scalar("b"), scalar("c"),
scalar("d", yTagInteger), scalar("e"), scalar("f", yTagString),
scalar("g"), scalar("h"), endMap(), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: tags in explicit map":
ensure("? !!str a\n: !!int b\n? c\n: !!str d", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
scalar("a", yTagString), scalar("b", yTagInteger), scalar("c"),
scalar("d", yTagString), endMap(), endDoc())
test "Parsing: tags for block objects":
ensure("--- !!map\nfoo: !!seq\n - a\n - !!str b\n!!str bar: !!str baz",
2015-12-27 22:40:27 +00:00
startDoc(), startMap(yTagMap), scalar("foo"),
startSequence(yTagSequence), scalar("a"), scalar("b", yTagString),
endSequence(), scalar("bar", yTagString),
scalar("baz", yTagString), endMap(), endDoc())
test "Parsing: root tag for block sequence":
ensure("--- !!seq\n- a", startDoc(), startSequence(yTagSequence),
scalar("a"), endSequence(), endDoc())
test "Parsing: root tag for explicit block map":
ensure("--- !!map\n? a\n: b", startDoc(), startMap(yTagMap),
scalar("a"), scalar("b"), endMap(), endDoc())
2015-12-21 22:10:42 +00:00
test "Parsing: tags for flow objects":
2015-12-27 22:40:27 +00:00
ensure("!!map { k: !!seq [ a, !!str b] }", startDoc(), startMap(yTagMap),
scalar("k"), startSequence(yTagSequence), scalar("a"),
scalar("b", yTagString), endSequence(), endMap(), endDoc())
test "Parsing: Tag after directives end":
2015-12-27 22:40:27 +00:00
ensure("--- !!str\nfoo", startDoc(), scalar("foo", yTagString), endDoc())
test "Parsing: Simple Anchor":
2015-12-27 22:40:27 +00:00
ensure("&a str", startDoc(), scalar("str", yTagQuestionMark,
0.AnchorId), endDoc())
test "Parsing: Anchors in sequence":
ensure(" - &a a\n - b\n - &c c\n - &a d", startDoc(), startSequence(),
2015-12-27 22:40:27 +00:00
scalar("a", yTagQuestionMark, 0.AnchorId), scalar("b"),
scalar("c", yTagQuestionMark, 1.AnchorId),
scalar("d", yTagQuestionMark, 0.AnchorId), endSequence(),
endDoc())
test "Parsing: Anchors in map":
ensure("&a a: b\nc: &d d", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
scalar("a", yTagQuestionMark, 0.AnchorId),
scalar("b"), scalar("c"),
2015-12-27 22:40:27 +00:00
scalar("d", yTagQuestionMark, 1.AnchorId),
endMap(), endDoc())
test "Parsing: Anchors and tags":
ensure(" - &a !!str a\n - !!int b\n - &c !!int c\n - &d d", startDoc(),
2015-12-27 22:40:27 +00:00
startSequence(), scalar("a", yTagString, 0.AnchorId),
scalar("b", yTagInteger), scalar("c", yTagInteger, 1.AnchorId),
scalar("d", yTagQuestionMark, 2.AnchorId), endSequence(),
endDoc())
test "Parsing: Aliases in sequence":
ensure(" - &a a\n - &b b\n - *a\n - *b", startDoc(), startSequence(),
2015-12-27 22:40:27 +00:00
scalar("a", yTagQuestionMark, 0.AnchorId),
scalar("b", yTagQuestionMark, 1.AnchorId), alias(0.AnchorId),
alias(1.AnchorId), endSequence(), endDoc())
test "Parsing: Aliases in map":
ensure("&a a: &b b\n*a: *b", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
scalar("a", yTagQuestionMark, 0.AnchorId),
scalar("b", yTagQuestionMark, 1.AnchorId), alias(0.AnchorId),
alias(1.AnchorId), endMap(), endDoc())
test "Parsing: Aliases in flow":
ensure("{ &a [a, &b b]: *b, *a: [c, *b, d]}", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
startSequence(yTagQuestionMark, 0.AnchorId), scalar("a"),
scalar("b", yTagQuestionMark, 1.AnchorId), endSequence(),
alias(1.AnchorId), alias(0.AnchorId), startSequence(),
scalar("c"), alias(1.AnchorId), scalar("d"), endSequence(),
2015-12-22 22:35:03 +00:00
endMap(), endDoc())
test "Parsing: Tags on empty scalars":
2015-12-22 22:35:03 +00:00
ensure("!!str : a\nb: !!int\n!!str : !!str", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
scalar("", yTagString), scalar("a"), scalar("b"),
scalar("", yTagInteger), scalar("", yTagString),
scalar("", yTagString), endMap(), endDoc())
test "Parsing: Anchors on empty scalars":
2015-12-22 22:35:03 +00:00
ensure("&a : a\nb: &b\n&c : &a", startDoc(), startMap(),
2015-12-27 22:40:27 +00:00
scalar("", yTagQuestionMark, 0.AnchorId), scalar("a"),
scalar("b"), scalar("", yTagQuestionMark, 1.AnchorId),
scalar("", yTagQuestionMark, 2.AnchorId),
scalar("", yTagQuestionMark, 0.AnchorId), endMap(), endDoc())