Skip to content

Commit a814951

Browse files
committed
feat(Lib/unittest): skip* now will output [SKIPPED] if possible, instead of nothing
1 parent ec62e98 commit a814951

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

src/pylib/Lib/unittest/case_py.nim

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,38 @@ gen2 assertGreaterEqual, `>=`
9191
gen2 assertIsInstance, `is`
9292
gen2 assertNotIsInstance, `is_not`
9393
94-
template addSkip(reason) = checkpoint reason ##\
95-
## XXX: TODO: not really python-like
94+
func fmtSkip(name, reason: string): string = name & ": " & reason
9695
9796
template skipTest*(reason: string){.genSelf.} =
98-
bind skip
99-
addSkip reason
97+
bind skip, fmtSkip
98+
const msg = fmtSkip(astToStr(self), reason)
99+
checkpoint msg
100+
## XXX: TODO: not really python-like
100101
skip()
101102
103+
proc skipTest(reason, testStmt: NimNode): NimNode =
104+
if testStmt.kind == nnkCommand:
105+
result = newNimNode nnkCommand
106+
result.add testStmt[0]
107+
let name = testStmt[1]
108+
result.add newCall(bindSym"fmtSkip", name, reason)
109+
result.add newStmtList(
110+
newCall(bindSym"skip")
111+
)
112+
else:
113+
result = newCall(bindSym"skipTest", reason)
114+
102115
template skip*(reason; body) =
103116
## EXT.
104-
addSkip reason
117+
bind skipTest
118+
skipTest(reason, body)
105119

106120
template skip*(reason: string): untyped =
107-
addSkip reason
108-
proc (_: proc) = discard
121+
bind skip, fmtSkip
122+
template temp(p: proc) =
123+
test fmtSkip(astToStr(p), reason):
124+
skip()
125+
temp
109126

110127
proc asis_id[P: proc](x: P): P = x
111128

@@ -114,20 +131,37 @@ template skipIf*(condition: bool, reason: string): proc =
114131
if condition: skip(reason)
115132
else: asis_id
116133

117-
template doIf(cond: bool; body) =
118-
if cond: body
119-
template doIf(cond: static bool; body) =
120-
when cond: body
134+
macro makeSkip(cond: bool; reason: string; body; staticBanch: static[bool]) =
135+
result = newNimNode(if staticBanch: nnkWhenStmt else: nnkIfStmt)
136+
result.add nnkElifBranch.newTree(
137+
cond, body
138+
)
139+
var elseBody = newStmtList()
140+
if body.kind == nnkStmtList:
141+
for s in body:
142+
elseBody.add skipTest(reason, s)
143+
elif body.kind == nnkCommand and body[0].eqIdent "test":
144+
# test "xxx"
145+
elseBody.add skipTest(reason, body)
146+
result.add nnkElse.newTree elseBody
147+
148+
template doOrSkip(cond: bool; reason: string; body) =
149+
bind makeSkip
150+
makeSkip cond, reason, body, false
151+
template doOrSkip(cond: static bool; reason: string; body) =
152+
bind makeSkip
153+
makeSkip cond, reason, body, true
121154

122155
template skipIf*(condition: bool, reason: string; body) =
123-
bind doIf
124-
addSkip reason
125-
doIf not condition, body
156+
## EXT.
157+
bind doOrSkip
158+
doOrSkip not condition, reason, body
126159

127160
template skipUnless*(condition: bool, reason: string): proc =
128161
bind skipIf
129162
skipIf(not condition, reason)
130163

131164
template skipUnless*(condition: bool, reason: string; body) =
165+
## EXT.
132166
bind skipIf
133167
skipIf(not condition, reason, body)

tests/testaments/builtins/round_float.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ discard """
22
action: "run"
33
targets: "c js"
44
5-
output: ""
5+
# output: "" this test also uses unittest.task
66
"""
77

88
import pylib/builtins # /[round, attr]
@@ -12,7 +12,7 @@ import pylib/builtins/[pyrange, complex, format]
1212
import pylib/numTypes/floats
1313

1414
import pylib/Lib/[unittest, math, random, sys]
15-
15+
import std/unittest as std_unittest
1616
import ../../utils
1717

1818
when intOver64b:
@@ -74,7 +74,7 @@ block:
7474
]#
7575
unittest.skipUnless(getattr(sys, "float_repr_style", "") == "short",
7676
"applies only when using short float repr style"):
77-
# def test_previous_round_bugs(self):
77+
test "previous_round_bugs":
7878
self.assertEqual(round(562949953421312.5, 1),
7979
562949953421312.5)
8080

@@ -96,7 +96,7 @@ block:
9696

9797
unittest.skipUnless(getattr(sys, "float_repr_style", "") == "short",
9898
"applies only when using short float repr style"):
99-
#def test_matches_float_format(self):
99+
test "matches_float_format":
100100
# round should give the same results as float formatting
101101
proc chk(x: float) =
102102
self.assertEqual(float(format(x, ".0f")), round(x, 0))

0 commit comments

Comments
 (0)