Turn some test outputs into actual tests

This commit is contained in:
Oleh Prypin
2015-04-21 15:45:30 +03:00
parent c433ae1aaa
commit 22db40e5e4
13 changed files with 84 additions and 52 deletions

View File

@@ -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>!"

View File

@@ -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"
r.excl "def"
for w in r.items:
echo w
for w in r.itemsWithPrefix("de"):
echo w
assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
assert toSeq(r.itemsWithPrefix("de")) == @["definition"]

View File

@@ -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]

View File

@@ -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]

View File

@@ -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"

View File

@@ -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

View File

@@ -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>"""

View File

@@ -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"

View File

@@ -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)

View File

@@ -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"

View File

@@ -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"""

View File

@@ -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)

View File

@@ -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"