-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
unit-test.rkt
87 lines (73 loc) · 2.53 KB
/
unit-test.rkt
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
(provide
test
(for-syntax check-equal?)
(for-syntax check-err)
(for-syntax test-module))
(define *SUCCESS-COUNT* 0)
(define *FAILURE-COUNT* 0)
;; Failed tests
(define *failures* '())
(define (mark-success)
(set! *SUCCESS-COUNT* (+ *SUCCESS-COUNT* 1)))
(define (mark-failed name)
(set! *FAILURE-COUNT* (+ *FAILURE-COUNT* 1))
(set! *failures* (cons name *failures*)))
(define (print-success name)
(display "test > ")
(display name)
(display " ... ")
(display-color "Ok" 'green)
(newline))
(define (print-failure name)
(display "test > ")
(display name)
(display " ... ")
(display-color "FAILED" 'red)
(newline))
(define-syntax check-equal?
(syntax-rules ()
[(check-equal? name input expected)
(with-handler (lambda (err) (mark-failed name)
(print-failure name)
(displayln err))
(test name input expected))]))
(define-syntax check-err?
(syntax-rules ()
[(check-equal? name input expected)
(with-handler (lambda (err) (mark-success)
(print-success name))
(test name input expected))]))
;; Check the equality, and otherwise do some nice printing of the result
(define (test name input expected)
(if (equal? input expected)
(begin
(mark-success)
(print-success name))
(begin
(mark-failed name)
(print-failure name)
(display " Expected: ")
(display expected)
(display ", Found ")
(displayln input))))
(define-syntax test-module
(syntax-rules ()
[(test-module name expr ...)
(begin
(display "###### Running tests for module ") (display name) (displayln " ######")
(begin expr ...)
(display "Test result: ") (display *SUCCESS-COUNT*) (display " passed; ") (display *FAILURE-COUNT*) (displayln " failed;")
(display "Failures: ") (displayln *failures*)
)]
[(test-module expr ...)
(begin expr ...)]))
; (test-module
; (check-equal? "Checks that the expressions make sense"
; (+ 10 20 30)
; (+ 10 20 30))
; (check-err? "This should fail (in a good way)!"
; (error! "Throwing an error")
; (+ 10 20 30))
; (check-equal? "This should fail"
; (+ 10 20)
; (+ 30 40)))