diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim
index 4be692f39..8536ab6f2 100644
--- a/lib/impure/db_sqlite.nim
+++ b/lib/impure/db_sqlite.nim
@@ -205,7 +205,7 @@ proc setEncoding*(connection: TDbConn, encoding: string): bool {.
exec(connection, sql"PRAGMA encoding = ?", [encoding])
result = connection.getValue(sql"PRAGMA encoding") == encoding
-when isMainModule:
+when not defined(testing) and isMainModule:
var db = open("db.sql", "", "", "")
exec(db, sql"create table tbl1(one varchar(10), two smallint)", [])
exec(db, sql"insert into tbl1 values('hello!',10)", [])
diff --git a/lib/impure/graphics.nim b/lib/impure/graphics.nim
index dfadb46ee..814c0ebe1 100644
--- a/lib/impure/graphics.nim
+++ b/lib/impure/graphics.nim
@@ -499,7 +499,7 @@ template withEvents*(surf: PSurface, event: expr, actions: stmt): stmt {.
if sdl.init(sdl.INIT_VIDEO) < 0: raiseEGraphics()
if sdl_ttf.init() < 0: raiseEGraphics()
-when isMainModule:
+when not defined(testing) and isMainModule:
var surf = newScreenSurface(800, 600)
surf.fillSurface(colWhite)
diff --git a/lib/impure/ssl.nim b/lib/impure/ssl.nim
index bb7cfc0d3..d318a1979 100644
--- a/lib/impure/ssl.nim
+++ b/lib/impure/ssl.nim
@@ -82,7 +82,7 @@ proc close*(sock: TSecureSocket) =
ERR_print_errors_fp(stderr)
raiseOSError(osLastError())
-when isMainModule:
+when not defined(testing) and isMainModule:
var s: TSecureSocket
echo connect(s, "smtp.gmail.com", 465)
diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim
index a44740bcf..9e96d8a63 100644
--- a/lib/packages/docutils/rstgen.nim
+++ b/lib/packages/docutils/rstgen.nim
@@ -1251,5 +1251,6 @@ proc rstToHtml*(s: string, options: TRstParseOptions,
when isMainModule:
- echo rstToHtml("*Hello* **world**!", {},
- newStringTable(modeStyleInsensitive))
+ assert rstToHtml("*Hello* **world**!", {},
+ newStringTable(modeStyleInsensitive)) ==
+ "Hello world!"
diff --git a/lib/pure/actors.nim b/lib/pure/actors.nim
index 8c61ce7df..294c24741 100644
--- a/lib/pure/actors.nim
+++ b/lib/pure/actors.nim
@@ -221,7 +221,7 @@ proc spawn*[TIn](p: var TActorPool[TIn, void], input: TIn,
setupTask()
schedule()
-when isMainModule:
+when not defined(testing) and isMainModule:
var
a: TActorPool[int, void]
createActorPool(a)
diff --git a/lib/pure/asyncftpclient.nim b/lib/pure/asyncftpclient.nim
index 96f54b49e..daf69d59f 100644
--- a/lib/pure/asyncftpclient.nim
+++ b/lib/pure/asyncftpclient.nim
@@ -300,7 +300,7 @@ proc newAsyncFtpClient*(address: string, port = Port(21),
result.dsockConnected = false
result.csock = newAsyncSocket()
-when isMainModule:
+when not defined(testing) and isMainModule:
var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test")
proc main(ftp: AsyncFtpClient) {.async.} =
await ftp.connect()
diff --git a/lib/pure/asynchttpserver.nim b/lib/pure/asynchttpserver.nim
index 64242234c..dc5a55dcc 100644
--- a/lib/pure/asynchttpserver.nim
+++ b/lib/pure/asynchttpserver.nim
@@ -260,7 +260,7 @@ proc close*(server: AsyncHttpServer) =
## Terminates the async http server instance.
server.socket.close()
-when isMainModule:
+when not defined(testing) and isMainModule:
proc main =
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
diff --git a/lib/pure/asyncio.nim b/lib/pure/asyncio.nim
index f58bb4302..6ae2c608b 100644
--- a/lib/pure/asyncio.nim
+++ b/lib/pure/asyncio.nim
@@ -660,7 +660,7 @@ proc len*(disp: Dispatcher): int =
## Retrieves the amount of delegates in ``disp``.
return disp.delegates.len
-when isMainModule:
+when not defined(testing) and isMainModule:
proc testConnect(s: AsyncSocket, no: int) =
echo("Connected! " & $no)
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim
index e7325e0d7..39d05d36b 100644
--- a/lib/pure/asyncnet.nim
+++ b/lib/pure/asyncnet.nim
@@ -458,7 +458,7 @@ proc isClosed*(socket: AsyncSocket): bool =
## Determines whether the socket has been closed.
return socket.closed
-when isMainModule:
+when not defined(testing) and isMainModule:
type
TestCases = enum
HighClient, LowClient, LowServer
diff --git a/lib/pure/collections/LockFreeHash.nim b/lib/pure/collections/LockFreeHash.nim
index c3954468a..0df97c685 100644
--- a/lib/pure/collections/LockFreeHash.nim
+++ b/lib/pure/collections/LockFreeHash.nim
@@ -525,7 +525,7 @@ proc get*[K,V](table: var PConcTable[K,V], key: var K): V =
#Tests ----------------------------
-when isMainModule:
+when not defined(testing) and isMainModule:
import locks, times, mersenne
const
diff --git a/lib/pure/collections/critbits.nim b/lib/pure/collections/critbits.nim
index 3d10e39aa..7e3f23851 100644
--- a/lib/pure/collections/critbits.nim
+++ b/lib/pure/collections/critbits.nim
@@ -286,18 +286,19 @@ proc `$`*[T](c: CritBitTree[T]): string =
result.add("}")
when isMainModule:
+ import sequtils
+
var r: CritBitTree[void]
r.incl "abc"
r.incl "xyz"
r.incl "def"
r.incl "definition"
r.incl "prefix"
+
doAssert r.contains"def"
- #r.del "def"
- for w in r.items:
- echo w
-
- for w in r.itemsWithPrefix("de"):
- echo w
+ r.excl "def"
+ 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 ae26c5af6..25f6616a6 100644
--- a/lib/pure/collections/intsets.nim
+++ b/lib/pure/collections/intsets.nim
@@ -198,14 +198,21 @@ proc empty*(s: IntSet): bool {.inline, deprecated.} =
result = s.counter == 0
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 3f37d1ef0..e9cd2cb3c 100644
--- a/lib/pure/collections/sequtils.nim
+++ b/lib/pure/collections/sequtils.nim
@@ -492,9 +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):
- 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]
@@ -616,4 +615,5 @@ when isMainModule:
#doAssert a.repeat(-1) == @[] # will not compile!
doAssert b.repeat(3) == @[]
- echo "Finished doc tests"
+ when not defined(testing):
+ echo "Finished doc tests"
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim
index bd6c2dc20..280e0eeba 100644
--- a/lib/pure/collections/sets.nim
+++ b/lib/pure/collections/sets.nim
@@ -970,6 +970,7 @@ when isMainModule and not defined(release):
if s <= i or mustRehash(s, i):
echo "performance issue: rightSize() will not elide enlarge() at ", i
- echo "Micro tests run successfully."
+ when not defined(testing):
+ echo "Micro tests run successfully."
testModule()
diff --git a/lib/pure/concurrency/cpuload.nim b/lib/pure/concurrency/cpuload.nim
index c1796089a..7ce5e01b7 100644
--- a/lib/pure/concurrency/cpuload.nim
+++ b/lib/pure/concurrency/cpuload.nim
@@ -78,7 +78,7 @@ proc advice*(s: var ThreadPoolState): ThreadPoolAdvice =
result = doNothing
inc s.calls
-when isMainModule:
+when not defined(testing) and isMainModule:
proc busyLoop() =
while true:
discard random(80)
diff --git a/lib/pure/cookies.nim b/lib/pure/cookies.nim
index 6247efed2..9983c4a04 100644
--- a/lib/pure/cookies.nim
+++ b/lib/pure/cookies.nim
@@ -56,6 +56,12 @@ proc setCookie*(key, value: string, expires: TimeInfo,
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/encodings.nim b/lib/pure/encodings.nim
index 25c7ad9ef..2a6134615 100644
--- a/lib/pure/encodings.nim
+++ b/lib/pure/encodings.nim
@@ -451,7 +451,7 @@ proc convert*(s: string, destEncoding = "UTF-8",
finally:
close(c)
-when isMainModule:
+when not defined(testing) and isMainModule:
let
orig = "öäüß"
cp1252 = convert(orig, "CP1252", "UTF-8")
diff --git a/lib/pure/fsmonitor.nim b/lib/pure/fsmonitor.nim
index e6919b661..83779eb9c 100644
--- a/lib/pure/fsmonitor.nim
+++ b/lib/pure/fsmonitor.nim
@@ -198,7 +198,7 @@ proc register*(d: Dispatcher, monitor: FSMonitor,
var deleg = toDelegate(monitor)
d.register(deleg)
-when isMainModule:
+when not defined(testing) and isMainModule:
proc main =
var disp = newDispatcher()
var monitor = newMonitor()
diff --git a/lib/pure/ftpclient.nim b/lib/pure/ftpclient.nim
index dc387b79c..b46f8343c 100644
--- a/lib/pure/ftpclient.nim
+++ b/lib/pure/ftpclient.nim
@@ -629,7 +629,7 @@ when isMainModule:
if not d.poll(): break
main()
-when isMainModule and false:
+when not defined(testing) and isMainModule:
var ftp = ftpClient("example.com", user = "foo", pass = "bar")
ftp.connect()
echo ftp.pwd()
diff --git a/lib/pure/gentabs.nim b/lib/pure/gentabs.nim
index a6128efc9..8c89a0ac3 100644
--- a/lib/pure/gentabs.nim
+++ b/lib/pure/gentabs.nim
@@ -186,8 +186,19 @@ when isMainModule:
assert( z["first"]["one"] == 1) # retrieve from first inner table
assert( z["second"]["red"] == 10) # retrieve from second inner table
- 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 d712e53f3..e6c15371e 100644
--- a/lib/pure/htmlgen.nim
+++ b/lib/pure/htmlgen.nim
@@ -483,7 +483,8 @@ macro `var`*(e: expr): expr {.immediate.} =
result = xmlCheckedTag(e, "var", commonAttr)
when isMainModule:
- var nim = "Nim"
- echo h1(a(href="http://nim-lang.org", nim))
- echo form(action="test", `accept-charset` = "Content-Type")
-
+ let nim = "Nim"
+ assert h1(a(href="http://nim-lang.org", nim)) ==
+ """
"""
+ assert form(action="test", `accept-charset` = "Content-Type") ==
+ """"""
diff --git a/lib/pure/htmlparser.nim b/lib/pure/htmlparser.nim
index 5e4eba4e5..9719181b8 100644
--- a/lib/pure/htmlparser.nim
+++ b/lib/pure/htmlparser.nim
@@ -593,7 +593,7 @@ proc loadHtml*(path: string): XmlNode =
var errors: seq[string] = @[]
result = loadHtml(path, errors)
-when isMainModule:
+when not defined(testing) and isMainModule:
import os
var errors: seq[string] = @[]
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index 4c2580da0..9c27ecdab 100644
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -819,7 +819,7 @@ proc get*(client: AsyncHttpClient, url: string): Future[Response] {.async.} =
result = await client.request(redirectTo, httpGET)
lastUrl = redirectTo
-when isMainModule:
+when not defined(testing) and isMainModule:
when true:
# Async
proc main() {.async.} =
diff --git a/lib/pure/httpserver.nim b/lib/pure/httpserver.nim
index 5efdbe297..dc76c9228 100644
--- a/lib/pure/httpserver.nim
+++ b/lib/pure/httpserver.nim
@@ -514,7 +514,7 @@ proc close*(h: PAsyncHTTPServer) =
## Closes the ``PAsyncHTTPServer``.
h.asyncSocket.close()
-when isMainModule:
+when not defined(testing) and isMainModule:
var counter = 0
var s: TServer
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index f0c554551..5d824d6f8 100644
--- a/lib/pure/json.nim
+++ b/lib/pure/json.nim
@@ -1153,19 +1153,22 @@ when false:
when isMainModule:
#var node = parse("{ \"test\": null }")
#echo(node.existsKey("test56"))
+
var parsed = parseFile("tests/testdata/jsontest.json")
var parsed2 = parseFile("tests/testdata/jsontest2.json")
- echo(parsed)
- echo()
- echo(pretty(parsed, 2))
- echo()
- echo(parsed["keyÄÖöoßß"])
- echo()
- echo(pretty(parsed2))
- try:
- echo(parsed["key2"][12123])
- raise newException(ValueError, "That line was expected to fail")
- except IndexError: echo()
+
+ when not defined(testing):
+ echo(parsed)
+ echo()
+ echo(pretty(parsed, 2))
+ echo()
+ echo(parsed["keyÄÖöoßß"])
+ echo()
+ echo(pretty(parsed2))
+ try:
+ echo(parsed["key2"][12123])
+ raise newException(ValueError, "That line was expected to fail")
+ except IndexError: echo()
let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd" }"""
# nil passthrough
@@ -1228,11 +1231,12 @@ when isMainModule:
]
assert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
- discard """
- while true:
- var json = stdin.readLine()
- var node = parse(json)
- echo(node)
- echo()
- echo()
- """
+ when not defined(testing):
+ discard """
+ while true:
+ var json = stdin.readLine()
+ var node = parse(json)
+ echo(node)
+ echo()
+ echo()
+ """
diff --git a/lib/pure/logging.nim b/lib/pure/logging.nim
index ca674af4b..a2ea53472 100644
--- a/lib/pure/logging.nim
+++ b/lib/pure/logging.nim
@@ -278,7 +278,7 @@ proc getLogFilter*(): Level =
# --------------
-when isMainModule:
+when not defined(testing) and isMainModule:
var L = newConsoleLogger()
var fL = newFileLogger("test.log", fmtStr = verboseFmtStr)
var rL = newRollingFileLogger("rolling.log", fmtStr = verboseFmtStr)
diff --git a/lib/pure/marshal.nim b/lib/pure/marshal.nim
index b63c334ff..bf9e33296 100644
--- a/lib/pure/marshal.nim
+++ b/lib/pure/marshal.nim
@@ -244,7 +244,7 @@ proc to*[T](data: string): T =
var tab = initTable[BiggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab)
-when isMainModule:
+when not defined(testing) and isMainModule:
template testit(x: expr) = echo($$to[type(x)]($$x))
var x: array[0..4, array[0..4, string]] = [
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index aa64933fb..daa108460 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -372,7 +372,9 @@ when isMainModule and not defined(JS):
randomize(seed)
for i in 0..SIZE-1:
assert buf[i] == random(high(int)), "non deterministic random seeding"
- echo "random values equal after reseeding"
+
+ when not defined(testing):
+ echo "random values equal after reseeding"
# Check for no side effect annotation
proc mySqrt(num: float): float {.noSideEffect.} =
diff --git a/lib/pure/mersenne.nim b/lib/pure/mersenne.nim
index a6a781cb8..74112e304 100644
--- a/lib/pure/mersenne.nim
+++ b/lib/pure/mersenne.nim
@@ -32,7 +32,7 @@ proc getNum*(m: var MersenneTwister): int =
return int(y)
# Test
-when isMainModule:
+when not defined(testing) and isMainModule:
var mt = newMersenneTwister(2525)
for i in 0..99:
diff --git a/lib/pure/mimetypes.nim b/lib/pure/mimetypes.nim
index a52ba4ebe..642419e64 100644
--- a/lib/pure/mimetypes.nim
+++ b/lib/pure/mimetypes.nim
@@ -518,5 +518,5 @@ proc register*(mimedb: var MimeDB, ext: string, mimetype: string) =
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/oids.nim b/lib/pure/oids.nim
index 0dc8e3c15..ac90dd16b 100644
--- a/lib/pure/oids.nim
+++ b/lib/pure/oids.nim
@@ -88,6 +88,6 @@ proc generatedTime*(oid: Oid): Time =
bigEndian32(addr(tmp), addr(dummy))
result = Time(tmp)
-when isMainModule:
+when not defined(testing) and isMainModule:
let xo = genOid()
echo xo.generatedTime
diff --git a/lib/pure/parsecsv.nim b/lib/pure/parsecsv.nim
index f4943ed89..117d75cfa 100644
--- a/lib/pure/parsecsv.nim
+++ b/lib/pure/parsecsv.nim
@@ -166,7 +166,7 @@ proc close*(my: var CsvParser) {.inline.} =
## closes the parser `my` and its associated input stream.
lexbase.close(my)
-when isMainModule:
+when not defined(testing) and isMainModule:
import os
var s = newFileStream(paramStr(1), fmRead)
if s == nil: quit("cannot open the file" & paramStr(1))
diff --git a/lib/pure/parsesql.nim b/lib/pure/parsesql.nim
index bb4ede779..91917b1c5 100644
--- a/lib/pure/parsesql.nim
+++ b/lib/pure/parsesql.nim
@@ -1330,7 +1330,7 @@ proc renderSQL*(n: SqlNode): string =
result = ""
ra(n, result, 0)
-when isMainModule:
+when not defined(testing) and isMainModule:
echo(renderSQL(parseSQL(newStringStream("""
CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
CREATE TABLE holidays (
diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim
index eb649a878..c07b713de 100644
--- a/lib/pure/parseutils.nim
+++ b/lib/pure/parseutils.nim
@@ -323,8 +323,12 @@ iterator interpolatedFragments*(s: string): tuple[kind: InterpolatedKind,
i = j
when isMainModule:
- for k, v in interpolatedFragments("$test{} $this is ${an{ example}} "):
- 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)
assert value == 56
diff --git a/lib/pure/parsexml.nim b/lib/pure/parsexml.nim
index 2663c5b2f..eb792f086 100644
--- a/lib/pure/parsexml.nim
+++ b/lib/pure/parsexml.nim
@@ -628,7 +628,7 @@ proc next*(my: var XmlParser) =
my.kind = xmlError
my.state = stateNormal
-when isMainModule:
+when not defined(testing) and isMainModule:
import os
var s = newFileStream(paramStr(1), fmRead)
if s == nil: quit("cannot open the file" & paramStr(1))
diff --git a/lib/pure/redis.nim b/lib/pure/redis.nim
index 9177ddee5..aa2e0f9bd 100644
--- a/lib/pure/redis.nim
+++ b/lib/pure/redis.nim
@@ -1080,7 +1080,7 @@ proc assertListsIdentical(listA, listB: seq[string]) =
assert(item == listB[i])
i = i + 1
-when isMainModule:
+when not defined(testing) and isMainModule:
when false:
var r = open()
diff --git a/lib/pure/romans.nim b/lib/pure/romans.nim
index 79fb75526..0c182843a 100644
--- a/lib/pure/romans.nim
+++ b/lib/pure/romans.nim
@@ -44,16 +44,13 @@ proc decimalToRoman*(number: range[1..3_999]): string =
("XC", 90), ("L", 50), ("XL", 40), ("X", 10), ("IX", 9),
("V", 5), ("IV", 4), ("I", 1)]
result = ""
- var decVal = number
+ var decVal: int = number
for key, val in items(romanComposites):
while decVal >= val:
dec(decVal, val)
result.add(key)
when isMainModule:
- import math
- randomize()
- for i in 1 .. 100:
- var rnd = 1 + random(3990)
- assert rnd == rnd.decimalToRoman.romanToDecimal
+ for i in 1 .. 3_999:
+ assert i == i.decimalToRoman.romanToDecimal
diff --git a/lib/pure/selectors.nim b/lib/pure/selectors.nim
index b6bc9dd3a..6901ecf58 100644
--- a/lib/pure/selectors.nim
+++ b/lib/pure/selectors.nim
@@ -296,7 +296,7 @@ proc contains*(s: Selector, key: SelectorKey): bool =
TReadyInfo: ReadyInfo, PSelector: Selector].}
-when isMainModule and not defined(nimdoc):
+when not defined(testing) and isMainModule and not defined(nimdoc):
# Select()
import sockets
type
diff --git a/lib/pure/smtp.nim b/lib/pure/smtp.nim
index 81198f9e1..c1bc259a5 100644
--- a/lib/pure/smtp.nim
+++ b/lib/pure/smtp.nim
@@ -253,7 +253,7 @@ proc close*(smtp: AsyncSmtp) {.async.} =
await smtp.sock.send("QUIT\c\L")
smtp.sock.close()
-when isMainModule:
+when not defined(testing) and isMainModule:
#var msg = createMessage("Test subject!",
# "Hello, my name is dom96.\n What\'s yours?", @["dominik@localhost"])
#echo(msg)
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 2678f4fc8..1b248126b 100644
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -1402,14 +1402,20 @@ when isMainModule:
doAssert align("a", 0) == "a"
doAssert align("1232", 6) == " 1232"
doAssert align("1232", 6, '#') == "##1232"
- 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"
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
- echo formatSize(1'i64 shl 31 + 300'i64) # == "4,GB"
- echo formatSize(1'i64 shl 31)
+ when not defined(testing):
+ echo formatSize(1'i64 shl 31 + 300'i64) # == "4,GB"
+ echo formatSize(1'i64 shl 31)
doAssert "$animal eats $food." % ["animal", "The cat", "food", "fish"] ==
"The cat eats fish."
diff --git a/lib/pure/subexes.nim b/lib/pure/subexes.nim
index d701b85b1..d213c99e6 100644
--- a/lib/pure/subexes.nim
+++ b/lib/pure/subexes.nim
@@ -386,8 +386,13 @@ when isMainModule:
longishA,
longish)"""
- 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)"
@@ -395,7 +400,12 @@ when isMainModule:
doAssert subex"$['''|'|''''|']']#" % "0" == "'|"
- 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/terminal.nim b/lib/pure/terminal.nim
index df637dcb6..a3c5cdcfb 100644
--- a/lib/pure/terminal.nim
+++ b/lib/pure/terminal.nim
@@ -375,7 +375,7 @@ when not defined(windows):
result = stdin.readChar()
discard fd.tcsetattr(TCSADRAIN, addr oldMode)
-when isMainModule:
+when not defined(testing) and isMainModule:
system.addQuitProc(resetAttributes)
write(stdout, "never mind")
eraseLine()
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index 5cc9b4993..c275ede69 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -1036,8 +1036,6 @@ when isMainModule:
# Tue 19 Jan 03:14:07 GMT 2038
var t = getGMTime(fromSeconds(2147483647))
- 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"
@@ -1112,4 +1110,6 @@ when isMainModule:
# Kitchen = "3:04PM"
s = "3:04PM"
f = "h:mmtt"
- echo "Kitchen: " & $s.parse(f)
+ 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 798eef5d0..a83b9be0f 100644
--- a/lib/pure/unidecode/unidecode.nim
+++ b/lib/pure/unidecode/unidecode.nim
@@ -70,5 +70,5 @@ proc unidecode*(s: string): string =
when isMainModule:
loadUnidecodeTable("lib/pure/unidecode/unidecode.dat")
- echo unidecode("Äußerst")
+ assert unidecode("Äußerst") == "Ausserst"
diff --git a/lib/pure/xmldomparser.nim b/lib/pure/xmldomparser.nim
index 7f34d72a8..050362435 100644
--- a/lib/pure/xmldomparser.nim
+++ b/lib/pure/xmldomparser.nim
@@ -155,7 +155,7 @@ proc loadXMLFile*(path: string): PDocument =
return loadXMLStream(s)
-when isMainModule:
+when not defined(testing) and isMainModule:
var xml = loadXMLFile("nim/xmldom/test.xml")
#echo(xml.getElementsByTagName("m:test2")[0].namespaceURI)
#echo(xml.getElementsByTagName("bla:test")[0].namespaceURI)
diff --git a/lib/pure/xmlparser.nim b/lib/pure/xmlparser.nim
index 755bfcdbc..840cae734 100644
--- a/lib/pure/xmlparser.nim
+++ b/lib/pure/xmlparser.nim
@@ -143,7 +143,7 @@ proc loadXml*(path: string): XmlNode =
result = loadXml(path, errors)
if errors.len > 0: raiseInvalidXml(errors)
-when isMainModule:
+when not defined(testing) and isMainModule:
import os
var errors: seq[string] = @[]
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim
index 0bf5b52a4..7526a989a 100644
--- a/lib/pure/xmltree.nim
+++ b/lib/pure/xmltree.nim
@@ -353,5 +353,6 @@ proc findAll*(n: XmlNode, tag: string): seq[XmlNode] =
findAll(n, tag, result)
when isMainModule:
- assert """Nim rules.""" ==
+ let link = "http://nim-lang.org"
+ assert """Nim rules.""" ==
$(<>a(href="http://nim-lang.org", newText("Nim rules.")))
diff --git a/lib/wrappers/claro.nim b/lib/wrappers/claro.nim
index d36b9f178..0fb0882bf 100644
--- a/lib/wrappers/claro.nim
+++ b/lib/wrappers/claro.nim
@@ -2710,7 +2710,7 @@ proc workspace_window_set_icon*(w: ptr TWorkspaceWindow, icon: ptr TImage){.
claro_base_init()
claro_graphics_init()
-when isMainModule:
+when not defined(testing) and isMainModule:
var w = newWindow(nil, newBounds(100, 100, 230, 230), 0)
window_set_title(w, "Hello, World!")