replace `assertStringEqual` by version checking for content and len

Now check for content *and* length of the strings in
`tserialization`. Also adds a test case for the case with which the
old version would have failed (one string longer than the other, thus
being different).

This version tries to keep the error reporting mechanism the same as
the old version.
This commit is contained in:
Vindaar 2018-10-09 16:57:21 +02:00 committed by flyx
parent 53c476ce38
commit ebe4201cbc
1 changed files with 39 additions and 8 deletions

View File

@ -5,7 +5,7 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
import "../yaml" import "../yaml"
import unittest, strutils, tables, times, math import unittest, strutils, tables, times, math, os
type type
MyTuple = tuple MyTuple = tuple
@ -96,13 +96,31 @@ proc constructObject*(s: var YamlStream, c: ConstructionContext,
constructScalarItem(s, item, BetterInt): constructScalarItem(s, item, BetterInt):
result = BetterInt(parseBiggestInt(item.scalarContent) + 1) result = BetterInt(parseBiggestInt(item.scalarContent) + 1)
template assertStringEqual(expected, actual: string) = template assertStringEqual(expected, actual: string, file = stdout) =
for i in countup(0, min(expected.len, actual.len) - 1): 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:
file.write "Expected and actual string's length differs.\n"
file.write "Expected length: ", expected.len, "\n"
file.write "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]: if expected[i] != actual[i]:
echo "string mismatch at character #", i, "(expected:\'", file.write "string mismatch at character #", i, "(expected:\'",
expected[i], "\', was \'", actual[i], "\'):" expected[i], "\', was \'", actual[i], "\'):\n"
echo "expected:\n", expected, "\nactual:\n", actual file.write "expected:\n", expected, "\nactual:\n", actual, "\n"
assert(false) 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:
file.write "Expected continues with: '", expected[minInd .. ^1], "'"
assert false
else:
file.write "Actual continues with: '", actual[minInd .. ^1], "'"
assert false
template expectConstructionError(li, co: int, message: string, body: typed) = template expectConstructionError(li, co: int, message: string, body: typed) =
try: try:
@ -124,6 +142,19 @@ proc newNode(v: string): ref Node =
let blockOnly = defineOptions(style=psBlockOnly) let blockOnly = defineOptions(style=psBlockOnly)
suite "Serialization": suite "Serialization":
test "Internal string assert":
let s1 = "foo"
let s2 = "foobar"
let fname = "dump.txt"
let f: File = open(fname, fmWrite)
try:
assertStringEqual(s1, s2, file=f)
except AssertionError:
discard
finally:
f.close()
removeFile(fname)
test "Load integer without fixed length": test "Load integer without fixed length":
var input = "-4247" var input = "-4247"
var result: int var result: int