sema-test 0.1.0

Test framework for Sema — define, run, and report test results

Source

package.sema

115 lines c76dd260aaa0
File details

Source stored with sema-test 0.1.0. Line links refer to these exact bytes.

SHA-256 c76dd260aaa0a5c769b20b89e472b8a84f141ff915abd1bda4539fe29990f0cf

1;; sema-test — test framework for Sema: define, run, and report test results
2
3(module sema-test
4  (export test/assert-equal
5          test/assert-true
6          test/assert-false
7          test/assert-throws
8          test/assert-near
9          test/assert-contains
10          test/run
11          test/run!)
12
13  ;; -------------------------------------------------------------------------
14  ;; Assertions — each raises a descriptive error on failure, returns #t on pass
15  ;; -------------------------------------------------------------------------
16
17  (define (test/assert-equal expected actual)
18    "Assert that expected and actual are deeply equal."
19    (when (not (equal? expected actual))
20      (error (format "expected ~a, got ~a" expected actual)))
21    #t)
22
23  (define (test/assert-true value)
24    "Assert that value is truthy."
25    (when (not value)
26      (error "expected truthy value, got falsy"))
27    #t)
28
29  (define (test/assert-false value)
30    "Assert that value is falsy."
31    (when value
32      (error (format "expected falsy value, got ~a" value)))
33    #t)
34
35  (define (test/assert-throws thunk)
36    "Assert that calling thunk raises an error."
37    (let ((threw #f))
38      (try
39        (begin (thunk) nil)
40        (catch e (set! threw #t)))
41      (when (not threw)
42        (error "expected an error to be thrown, but none was"))
43      #t))
44
45  (define (test/assert-near expected actual tolerance)
46    "Assert that expected and actual differ by at most tolerance."
47    (let ((diff (abs (- expected actual))))
48      (when (> diff tolerance)
49        (error (format "expected ~a ≈ ~a (tolerance ~a), diff was ~a"
50                       expected actual tolerance diff)))
51      #t))
52
53  (define (test/assert-contains haystack needle)
54    "Assert that a list contains an element or a string contains a substring."
55    (let ((found (cond
56                   ((string? haystack) (string/contains? haystack needle))
57                   ((list? haystack)   (any #(equal? % needle) haystack))
58                   (#t (error (format "test/assert-contains: expected list or string, got ~a" haystack))))))
59      (when (not found)
60        (error (format "expected ~a to contain ~a" haystack needle)))
61      #t))
62
63  ;; -------------------------------------------------------------------------
64  ;; Runner
65  ;; -------------------------------------------------------------------------
66
67  ;; "<native-fn check-parse>" / "<lambda check-parse>" → "check-parse"
68  (define (fn-name fn-val)
69    (let ((s (str fn-val)))
70      (if (and (string/starts-with? s "<")
71               (string/ends-with? s ">")
72               (string/contains? s " "))
73        (let ((inner (substring s 1 (- (string-length s) 1))))
74          (last (string/split inner " ")))
75        s)))
76
77  ;; Errors caught from user code are maps with :message — show just that.
78  (define (error-message err)
79    (if (and (map? err) (contains? err :message))
80      (get err :message)
81      (str err)))
82
83  (define (run-one test-fn)
84    (let ((passed #f)
85          (err nil))
86      (try
87        (begin (test-fn) (set! passed #t))
88        (catch e (set! err e)))
89      {:name (fn-name test-fn) :passed passed :error err}))
90
91  (define (print-result result)
92    (if (get result :passed)
93      (println (format "  ✓ ~a" (get result :name)))
94      (println (format "  ✗ ~a — ~a" (get result :name) (error-message (get result :error))))))
95
96  (define (test/run . test-fns)
97    "Run test functions, print a report, return {:passed :failed :total}."
98    (let* ((results (map run-one test-fns))
99           (passed  (length (filter #(get % :passed) results)))
100           (total   (length results))
101           (failed  (- total passed)))
102      (println "")
103      (for-each print-result results)
104      (println "")
105      (if (= failed 0)
106        (println (format "All ~a tests passed ✓" total))
107        (println (format "~a/~a tests failed ✗" failed total)))
108      {:passed passed :failed failed :total total}))
109
110  (define (test/run! . test-fns)
111    "Run test functions and exit with code 1 if any fail."
112    (let ((summary (apply test/run test-fns)))
113      (when (> (get summary :failed) 0)
114        (exit 1))
115      summary)))