Skip to content

Commit 4853b49

Browse files
committed
refact(Lib/unittest): split to unittest/case_py
1 parent 4c8e84e commit 4853b49

File tree

4 files changed

+133
-122
lines changed

4 files changed

+133
-122
lines changed

src/pylib/Lib/unittest.nim

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,3 @@
1-
## Lib/unittest
2-
##
3-
## .. hint:: Currently `self: TestCase` in all functions is only a placeholder,
4-
## the actual implementation just replies the std/unittest.
5-
##
61

7-
import std/unittest
8-
import std/macros
9-
from ../pyerrors/simperr import TypeError
10-
export TypeError
11-
12-
type
13-
TestCase* = ref object of RootObj
14-
15-
func newTestCase*: TestCase = TestCase()
16-
17-
18-
macro genSelf(templ) =
19-
result = newStmtList()
20-
result.add templ
21-
var nTempl = copyNimTree templ
22-
nTempl.params.insert 1, newIdentDefs(ident"self", bindSym"TestCase")
23-
result.add nTempl
24-
25-
template gen1(name, op){.dirty.} =
26-
bind check
27-
template name*(a){.genSelf.} =
28-
check op(a)
29-
30-
template asis[T](a: T): T = a
31-
template gen1(name){.dirty.} =
32-
gen1 name, asis
33-
34-
template gen2(name, op){.dirty.} =
35-
bind check
36-
template name*(a, b){.genSelf.} =
37-
check op(a, b)
38-
39-
gen1 assertFalse, `not`
40-
gen1 assertTrue
41-
42-
template assertRaises*(typ: typedesc, cb: typed, va: varargs[untyped]){.genSelf.} =
43-
bind expect
44-
expect typ:
45-
when compiles((let _ = cb(va))): discard cb(va)
46-
else: cb(va)
47-
48-
template assertRaises*(typ: typedesc[TypeError], cb: typed, va: varargs[untyped]){.genSelf.} =
49-
bind expect
50-
when compiles((let _ = cb(va))):
51-
expect typ: discard cb(va)
52-
elif compiles(cb(va)):
53-
expect typ: cb(va)
54-
else:
55-
discard ## "not compile" is seen as Compile-time TypeError
56-
57-
gen2 assertEqual, `==`
58-
gen2 assertNotEqual, `!=`
59-
60-
gen2 assertIn, `in`
61-
gen2 assertNotIn, `not_in`
62-
63-
template assertIsOrNotIs[T](a, b: T; op){.genSelf.} =
64-
bind check
65-
when T is (pointer|ptr|ref|proc|iterator):
66-
check a op b
67-
elif a is static or b is static:
68-
check a op b
69-
else:
70-
check a.addr op b.addr
71-
72-
template assertIs*[T](a, b: T){.genSelf.} =
73-
bind assertIsOrNotIs
74-
assertIsOrNotIs(a, b, `==`)
75-
76-
template assertIsNot*[T](a, b: T){.genSelf.} =
77-
bind assertIsOrNotIs
78-
assertIsOrNotIs(a, b, `!=`)
79-
80-
gen2 assertLess, `<`
81-
gen2 assertLessEqual, `<=`
82-
gen2 assertGreater, `>`
83-
gen2 assertGreaterEqual, `>=`
84-
85-
gen2 assertIsInstance, `is`
86-
gen2 assertNotIsInstance, `is_not`
87-
88-
template addSkip(reason) = checkpoint reason ##\
89-
## XXX: TODO: not really python-like
90-
91-
template skipTest*(reason: string){.genSelf.} =
92-
bind skip
93-
addSkip reason
94-
skip()
95-
96-
template skip*(reason; body) =
97-
## EXT.
98-
addSkip reason
99-
100-
template skip*(reason: string): untyped =
101-
addSkip reason
102-
proc (_: proc) = discard
103-
104-
proc asis_id[P: proc](x: P): P = x
105-
106-
template skipIf*(condition: bool, reason: string) =
107-
bind asis_id, skip
108-
if condition:
109-
return skip(reason)
110-
return asis_id
111-
112-
template skipIf*(condition: bool, reason: string; body) =
113-
addSkip reason
114-
if not condition:
115-
body
116-
117-
template skipUnless*(condition: bool, reason: string) =
118-
bind skipIf
119-
skipIf(not condition, reason)
120-
121-
template skipUnless*(condition: bool, reason: string; body) =
122-
bind skipIf
123-
skipIf(not condition, reason, body)
2+
import ./unittest/case_py
3+
export case_py

