diff options
author | Oleh Prypin <blaxpirit@gmail.com> | 2015-04-21 15:45:30 +0300 |
---|---|---|
committer | Oleh Prypin <blaxpirit@gmail.com> | 2015-04-21 15:59:32 +0300 |
commit | 22db40e5e4c1bfb5f2ea3b6864873b2edff30764 (patch) | |
tree | e36e74d0ae83467e0879883e277e249d826993a0 | |
parent | c433ae1aaaf9c997281d244bce061470cd55568e (diff) | |
download | Nim-22db40e5e4c1bfb5f2ea3b6864873b2edff30764.tar.gz |
Turn some test outputs into actual tests
-rw-r--r-- | lib/packages/docutils/rstgen.nim | 7 | ||||
-rw-r--r-- | lib/pure/collections/critbits.nim | 12 | ||||
-rw-r--r-- | lib/pure/collections/intsets.nim | 15 | ||||
-rw-r--r-- | lib/pure/collections/sequtils.nim | 6 | ||||
-rw-r--r-- | lib/pure/cookies.nim | 12 | ||||
-rw-r--r-- | lib/pure/gentabs.nim | 22 | ||||
-rw-r--r-- | lib/pure/htmlgen.nim | 11 | ||||
-rw-r--r-- | lib/pure/mimetypes.nim | 6 | ||||
-rw-r--r-- | lib/pure/parseutils.nim | 8 | ||||
-rw-r--r-- | lib/pure/strutils.nim | 10 | ||||
-rw-r--r-- | lib/pure/subexes.nim | 20 | ||||
-rw-r--r-- | lib/pure/times.nim | 4 | ||||
-rw-r--r-- | lib/pure/unidecode/unidecode.nim | 3 |
13 files changed, 84 insertions, 52 deletions
diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index 551c911fc..9e96d8a63 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -1250,6 +1250,7 @@ proc rstToHtml*(s: string, options: TRstParseOptions, renderRstToOut(d, rst, result) -when not defined(testing) and isMainModule: - echo rstToHtml("*Hello* **world**!", {}, - newStringTable(modeStyleInsensitive)) +when isMainModule: + assert rstToHtml("*Hello* **world**!", {}, + newStringTable(modeStyleInsensitive)) == + "<em>Hello</em> <strong>world</strong>!" diff --git a/lib/pure/collections/critbits.nim b/lib/pure/collections/critbits.nim index 957da8a72..7e3f23851 100644 --- a/lib/pure/collections/critbits.nim +++ b/lib/pure/collections/critbits.nim @@ -286,6 +286,8 @@ proc `$`*[T](c: CritBitTree[T]): string = result.add("}") when isMainModule: + import sequtils + var r: CritBitTree[void] r.incl "abc" r.incl "xyz" @@ -295,12 +297,8 @@ when isMainModule: doAssert r.contains"def" - when not defined(testing): - #r.del "def" - - for w in r.items: - echo w + r.excl "def" - for w in r.itemsWithPrefix("de"): - echo w + assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"] + assert toSeq(r.itemsWithPrefix("de")) == @["definition"] diff --git a/lib/pure/collections/intsets.nim b/lib/pure/collections/intsets.nim index 336daf265..25f6616a6 100644 --- a/lib/pure/collections/intsets.nim +++ b/lib/pure/collections/intsets.nim @@ -197,15 +197,22 @@ proc empty*(s: IntSet): bool {.inline, deprecated.} = ## worked reliably and so is deprecated. result = s.counter == 0 -when not defined(testing) and isMainModule: +when isMainModule: + import sequtils, algorithm + var x = initIntSet() x.incl(1) x.incl(2) x.incl(7) x.incl(1056) - for e in items(x): echo e - var y: TIntSet + var xs = toSeq(items(x)) + xs.sort(cmp[int]) + assert xs == @[1, 2, 7, 1056] + + var y: IntSet assign(y, x) - for e in items(y): echo e + var ys = toSeq(items(y)) + ys.sort(cmp[int]) + assert ys == @[1, 2, 7, 1056] diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 24b47eee6..e9cd2cb3c 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -492,10 +492,8 @@ when isMainModule: block: # filter iterator test let numbers = @[1, 4, 5, 8, 9, 7, 4] - for n in filter(numbers, proc (x: int): bool = x mod 2 == 0): - when not defined(testing): - echo($n) - # echoes 4, 8, 4 in separate lines + assert toSeq(filter(numbers, proc (x: int): bool = x mod 2 == 0)) == + @[4, 8, 4] block: # keepIf test var floats = @[13.0, 12.5, 5.8, 2.0, 6.1, 9.9, 10.1] diff --git a/lib/pure/cookies.nim b/lib/pure/cookies.nim index d25f7537e..9983c4a04 100644 --- a/lib/pure/cookies.nim +++ b/lib/pure/cookies.nim @@ -53,9 +53,15 @@ proc setCookie*(key, value: string, expires: TimeInfo, format(expires, "ddd',' dd MMM yyyy HH:mm:ss 'UTC'"), noname, secure, httpOnly) -when not defined(testing) and isMainModule: +when isMainModule: var tim = Time(int(getTime()) + 76 * (60 * 60 * 24)) - echo(setCookie("test", "value", tim.getGMTime())) + let cookie = setCookie("test", "value", tim.getGMTime()) + when not defined(testing): + echo cookie + let start = "Set-Cookie: test=value; Expires=" + assert cookie[0..start.high] == start - echo parseCookies("uid=1; kp=2") + let table = parseCookies("uid=1; kp=2") + assert table["uid"] == "1" + assert table["kp"] == "2" diff --git a/lib/pure/gentabs.nim b/lib/pure/gentabs.nim index 8ea93a5d2..8c89a0ac3 100644 --- a/lib/pure/gentabs.nim +++ b/lib/pure/gentabs.nim @@ -186,9 +186,19 @@ when isMainModule: assert( z["first"]["one"] == 1) # retrieve from first inner table assert( z["second"]["red"] == 10) # retrieve from second inner table - when not defined(testing) : - for k,v in pairs(z): - echo( "$# ($#) ->" % [k,$len(v)] ) - #for k2,v2 in pairs(v): - # echo( " $# <-> $#" % [k2,$v2] ) - echo() + var output = "" + for k, v in pairs(z): + output.add( "$# ($#) ->\n" % [k,$len(v)] ) + for k2,v2 in pairs(v): + output.add( " $# <-> $#\n" % [k2,$v2] ) + + let expected = unindent """ + first (3) -> + two <-> 2 + three <-> 3 + one <-> 1 + second (2) -> + red <-> 10 + blue <-> 20 + """ + assert output == expected diff --git a/lib/pure/htmlgen.nim b/lib/pure/htmlgen.nim index bffb33b80..e6c15371e 100644 --- a/lib/pure/htmlgen.nim +++ b/lib/pure/htmlgen.nim @@ -482,8 +482,9 @@ macro `var`*(e: expr): expr {.immediate.} = let e = callsite() result = xmlCheckedTag(e, "var", commonAttr) -when not defined(testing) and isMainModule: - var nim = "Nim" - echo h1(a(href="http://nim-lang.org", nim)) - echo form(action="test", `accept-charset` = "Content-Type") - +when isMainModule: + let nim = "Nim" + assert h1(a(href="http://nim-lang.org", nim)) == + """<h1><a href="http://nim-lang.org">Nim</a></h1>""" + assert form(action="test", `accept-charset` = "Content-Type") == + """<form action="test" accept-charset="Content-Type"></form>""" diff --git a/lib/pure/mimetypes.nim b/lib/pure/mimetypes.nim index 723d6e56d..642419e64 100644 --- a/lib/pure/mimetypes.nim +++ b/lib/pure/mimetypes.nim @@ -516,7 +516,7 @@ proc register*(mimedb: var MimeDB, ext: string, mimetype: string) = ## Adds ``mimetype`` to the ``mimedb``. mimedb.mimes[ext] = mimetype -when not defined(testing) and isMainModule: +when isMainModule: var m = newMimetypes() - echo m.getMimetype("mp4") - echo m.getExt("text/html") + assert m.getMimetype("mp4") == "video/mp4" + assert m.getExt("text/html") == "html" diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim index d65f9ceae..c07b713de 100644 --- a/lib/pure/parseutils.nim +++ b/lib/pure/parseutils.nim @@ -323,9 +323,11 @@ iterator interpolatedFragments*(s: string): tuple[kind: InterpolatedKind, i = j when isMainModule: - for k, v in interpolatedFragments("$test{} $this is ${an{ example}} "): - when not defined(testing): - echo "(", k, ", \"", v, "\")" + import sequtils + let input = "$test{} $this is ${an{ example}} " + let expected = @[(ikVar, "test"), (ikStr, "{} "), (ikVar, "this"), + (ikStr, " is "), (ikExpr, "an{ example}"), (ikStr, " ")] + assert toSeq(interpolatedFragments(input)) == expected var value = 0 discard parseHex("0x38", value) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 8dc4fa85e..1b248126b 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1402,9 +1402,13 @@ when isMainModule: doAssert align("a", 0) == "a" doAssert align("1232", 6) == " 1232" doAssert align("1232", 6, '#') == "##1232" - when not defined(testing): - echo wordWrap(""" this is a long text -- muchlongerthan10chars and here - it goes""", 10, false) + + let + inp = """ this is a long text -- muchlongerthan10chars and here + it goes""" + outp = " this is a\nlong text\n--\nmuchlongerthan10chars\nand here\nit goes" + doAssert wordWrap(inp, 10, false) == outp + doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001" doAssert formatBiggestFloat(0.00000000001, ffScientific, 1) == "1.0e-11" diff --git a/lib/pure/subexes.nim b/lib/pure/subexes.nim index 643694806..d213c99e6 100644 --- a/lib/pure/subexes.nim +++ b/lib/pure/subexes.nim @@ -386,9 +386,13 @@ when isMainModule: longishA, longish)""" - when not defined(testing): - echo "type TMyEnum* = enum\n $', '2i'\n '{..}" % ["fieldA", - "fieldB", "FiledClkad", "fieldD", "fieldE", "longishFieldName"] + assert "type TMyEnum* = enum\n $', '2i'\n '{..}" % ["fieldA", + "fieldB", "FiledClkad", "fieldD", "fieldE", "longishFieldName"] == + strutils.unindent """ + type TMyEnum* = enum + fieldA, fieldB, + FiledClkad, fieldD, + fieldE, longishFieldName""" doAssert subex"$1($', '{2..})" % ["f", "a", "b", "c"] == "f(a, b, c)" @@ -396,8 +400,12 @@ when isMainModule: doAssert subex"$['''|'|''''|']']#" % "0" == "'|" - when not defined(testing): - echo subex("type\n TEnum = enum\n $', '40c'\n '{..}") % [ - "fieldNameA", "fieldNameB", "fieldNameC", "fieldNameD"] + assert subex("type\n TEnum = enum\n $', '40c'\n '{..}") % [ + "fieldNameA", "fieldNameB", "fieldNameC", "fieldNameD"] == + strutils.unindent """ + type + TEnum = enum + fieldNameA, fieldNameB, fieldNameC, + fieldNameD""" diff --git a/lib/pure/times.nim b/lib/pure/times.nim index c813d71d4..c275ede69 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -1036,9 +1036,6 @@ when isMainModule: # Tue 19 Jan 03:14:07 GMT 2038 var t = getGMTime(fromSeconds(2147483647)) - when not defined(testing): - echo t.format("ddd dd MMM hh:mm:ss ZZZ yyyy") - echo t.format("ddd ddMMMhhmmssZZZyyyy") assert t.format("ddd dd MMM hh:mm:ss ZZZ yyyy") == "Tue 19 Jan 03:14:07 UTC 2038" assert t.format("ddd ddMMMhh:mm:ssZZZyyyy") == "Tue 19Jan03:14:07UTC2038" @@ -1113,5 +1110,6 @@ when isMainModule: # Kitchen = "3:04PM" s = "3:04PM" f = "h:mmtt" + assert "15:04:00" in $s.parse(f) when not defined(testing): echo "Kitchen: " & $s.parse(f) diff --git a/lib/pure/unidecode/unidecode.nim b/lib/pure/unidecode/unidecode.nim index d591ac7de..a83b9be0f 100644 --- a/lib/pure/unidecode/unidecode.nim +++ b/lib/pure/unidecode/unidecode.nim @@ -70,6 +70,5 @@ proc unidecode*(s: string): string = when isMainModule: loadUnidecodeTable("lib/pure/unidecode/unidecode.dat") - when not defined(testing): - echo unidecode("Äußerst") + assert unidecode("Äußerst") == "Ausserst" |