2016-02-27 12:09:50 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2023-08-29 21:23:15 +00:00
|
|
|
# (c) Copyright 2015-2023 Felix Krause
|
2016-02-27 12:09:50 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
import ../yaml, ../yaml/data
|
2016-02-22 20:56:30 +00:00
|
|
|
|
2016-03-25 16:28:58 +00:00
|
|
|
proc escapeNewlines(s: string): string =
|
|
|
|
result = ""
|
|
|
|
for c in s:
|
|
|
|
case c
|
|
|
|
of '\l': result.add("\\n")
|
|
|
|
of '\t': result.add("\\t")
|
|
|
|
of '\\': result.add("\\\\")
|
|
|
|
else: result.add(c)
|
|
|
|
|
2020-11-04 21:47:52 +00:00
|
|
|
proc printDifference(entity: string, expected, actual: Properties): bool =
|
2020-11-04 15:40:37 +00:00
|
|
|
result = false
|
|
|
|
if expected.tag != actual.tag:
|
2020-11-04 21:47:52 +00:00
|
|
|
echo "[", entity, ".tag] expected ", $expected.tag, ", got ", $actual.tag
|
2020-11-04 15:40:37 +00:00
|
|
|
result = true
|
|
|
|
if expected.anchor != actual.anchor:
|
2020-11-04 21:47:52 +00:00
|
|
|
echo "[", entity, ".anchor] expected ", $expected.anchor, ", got ", $actual.anchor
|
2020-11-04 15:40:37 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
proc printDifference*(expected, actual: Event) =
|
2016-04-02 15:48:22 +00:00
|
|
|
if expected.kind != actual.kind:
|
2020-11-04 21:47:52 +00:00
|
|
|
echo "expected ", expected.kind, ", got ", $actual.kind
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
|
|
|
case expected.kind
|
|
|
|
of yamlScalar:
|
2020-11-04 21:47:52 +00:00
|
|
|
if not printDifference("scalar", expected.scalarProperties, actual.scalarProperties):
|
2020-11-04 15:40:37 +00:00
|
|
|
if expected.scalarContent != actual.scalarContent:
|
|
|
|
let msg = "[scalarEvent] content mismatch!\nexpected: " &
|
|
|
|
escapeNewlines(expected.scalarContent) &
|
|
|
|
"\ngot : " & escapeNewlines(actual.scalarContent)
|
|
|
|
if expected.scalarContent.len != actual.scalarContent.len:
|
|
|
|
echo msg, "\n(length does not match)"
|
|
|
|
else:
|
|
|
|
for i in 0..expected.scalarContent.high:
|
|
|
|
if expected.scalarContent[i] != actual.scalarContent[i]:
|
|
|
|
echo msg, "\n(first different char at pos ", i, ": expected ",
|
|
|
|
cast[int](expected.scalarContent[i]), ", got ",
|
|
|
|
cast[int](actual.scalarContent[i]), ")"
|
|
|
|
break
|
|
|
|
else: echo "[scalar] Unknown difference"
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlStartMap:
|
2020-11-04 21:47:52 +00:00
|
|
|
if not printDifference("map", expected.mapProperties, actual.mapProperties):
|
2020-11-04 15:40:37 +00:00
|
|
|
echo "[map] Unknown difference"
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlStartSeq:
|
2020-11-04 21:47:52 +00:00
|
|
|
if not printDifference("seq", expected.seqProperties, actual.seqProperties):
|
2020-11-04 15:40:37 +00:00
|
|
|
echo "[seq] Unknown difference"
|
2016-04-02 15:48:22 +00:00
|
|
|
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
|
2016-02-22 20:56:30 +00:00
|
|
|
|
|
|
|
template ensure*(input: var YamlStream,
|
2020-11-04 15:40:37 +00:00
|
|
|
expected: varargs[Event]) {.dirty.} =
|
2016-04-02 15:48:22 +00:00
|
|
|
var i = 0
|
|
|
|
for token in input:
|
|
|
|
if i >= expected.len:
|
|
|
|
echo "received more tokens than expected (next token = ", token.kind, ")"
|
|
|
|
fail()
|
|
|
|
break
|
|
|
|
if token != expected[i]:
|
2020-11-04 21:47:52 +00:00
|
|
|
echo "at event #" & $i & ":"
|
2016-04-02 15:48:22 +00:00
|
|
|
printDifference(expected[i], token)
|
|
|
|
fail()
|
|
|
|
break
|
2023-07-31 17:16:24 +00:00
|
|
|
i.inc()
|
|
|
|
|
|
|
|
template assertStringEqual*(expected, actual: string) =
|
|
|
|
if expected != actual:
|
|
|
|
# if they are unequal, walk through the strings and check each
|
|
|
|
# character for a better error message
|
|
|
|
if expected.len != actual.len:
|
|
|
|
echo "Expected and actual string's length differs.\n"
|
|
|
|
echo "Expected length: ", expected.len, "\n"
|
|
|
|
echo "Actual length: ", actual.len, "\n"
|
|
|
|
# check length up to smaller of the two strings
|
|
|
|
for i in countup(0, min(expected.high, actual.high)):
|
|
|
|
if expected[i] != actual[i]:
|
|
|
|
echo "string mismatch at character #", i, "(expected:\'",
|
|
|
|
expected[i], "\', was \'", actual[i], "\'):\n"
|
|
|
|
echo "expected:\n", expected, "\nactual:\n", actual, "\n"
|
|
|
|
assert(false)
|
|
|
|
# if we haven't raised an assertion error here, the problem is that
|
|
|
|
# one string is longer than the other
|
|
|
|
let minInd = min(expected.len, actual.len) # len instead of high to continue
|
|
|
|
# after shorter string
|
|
|
|
if expected.high > actual.high:
|
|
|
|
echo "Expected continues with: '", expected[minInd .. ^1], "'"
|
|
|
|
assert false
|
|
|
|
else:
|
|
|
|
echo "Actual continues with: '", actual[minInd .. ^1], "'"
|
|
|
|
assert false
|