From ebe4201cbc096e38c5e919401b18bb87cbdab8a9 Mon Sep 17 00:00:00 2001 From: Vindaar Date: Tue, 9 Oct 2018 16:57:21 +0200 Subject: [PATCH] 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. --- test/tserialization.nim | 47 ++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/test/tserialization.nim b/test/tserialization.nim index 5aef4ac..ddbf57e 100644 --- a/test/tserialization.nim +++ b/test/tserialization.nim @@ -5,7 +5,7 @@ # distribution, for details about the copyright. import "../yaml" -import unittest, strutils, tables, times, math +import unittest, strutils, tables, times, math, os type MyTuple = tuple @@ -96,13 +96,31 @@ proc constructObject*(s: var YamlStream, c: ConstructionContext, constructScalarItem(s, item, BetterInt): result = BetterInt(parseBiggestInt(item.scalarContent) + 1) -template assertStringEqual(expected, actual: string) = - for i in countup(0, min(expected.len, actual.len) - 1): - if expected[i] != actual[i]: - echo "string mismatch at character #", i, "(expected:\'", - expected[i], "\', was \'", actual[i], "\'):" - echo "expected:\n", expected, "\nactual:\n", actual - assert(false) +template assertStringEqual(expected, actual: string, file = stdout) = + 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]: + file.write "string mismatch at character #", i, "(expected:\'", + expected[i], "\', was \'", actual[i], "\'):\n" + file.write "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: + 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) = try: @@ -124,6 +142,19 @@ proc newNode(v: string): ref Node = let blockOnly = defineOptions(style=psBlockOnly) 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": var input = "-4247" var result: int