Added test suite tools
This commit is contained in:
parent
6236f19ae6
commit
e310d9ce21
37
d-/test.scm
Normal file
37
d-/test.scm
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
(define-module (d- test)
|
||||||
|
#:use-module (ice-9 format)
|
||||||
|
#:use-module (ice-9 control)
|
||||||
|
#:export (define-test-suite assert))
|
||||||
|
|
||||||
|
(define (assert expected actual eq? fail)
|
||||||
|
(when (not (eq? expected actual))
|
||||||
|
(format #t "fail - expected ~a but got ~a" expected actual)
|
||||||
|
(newline)
|
||||||
|
(fail)))
|
||||||
|
|
||||||
|
(define-syntax define-test-suite
|
||||||
|
(lambda (stx)
|
||||||
|
(with-syntax ([fail (datum->syntax stx 'fail)])
|
||||||
|
(define (assert-stx expr)
|
||||||
|
(syntax-case expr (assert-equal assert-not-equal)
|
||||||
|
[(assert-equal e a) #'(assert e a equal? fail)]
|
||||||
|
[(assert-not-equal e a) #'(assert e a (compose not equal?) fail)]
|
||||||
|
[(assert-unspecified v) #'(assert (when #f 'a) v eq? fail)]
|
||||||
|
[else #'else]))
|
||||||
|
(define (define-test-stx test)
|
||||||
|
(syntax-case test (define-test)
|
||||||
|
[(define-test test-name e ...)
|
||||||
|
#`(call/ec (lambda (fail)
|
||||||
|
(format #t "~a: " test-name)
|
||||||
|
(begin
|
||||||
|
#,@(map assert-stx #'(e ...)))
|
||||||
|
(display "ok")
|
||||||
|
(newline)))]))
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(define-test-suite name test ...)
|
||||||
|
#`(begin
|
||||||
|
(format #t "Suite ~a:" name)
|
||||||
|
(newline)
|
||||||
|
#,@(map define-test-stx
|
||||||
|
#'(test ...))
|
||||||
|
(newline))]))))
|
140
test.scm
140
test.scm
@ -1,98 +1,64 @@
|
|||||||
(use-modules (d-)
|
(use-modules (d-)
|
||||||
(rnrs base)
|
(d- test)
|
||||||
(srfi srfi-1))
|
(srfi srfi-1))
|
||||||
|
|
||||||
;; TODO eventually these will be cool prompt thingies
|
(define-test-suite "~>"
|
||||||
|
(define-test "unwrapped"
|
||||||
|
(define value (~> 1 1+ 1+))
|
||||||
|
(assert-equal 3 value))
|
||||||
|
(define-test "wrapped"
|
||||||
|
(define value (~> 1 (/ 2) (/ 2)))
|
||||||
|
(assert-equal 1/4 value)))
|
||||||
|
|
||||||
(define (assert-equal a b)
|
(define-test-suite "~>>"
|
||||||
(display "... ")
|
(define-test "unwrapped"
|
||||||
(if (equal? a b)
|
(define value (~>> 1 1+ 1+))
|
||||||
(display "OK")
|
(assert-equal 3 value))
|
||||||
(begin
|
(define-test "wrapped"
|
||||||
(display "expected ")
|
(define value (~>> 1 (/ 2) (/ 2)))
|
||||||
(display b)
|
(assert-equal 1 value)))
|
||||||
(display " but instead got ")
|
|
||||||
(display a))))
|
|
||||||
|
|
||||||
(define (assert-unspecified a)
|
(define-test-suite "if-not"
|
||||||
(display "... ")
|
(define-test "test"
|
||||||
(if (unspecified? a)
|
(assert-equal 'a (if-not #f 'a 'b))))
|
||||||
(display "OK")
|
|
||||||
(begin
|
|
||||||
(display "expected unspecifed but instead got ")
|
|
||||||
(display a))))
|
|
||||||
|
|
||||||
(define-syntax-rule (define-unit-test (test-name body ...))
|
(define-test-suite "when-not"
|
||||||
(let ()
|
(define-test "positive-case"
|
||||||
(display "< ")
|
(assert-equal 'return (when-not #f 'do-some-stuff 'return)))
|
||||||
(display (symbol->string 'test-name))
|
(define-test "negative-case"
|
||||||
body ...
|
(assert-unspecified (when-not #t 'do-some-stuff 'return))))
|
||||||
(newline)))
|
|
||||||
|
|
||||||
(define-syntax-rule (define-test module-name tests ...)
|
(define-test-suite "for"
|
||||||
(begin
|
(define-test "permutation"
|
||||||
(display "> ")
|
(define value (for ([i (iota 2)]
|
||||||
(display (symbol->string 'module-name))
|
[j (iota 2)])
|
||||||
(newline)
|
(cons i j)))
|
||||||
(define-unit-test tests) ...))
|
(assert-equal '((0 . 0) (0 . 1) (1 . 0) (1 . 1)) value)))
|
||||||
|
|
||||||
(define-test ~>
|
(define-test-suite "if-let"
|
||||||
(unwrapped
|
(define-test "positive-case"
|
||||||
(define value (~> 1 1+ 1+))
|
(assert-equal 2 (if-let ([v (or #f 1)]) (+ v 1))))
|
||||||
(assert-equal value 3))
|
(define-test "negative-case"
|
||||||
(wrapped
|
(assert-equal #f (if-let ([v (and #f 1)]) (+ v 1)))))
|
||||||
(define value (~> 1 (/ 2) (/ 2)))
|
|
||||||
(assert-equal value 1/4)))
|
|
||||||
|
|
||||||
(define-test ~>>
|
(define-test-suite "partial"
|
||||||
(unwrapped
|
(define-test "test"
|
||||||
(define value (~>> 1 1+ 1+))
|
(define value (partial / 2))
|
||||||
(assert-equal value 3))
|
(assert-equal 1/2 (value 4))
|
||||||
(wrapped
|
(assert-equal 2 (value 1))))
|
||||||
(define value (~>> 1 (/ 2) (/ 2)))
|
|
||||||
(assert-equal value 1)))
|
|
||||||
|
|
||||||
(define-test if-not
|
(define-test-suite "argmin"
|
||||||
(test
|
(define-test "test"
|
||||||
(assert-equal (if-not #f 'a 'b) 'a)))
|
(assert-equal '(b . 2) (argmin cdr < '(a . 5) '(b . 2) '(c . 3)))))
|
||||||
|
|
||||||
(define-test when-not
|
(define-test-suite "generator"
|
||||||
(positive-case
|
(define-test "test"
|
||||||
(assert-equal (when-not #f 'do-some-stuff 'return) 'return))
|
(define number-generator
|
||||||
(negative-case
|
(generator
|
||||||
(assert-unspecified (when-not #t 'do-some-stuff 'return))))
|
(let loop ([i 1])
|
||||||
|
(yield i)
|
||||||
(define-test for
|
(loop (1+ i)))))
|
||||||
(permutation
|
(assert-equal 1 (number-generator))
|
||||||
(define value (for ([i (iota 2)]
|
(assert-equal 2 (number-generator))
|
||||||
[j (iota 2)])
|
(assert-equal 3 (number-generator))
|
||||||
(cons i j)))
|
(assert-equal 4 (number-generator))))
|
||||||
(assert-equal value '((0 . 0) (0 . 1) (1 . 0) (1 . 1)))))
|
|
||||||
|
|
||||||
(define-test if-let
|
|
||||||
(positive-case
|
|
||||||
(assert-equal (if-let ([v (or #f 1)]) (+ v 1)) 2))
|
|
||||||
(negative-case
|
|
||||||
(assert-equal (if-let ([v (and #f 1)]) (+ v 1)) #f)))
|
|
||||||
|
|
||||||
(define-test partial
|
|
||||||
(test
|
|
||||||
(define value (partial / 2))
|
|
||||||
(assert-equal (value 4) 1/2)
|
|
||||||
(assert-equal (value 1) 2)))
|
|
||||||
|
|
||||||
(define-test argmin
|
|
||||||
(test
|
|
||||||
(assert-equal (argmin cdr < '(a . 5) '(b . 2) '(c . 3)) '(b . 2))))
|
|
||||||
|
|
||||||
(define-test generator
|
|
||||||
(test
|
|
||||||
(define number-generator
|
|
||||||
(generator
|
|
||||||
(let loop ([i 1])
|
|
||||||
(yield i)
|
|
||||||
(loop (1+ i)))))
|
|
||||||
(assert-equal (number-generator) 1)
|
|
||||||
(assert-equal (number-generator) 2)
|
|
||||||
(assert-equal (number-generator) 3)
|
|
||||||
(assert-equal (number-generator) 4)))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user