Skip to content

Commit 480cdba

Browse files
authored
Merge pull request #34 from nomennescio/fix-issue-33
Fix issue 33
2 parents 55282e1 + e50edf6 commit 480cdba

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

test/test-error-handling.expected

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,38 @@
33

44
<IT::>Unexpected system error in user code
55

6-
<ERROR::>ERROR:{ division-by-zero { x 1 } }
6+
<ERROR::>Thrown: ERROR:{ division-by-zero { x 1 } }
77

8-
<COMPLETEDIN::>0.382253 ms
8+
<COMPLETEDIN::>0.274156 ms
99

1010
<IT::>Unexpected custom error in user code
1111

12-
<ERROR::>ERROR:{ custom-error { error-message "thrown custom error" } {...
12+
<ERROR::>Thrown: ERROR:{ custom-error { error-message "thrown custom error" } {...
1313

14-
<COMPLETEDIN::>0.110253 ms
14+
<COMPLETEDIN::>0.179815 ms
1515

1616
<IT::>Expected custom error
1717

1818
<PASSED::>Test Passed
1919

20-
<COMPLETEDIN::>0.089989 ms
20+
<COMPLETEDIN::>0.059046 ms
2121

2222
<IT::>Missing expected custom error
2323

24-
<FAILED::>Test Failed : Expected :<:LF:>ERROR:{ custom-error<:LF:> { error-message "thrown custom error" }<:LF:> { integer-argument 1 }<:LF:>}<:LF:>but got :<:LF:>1
24+
<FAILED::>Test Failed : Expected :<:LF:>THROWN: ERROR:{ custom-error<:LF:> { error-message "thrown custom error" }<:LF:> { integer-argument 1 }<:LF:>}<:LF:>but got :<:LF:>1
2525

26-
<COMPLETEDIN::>0.239820 ms
26+
<COMPLETEDIN::>0.166183 ms
2727

28-
<COMPLETEDIN::>1.043601 ms
28+
<IT::>Missing expected thrown error
29+
30+
<FAILED::>Test Failed : Expected :<:LF:>THROWN: "thrown error"<:LF:>but got :<:LF:>"thrown error"
31+
32+
<COMPLETEDIN::>0.028068 ms
33+
34+
<IT::>Missing expected thrown custom error
35+
36+
<FAILED::>Test Failed : Expected :<:LF:>THROWN: ERROR:{ custom-error<:LF:> { error-message "thrown custom error" }<:LF:> { integer-argument 1 }<:LF:>}<:LF:>but got :<:LF:>ERROR:{ custom-error<:LF:> { error-message "thrown custom error" }<:LF:> { integer-argument 1 }<:LF:>}
37+
38+
<COMPLETEDIN::>0.056286 ms
39+
40+
<COMPLETEDIN::>0.952354 ms

test/test-error-handling.factor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
! Copyright 2022 nomennescio
22

3-
USING: tools.testest math ;
3+
USING: tools.testest kernel math ;
44
IN: tests
55

66
ERROR: custom-error error-message integer-argument ;
7+
C: <custom-error> custom-error
78

89
: run-tests ( -- )
910

@@ -20,6 +21,12 @@ ERROR: custom-error error-message integer-argument ;
2021
"Missing expected custom error" it#{
2122
<{ 1 -> "thrown custom error" 1 custom-error }>
2223
}#
24+
"Missing expected thrown error" it#{
25+
<{ "thrown error" -> "thrown error" throw }>
26+
}#
27+
"Missing expected thrown custom error" it#{
28+
<{ "thrown custom error" 1 <custom-error> -> "thrown custom error" 1 custom-error }>
29+
}#
2330
}#
2431
;
2532

test/test-stack-underflow.expected

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@
33

44
<IT::>single test
55

6-
<ERROR::>Data stack underflow
6+
<ERROR::>Thrown: Data stack underflow
77

88

9-
<COMPLETEDIN::>0.052173 ms
9+
<COMPLETEDIN::>0.143086 ms
1010

