1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#
#
# Nimrod's Runtime Library
# (c) Copyright 2011 Nimrod Contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## :Author: Zahary Karadjov (zah@github)
##
## This module implements the standard unit testing facilities such as
## suites, fixtures and test cases as well as facilities for combinatorial
## and randomzied test case generation (not yet available)
## and object mocking (not yet available)
##
## It is loosely based on C++'s boost.test and Haskell's QuickTest
import
macros, terminal, os
type
TTestStatus* = enum OK, FAILED
TOutputLevel* = enum PRINT_ALL, PRINT_FAILURES, PRINT_NONE
var
# XXX: These better be thread-local
AbortOnError*: bool
OutputLevel*: TOutputLevel
ColorOutput*: bool
checkpoints: seq[string] = @[]
template TestSetupIMPL*: stmt = nil
template TestTeardownIMPL*: stmt = nil
proc shouldRun(testName: string): bool =
result = true
template suite*(name: expr, body: stmt): stmt =
block:
template setup*(setupBody: stmt): stmt =
template TestSetupIMPL: stmt = setupBody
template teardown*(teardownBody: stmt): stmt =
template TestTeardownIMPL: stmt = teardownBody
body
proc testDone(name: string, s: TTestStatus) =
if s == FAILED:
program_result += 1
if OutputLevel != PRINT_NONE and (OutputLevel == PRINT_ALL or s == FAILED):
var color = (if s == OK: fgGreen else: fgRed)
if ColorOutput:
styledEcho styleBright, color, "[", $s, "] ", fgWhite, name, "\n"
else:
echo "[", $s, "] ", name, "\n"
template test*(name: expr, body: stmt): stmt =
bind shouldRun, checkpoints, testDone
if shouldRun(name):
checkpoints = @[]
var TestStatusIMPL = OK
try:
TestSetupIMPL()
body
finally:
TestTeardownIMPL()
testDone name, TestStatusIMPL
proc checkpoint*(msg: string) =
checkpoints.add(msg)
# TODO: add support for something like SCOPED_TRACE from Google Test
template fail* =
bind checkpoints
for msg in items(checkpoints):
echo msg
if AbortOnError: quit(1)
TestStatusIMPL = FAILED
checkpoints = @[]
macro check*(conditions: stmt): stmt =
proc standardRewrite(e: expr): stmt =
template rewrite(Exp, lineInfoLit: expr, expLit: string): stmt =
if not Exp:
checkpoint(lineInfoLit & ": Check failed: " & expLit)
fail()
result = getAst(rewrite(e, e.lineinfo, e.toStrLit))
case conditions.kind
of nnkCall, nnkCommand, nnkMacroStmt:
case conditions[1].kind
of nnkInfix:
proc rewriteBinaryOp(op: expr): stmt =
template rewrite(op, left, right, lineInfoLit: expr, opLit, leftLit, rightLit: string): stmt =
block:
var
lhs = left
rhs = right
if not `op`(lhs, rhs):
checkpoint(lineInfoLit & ": Check failed: " & opLit)
checkpoint(" " & leftLit & " was " & $lhs)
checkpoint(" " & rightLit & " was " & $rhs)
fail()
result = getAst(rewrite(
op[0], op[1], op[2],
op.lineinfo,
op.toStrLit,
op[1].toStrLit,
op[2].toStrLit))
result = rewriteBinaryOp(conditions[1])
of nnkCall, nnkCommand:
# TODO: We can print out the call arguments in case of failure
result = standardRewrite(conditions[1])
of nnkStmtList:
result = newNimNode(nnkStmtList)
for i in countup(0, conditions[1].len - 1):
result.add(newCall(!"check", conditions[1][i]))
else:
result = standardRewrite(conditions[1])
else:
var ast = conditions.treeRepr
error conditions.lineinfo & ": Malformed check statement:\n" & ast
template require*(conditions: stmt): stmt =
block:
const AbortOnError = true
check conditions
macro expect*(exp: stmt): stmt =
template expectBody(errorTypes, lineInfoLit: expr, body: stmt): stmt =
try:
body
checkpoint(lineInfoLit & ": Expect Failed, no exception was thrown.")
fail()
except errorTypes:
nil
var expectCall = exp[0]
var body = exp[1]
var errorTypes = newNimNode(nnkBracket)
for i in countup(1, expectCall.len - 1):
errorTypes.add(expectCall[i])
result = getAst(expectBody(errorTypes, exp.lineinfo, body))
## Reading settings
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string
if envOutLvl.len > 0:
for opt in countup(low(TOutputLevel), high(TOutputLevel)):
if $opt == envOutLvl:
OutputLevel = opt
break
AbortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
ColorOutput = not existsEnv("NIMTEST_NO_COLOR")
|