src/pylib/Lib/unittest/case_py.nim

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
## Lib/unittest
2+
##
3+
## .. hint:: Currently `self: TestCase` in all functions is only a placeholder,
4+
## the actual implementation just replies the std/unittest.
5+
##
6+
7+
import std/unittest
8+
import std/macros
9+
from ../../pyerrors/simperr import TypeError
10+
export TypeError
11+
12+
import ./case_py/[
13+
types,
14+
util
15+
]
16+
export types
17+
18+
template gen1(name, op){.dirty.} =
19+
bind check
20+
template name*(a){.genSelf.} =
21+
check op(a)
22+
23+
template asis[T](a: T): T = a
24+
template gen1(name){.dirty.} =
25+
gen1 name, asis
26+
27+
template gen2(name, op){.dirty.} =
28+
bind check
29+
template name*(a, b){.genSelf.} =
30+
check op(a, b)
31+
32+
gen1 assertFalse, `not`
33+
gen1 assertTrue
34+
35+
template assertRaises*(typ: typedesc, cb: typed, va: varargs[untyped]){.genSelf.} =
36+
bind expect
37+
expect typ:
38+
when compiles((let _ = cb(va))): discard cb(va)
39+
else: cb(va)
40+
41+
template assertRaises*(typ: typedesc[TypeError], cb: typed, va: varargs[untyped]){.genSelf.} =
42+
bind expect
43+
when compiles((let _ = cb(va))):
44+
expect typ: discard cb(va)
45+
elif compiles(cb(va)):
46+
expect typ: cb(va)
47+
else:
48+
discard ## "not compile" is seen as Compile-time TypeError
49+
50+
gen2 assertEqual, `==`
51+
gen2 assertNotEqual, `!=`
52+
53+
gen2 assertIn, `in`
54+
gen2 assertNotIn, `not_in`
55+
56+
template assertIsOrNotIs[T](a, b: T; op){.genSelf.} =
57+
bind check
58+
when T is (pointer|ptr|ref|proc|iterator):
59+
check a op b
60+
elif a is static or b is static:
61+
check a op b
62+
else:
63+
check a.addr op b.addr
64+
65+
template assertIs*[T](a, b: T){.genSelf.} =
66+
bind assertIsOrNotIs
67+
assertIsOrNotIs(a, b, `==`)
68+
69+
template assertIsNot*[T](a, b: T){.genSelf.} =
70+
bind assertIsOrNotIs
71+
assertIsOrNotIs(a, b, `!=`)
72+
73+
gen2 assertLess, `<`
74+
gen2 assertLessEqual, `<=`
75+
gen2 assertGreater, `>`
76+
gen2 assertGreaterEqual, `>=`
77+
78+
gen2 assertIsInstance, `is`
79+
gen2 assertNotIsInstance, `is_not`
80+
81+
template addSkip(reason) = checkpoint reason ##\
82+
## XXX: TODO: not really python-like
83+
84+
template skipTest*(reason: string){.genSelf.} =
85+
bind skip
86+
addSkip reason
87+
skip()
88+
89+
template skip*(reason; body) =
90+
## EXT.
91+
addSkip reason
92+
93+
template skip*(reason: string): untyped =
94+
addSkip reason
95+
proc (_: proc) = discard
96+
97+
proc asis_id[P: proc](x: P): P = x
98+
99+
template skipIf*(condition: bool, reason: string) =
100+
bind asis_id, skip
101+
if condition:
102+
return skip(reason)
103+
return asis_id
104+
105+
template skipIf*(condition: bool, reason: string; body) =
106+
addSkip reason
107+
if not condition:
108+
body
109+
110+
template skipUnless*(condition: bool, reason: string) =
111+
bind skipIf
112+
skipIf(not condition, reason)
113+
114+
template skipUnless*(condition: bool, reason: string; body) =
115+
bind skipIf
116+
skipIf(not condition, reason, body)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
type
4+
TestCase* = ref object of RootObj
5+
6+
func newTestCase*: TestCase = TestCase()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import std/macros
2+
import ./types
3+
4+
macro genSelf*(templ) =
5+
result = newStmtList()
6+
result.add templ
7+
var nTempl = copyNimTree templ
8+
nTempl.params.insert 1, newIdentDefs(ident"self", bindSym"TestCase")
9+
result.add nTempl

0 commit comments

Comments
 (0)