2015-12-07 21:09:57 +00:00
|
|
|
import "../src/yaml/sequential"
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import streams
|
|
|
|
|
|
|
|
proc startDoc(): YamlParserEvent =
|
|
|
|
new(result)
|
|
|
|
result.kind = yamlStartDocument
|
|
|
|
|
|
|
|
proc endDoc(): YamlParserEvent =
|
|
|
|
new(result)
|
|
|
|
result.kind = yamlEndDocument
|
|
|
|
|
2015-12-21 20:58:28 +00:00
|
|
|
proc scalar(content: string, tag: TagId = tagNonSpecificQmark,
|
2015-12-21 20:40:27 +00:00
|
|
|
anchor: string = nil): YamlParserEvent =
|
2015-12-07 21:09:57 +00:00
|
|
|
new(result)
|
|
|
|
result.kind = yamlScalar
|
|
|
|
result.scalarAnchor = anchor
|
|
|
|
result.scalarTag = tag
|
|
|
|
result.scalarContent = content
|
|
|
|
|
2015-12-21 22:10:42 +00:00
|
|
|
proc startSequence(tag: TagId = tagNonSpecificQmark, anchor: string = nil):
|
2015-12-21 20:58:28 +00:00
|
|
|
YamlParserEvent =
|
2015-12-07 21:09:57 +00:00
|
|
|
new(result)
|
|
|
|
result.kind = yamlStartSequence
|
|
|
|
result.objAnchor = anchor
|
|
|
|
result.objTag = tag
|
|
|
|
|
|
|
|
proc endSequence(): YamlParserEvent =
|
|
|
|
new(result)
|
|
|
|
result.kind = yamlEndSequence
|
|
|
|
|
2015-12-21 22:10:42 +00:00
|
|
|
proc startMap(tag: TagId = tagNonSpecificQmark, anchor: string = nil):
|
2015-12-21 20:58:28 +00:00
|
|
|
YamlParserEvent =
|
2015-12-07 21:09:57 +00:00
|
|
|
new(result)
|
|
|
|
result.kind = yamlStartMap
|
|
|
|
result.objAnchor = anchor
|
|
|
|
result.objTag = tag
|
|
|
|
|
|
|
|
proc endMap(): YamlParserEvent =
|
|
|
|
new(result)
|
|
|
|
result.kind = yamlEndMap
|
|
|
|
|
|
|
|
proc printDifference(expected, actual: YamlParserEvent) =
|
|
|
|
if expected.kind != actual.kind:
|
|
|
|
echo "expected " & $expected.kind & ", got " & $actual.kind
|
|
|
|
if actual.kind == yamlError:
|
2015-12-11 21:55:21 +00:00
|
|
|
echo "Error message: (", actual.line, ", ", actual.column, ") ",
|
|
|
|
actual.description
|
2015-12-07 21:09:57 +00:00
|
|
|
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
|
2015-12-07 21:09:57 +00:00
|
|
|
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 & "\" "
|
|
|
|
for i in 0..expected.scalarContent.high:
|
|
|
|
if i >= actual.scalarContent.high:
|
|
|
|
echo msg, "(expected more chars, first char missing: ",
|
|
|
|
cast[int](expected.scalarContent[i]), ")"
|
|
|
|
break
|
|
|
|
elif 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
|
2015-12-07 21:09:57 +00:00
|
|
|
else:
|
|
|
|
echo "[scalar] Unknown difference"
|
2015-12-21 20:40:27 +00:00
|
|
|
of yamlStartMap, yamlStartSequence:
|
|
|
|
if expected.objTag != actual.objTag:
|
2015-12-21 20:58:28 +00:00
|
|
|
echo "[object.tag] expected ", expected.objTag, ", got",
|
|
|
|
actual.objTag
|
2015-12-07 21:09:57 +00:00
|
|
|
else:
|
|
|
|
echo "Unknown difference in event kind " & $expected.kind
|
|
|
|
|
|
|
|
template ensure(input: string, expected: varargs[YamlParserEvent]) {.dirty.} =
|
2015-12-21 22:10:42 +00:00
|
|
|
var i = 0
|
2015-12-07 21:09:57 +00:00
|
|
|
|
2015-12-21 20:40:27 +00:00
|
|
|
for token in parser.events(newStringStream(input)):
|
2015-12-07 21:09:57 +00:00
|
|
|
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 parser = initParser()
|
|
|
|
|
2015-12-07 21:09:57 +00:00
|
|
|
test "Parsing: Simple Scalar":
|
|
|
|
ensure("Scalar", startDoc(), scalar("Scalar"), endDoc())
|
|
|
|
test "Parsing: Simple Sequence":
|
|
|
|
ensure("- item", startDoc(), startSequence(), scalar("item"),
|
|
|
|
endSequence(), endDoc())
|
|
|
|
test "Parsing: Simple Map":
|
2015-12-10 22:02:06 +00:00
|
|
|
ensure("key: value\nkey2: value2", startDoc(), startMap(),
|
|
|
|
scalar("key"), scalar("value"), scalar("key2"), scalar("value2"),
|
|
|
|
endMap(), endDoc())
|
|
|
|
test "Parsing: Explicit Map":
|
|
|
|
ensure("? key\n: value\n? key2\n: value2", startDoc(), startMap(),
|
|
|
|
scalar("key"), scalar("value"), scalar("key2"), scalar("value2"),
|
|
|
|
endMap(), endDoc())
|
2015-12-10 19:53:43 +00:00
|
|
|
test "Parsing: Map in Sequence":
|
|
|
|
ensure(" - key: value", startDoc(), startSequence(), startMap(),
|
|
|
|
scalar("key"), scalar("value"), 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(),
|
2015-12-10 21:28:57 +00:00
|
|
|
scalar("l2_i1"), endSequence(), endDoc())
|
|
|
|
test "Parsing: Flow Sequence":
|
|
|
|
ensure("[a, b]", startDoc(), startSequence(), scalar("a"), scalar("b"),
|
|
|
|
endSequence(), endDoc())
|
|
|
|
test "Parsing: Flow Map":
|
|
|
|
ensure("{a: b, c: d}", startDoc(), startMap(), scalar("a"), scalar("b"),
|
|
|
|
scalar("c"), 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]}", startDoc(), startMap(), scalar("a"),
|
|
|
|
startSequence(), scalar("b"), scalar("c"), endSequence(),
|
|
|
|
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"),
|
2015-12-14 20:26:34 +00:00
|
|
|
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":
|
2015-12-21 22:10:42 +00:00
|
|
|
ensure("\"a\"", startDoc(), scalar("a", tagNonSpecificEmark), endDoc())
|
|
|
|
test "Parsing: explicit non-specific tag":
|
|
|
|
ensure("! a", startDoc(), scalar("a", tagNonSpecificEmark), endDoc())
|
|
|
|
test "Parsing: secondary tag handle resolution":
|
|
|
|
let id = parser.registerUri("tag:yaml.org,2002:str")
|
|
|
|
ensure("!!str a", startDoc(), scalar("a", id), endDoc())
|
|
|
|
test "Parsing: resolving custom tag handles":
|
|
|
|
let id = parser.registerUri("tag:example.com,2015:foo")
|
|
|
|
ensure("%TAG !t! tag:example.com,2015:\n---\n!t!foo a", startDoc(),
|
|
|
|
scalar("a", id), endDoc())
|
|
|
|
test "Parsing: tags in sequence":
|
|
|
|
let
|
|
|
|
idStr = parser.registerUri("tag:yaml.org,2002:str")
|
|
|
|
idInt = parser.registerUri("tag:yaml.org,2002:int")
|
|
|
|
ensure(" - !!str a\n - b\n - !!int c\n - d", startDoc(),
|
|
|
|
startSequence(), scalar("a", idStr), scalar("b"),
|
|
|
|
scalar("c", idInt), scalar("d"), endSequence(), endDoc())
|
|
|
|
test "Parsing: tags in implicit map":
|
|
|
|
let
|
|
|
|
idStr = parser.registerUri("tag:yaml.org,2002:str")
|
|
|
|
idInt = parser.registerUri("tag:yaml.org,2002:int")
|
|
|
|
ensure("!!str a: b\nc: !!int d\ne: !!str f\ng: h", startDoc(), startMap(),
|
|
|
|
scalar("a", idStr), scalar("b"), scalar("c"), scalar("d", idInt),
|
|
|
|
scalar("e"), scalar("f", idStr), scalar("g"), scalar("h"),
|
|
|
|
endMap(), endDoc())
|
|
|
|
test "Parsing: tags in explicit map":
|
|
|
|
let
|
|
|
|
idStr = parser.registerUri("tag:yaml.org,2002:str")
|
|
|
|
idInt = parser.registerUri("tag:yaml.org,2002:int")
|
|
|
|
ensure("? !!str a\n: !!int b\n? c\n: !!str d", startDoc(), startMap(),
|
|
|
|
scalar("a", idStr), scalar("b", idInt), scalar("c"),
|
|
|
|
scalar("d", idStr), endMap(), endDoc())
|
|
|
|
test "Parsing: tags for flow objects":
|
|
|
|
let
|
|
|
|
idStr = parser.registerUri("tag:yaml.org,2002:str")
|
|
|
|
idMap = parser.registerUri("tag:yaml.org,2002:map")
|
|
|
|
idSeq = parser.registerUri("tag:yaml.org,2002:seq")
|
|
|
|
ensure("!!map { k: !!seq [ a, !!str b] }", startDoc(), startMap(idMap),
|
|
|
|
scalar("k"), startSequence(idSeq), scalar("a"),
|
|
|
|
scalar("b", idStr), endSequence(), endMap(), endDoc())
|