aboutsummaryrefslogtreecommitdiffstats
path: root/lib/csc/testing.csc
blob: 68678cab91df549cc058ba6565bd1ff02e1b469f (plain) (blame)
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
87
88
89
90
91
(define-library (csc testing)
  (export
    assert
    assert-equal
    assert-raises
    test
    test-main)
  (import (scheme base)
          (only (scheme write)
            display
            write)
          (only (csc compare) diff))
  (begin


    (define *all-tests-succeeded* #t)


    (define (set-all-succeeded! val) (set! *all-tests-succeeded* val))


    (define-record-type <test-handle>
      (make-test-handle test-name)
      test-handle?
      (test-name test-name set-name!))


    (define-record-type <test-error>
      (make-test-error)
      test-error?)


    (define *test-error* (make-test-error))


    (define *test-handle* (make-test-handle "global"))


    (define (print . xs)
      (unless (null? xs)
        (let ((x (car xs)))
          (if (or (string? x)
                  (symbol? x))
            (display x)
            (write x)))
        (apply print (cdr xs))))


    (define-syntax test
      (syntax-rules ()
        ((test name body body* ...)
         (begin
           (set-name! *test-handle* (symbol->string 'name))
           (print "=== RUN   " 'name "\n")
           (guard (e ((test-error? e)
                       (print "--- FAIL: " 'name "\n")
                       (set-all-succeeded! #f)))
             body body* ...
             (print "--- PASS: " 'name "\n"))))))


    (define-syntax assert
      (syntax-rules ()
        ((assert expr)
         (unless expr
           (print (test-name *test-handle*) ": (assert " 'expr ") failed.\n")
           (raise *test-error*)))))


    (define-syntax assert-equal
      (syntax-rules ()
        ((assert-equal left right transformers ...)
          (let ((d (diff left right transformers ...)))
            (unless (string=? "" d)
              (print "Fatal:\n  " 'left "\nis not equal to\n  " 'right "\ndiff (-left +right):\n" d "\n")
              (raise *test-error*))))))


    (define-syntax assert-raises
      (syntax-rules ()
        ((assert-raises predicate body body* ...)
          (assert
            (guard (e ((predicate e) #t))
              body body* ...
              #f)))))


    (define (test-main)
      (if *all-tests-succeeded*
        (print "PASS\n")
        (print "FAIL\n")))))