Skip to content

Commit f7c6b3e

Browse files
Add assertThrows-like test functionality
1 parent f6f3d02 commit f7c6b3e

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Test test suite on Exceptions
2+
✓ Test test assertNotThrown pass
3+
✕ Test test assertNotThrown fail
4+
Unexpected Exception
5+
✕ Test test assertThrown fail
6+
Expected Exception, but exited normally
7+
✓ Test test assertThrows pass
8+
✓ Test test assertNoThrow pass
9+
✕ Test test assertNoThrow fail
10+
Unexpected Exception
11+
✕ Test test assertThrows fail
12+
Expected Exception, but exited normally
13+
✓ Test test assertThrows pass
14+
15+
4 pass
16+
4 fail
17+
8 tests total
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import test
2+
3+
def main() = {
4+
// Don't print out times in CI.
5+
suite("Test test suite on Exceptions", false) {
6+
// on[E] variant
7+
// -------------
8+
test("Test test assertNotThrown pass") {
9+
with on[MissingValue].assertNotThrown
10+
assert(Some(12).value, 12)
11+
}
12+
test("Test test assertNotThrown fail") {
13+
with on[MissingValue].assertNotThrown
14+
assert(None().value, 12)
15+
}
16+
test("Test test assertThrown fail") {
17+
with on[MissingValue].assertThrown
18+
assert(Some(12).value, 12)
19+
}
20+
test("Test test assertThrows pass") {
21+
with on[MissingValue].assertThrown[MissingValue]
22+
assert(None().value, 12)
23+
}
24+
// direct variant
25+
// --------------
26+
test("Test test assertNoThrow pass") {
27+
with assertNoThrow[MissingValue]
28+
assert(Some(12).value, 12)
29+
}
30+
test("Test test assertNoThrow fail") {
31+
with assertNoThrow[MissingValue]
32+
assert(None().value, 12)
33+
}
34+
test("Test test assertThrows fail") {
35+
with assertThrows[MissingValue]
36+
assert(Some(12).value, 12)
37+
}
38+
test("Test test assertThrows pass") {
39+
with assertThrows[MissingValue]
40+
assert(None().value, 12)
41+
}
42+
};
43+
()
44+
}

libraries/common/test.effekt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ def assertEqual[A](obtained: A, expected: A) { equals: (A, A) => Bool } { show:
5959
// NOTE: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6060
// Here's an accidental capture! Can we prevent this somehow nicely?
6161

62+
def assertNotThrown[E](ex: on[E]){ body: => Unit / Exception[E] }: Unit / Assertion = {
63+
ex.default{ do assert(false, "Unexpected Exception") }{body}
64+
}
65+
def assertNoThrow[E]{ body: => Unit / Exception[E] }: Unit / Assertion = {
66+
on[E].default{ do assert(false, "Unexpected Exception") }{body}
67+
}
68+
def assertThrown[E](ex: on[E]){ body: => Unit / Exception[E] }: Unit / Assertion = {
69+
do assert(ex.default{ true }{ body(); false }, "Expected Exception, but exited normally")
70+
}
71+
def assertThrows[E]{body: => Unit / Exception[E] }: Unit / Assertion = {
72+
do assert(on[E].default{ true }{ body(); false }, "Expected Exception, but exited normally")
73+
}
6274

6375
interface Test {
6476
def success(name: String, duration: Int): Unit

0 commit comments

Comments
 (0)