1111
<IT::>double test
1212

13-
<ERROR::>Data stack underflow
13+
<ERROR::>Thrown: Data stack underflow
1414

1515

16-
<ERROR::>Data stack underflow
16+
<ERROR::>Thrown: Data stack underflow
1717

1818

19-
<COMPLETEDIN::>0.020681 ms
19+
<COMPLETEDIN::>0.019864 ms
2020

2121
<IT::>double test
2222

23-
<ERROR::>Data stack underflow
23+
<ERROR::>Thrown: Data stack underflow
2424

2525

26-
<ERROR::>Data stack underflow
26+
<ERROR::>Thrown: Data stack underflow
2727

2828

29-
<COMPLETEDIN::>0.013233 ms
29+
<COMPLETEDIN::>0.015669 ms
3030

3131
<IT::>double test
3232

33-
<ERROR::>Data stack underflow
33+
<ERROR::>Thrown: Data stack underflow
3434

3535

36-
<ERROR::>Data stack underflow
36+
<ERROR::>Thrown: Data stack underflow
3737

3838

39-
<COMPLETEDIN::>0.012404 ms
39+
<COMPLETEDIN::>0.015315 ms
4040

41-
<COMPLETEDIN::>0.314743 ms
41+
<COMPLETEDIN::>0.341698 ms

tools/testest/testest.factor

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SYMBOL: test-failed.
2323

2424
<PRIVATE
2525

26-
: with-message ( quot -- message ) with-string-writer unclip-last [ "\n" "<:LF:>" replace ] dip suffix write ; inline
26+
: with-message ( quot -- ) with-string-writer unclip-last [ "\n" "<:LF:>" replace ] dip suffix write ; inline
2727

2828
: passed. ( -- ) [ test-passed. get call( -- ) ] with-message ; inline
2929
: failed. ( error -- ) [ test-failed. get call( error -- ) ] with-message ; inline
@@ -32,10 +32,15 @@ SYMBOL: test-failed.
3232
: failed# ( -- ) nl "<FAILED::>" write ;
3333
: error# ( -- ) nl "<ERROR::>" write ;
3434

35-
: catch-all ( stack quot -- stack' throwed? ) '[ _ _ with-datastack f ] [ 1array t ] recover ; inline
35+
ERROR: thrown error ;
36+
37+
: catch-all ( stack quot -- stack' ) '[ _ _ with-datastack ] [ \ thrown boa 1array ] recover ; inline
38+
39+
: unexpected-error? ( obj1 obj2 -- ? ) ?first thrown? not swap ?first thrown? and ;
3640

3741
: (unit-test) ( test-quot expected-quot -- )
38-
[ { } swap catch-all ] bi@ not rot and [ drop first error# [ print-error ] with-message nl ]
42+
[ { } swap catch-all ] bi@ 2dup unexpected-error?
43+
[ drop first error# [ print-error ] with-message nl ]
3944
[ '[ _ _ assert-sequence= passed# passed. nl ] [ failed# failed. nl ] recover ] if
4045
;
4146

@@ -66,12 +71,17 @@ SYMBOL: ERROR:{
6671
: pprint-error ( error-tuple -- ) [ ERROR:{ ] dip [ class-of ] [ tuple>assoc ] bi \ } (pprint-tuple) ;
6772

6873
! print errors differently from tuples
74+
6975
M: tuple pprint* dup class-of error-class? [ pprint-error ] [ pprint-tuple ] if ;
7076
M: tuple error. dup class-of error-class? [ pprint-short ] [ describe ] if ;
7177

78+
SYMBOL: THROWN:
79+
M: thrown pprint* \ THROWN: pprint-word error>> pprint* ;
80+
M: thrown error. "Thrown: " write error>> error. ;
81+
7282
M: assert-sequence error.
7383
[ "Expected :" write expected>> seq. ]
7484
[ nl "but got :" write got>> seq. ] bi
7585
;
7686

77-
PRIVATE>
87+
PRIVATE>

0 commit comments

Comments
 (0)