diff --git a/pkgs/rackunit-pkgs/rackunit-lib/rackunit/private/check.rkt b/pkgs/rackunit-pkgs/rackunit-lib/rackunit/private/check.rkt index 191de1990f..8b9539751e 100644 --- a/pkgs/rackunit-pkgs/rackunit-lib/rackunit/private/check.rkt +++ b/pkgs/rackunit-pkgs/rackunit-lib/rackunit/private/check.rkt @@ -205,7 +205,13 @@ #t (fail-check)))))])) +(define (raise-error-if-not-thunk name thunk) + (unless (and (procedure? thunk) + (procedure-arity-includes? thunk 0)) + (raise-arguments-error name "thunk must be a procedure that accepts 0 arguments" "thunk" thunk))) + (define-check (check-exn raw-pred thunk) + (raise-error-if-not-thunk 'check-exn thunk) (let ([pred (if (regexp? raw-pred) (λ (x) (and (exn:fail? x) (regexp-match raw-pred (exn-message x)))) raw-pred)]) @@ -236,6 +242,7 @@ (lambda () (fail-check)))))) (define-check (check-not-exn thunk) + (raise-error-if-not-thunk 'check-not-exn thunk) (with-handlers ([exn:test:check? (lambda (exn) (refail-check exn))] diff --git a/pkgs/rackunit-pkgs/rackunit-test/tests/rackunit/check-test.rkt b/pkgs/rackunit-pkgs/rackunit-test/tests/rackunit/check-test.rkt index f790a6bbaa..8e7be6e82e 100644 --- a/pkgs/rackunit-pkgs/rackunit-test/tests/rackunit/check-test.rkt +++ b/pkgs/rackunit-pkgs/rackunit-test/tests/rackunit/check-test.rkt @@ -318,6 +318,32 @@ found?)) #f names)))) + ;; Verify that check-exn and check-not-exn raise errors (not check + ;; failures) if not given thunks. + (test-case + "check-exn raises contract exception if not given a procedure" + (check-exn exn:fail:contract? + (lambda () + (check-exn exn:fail? 'not-a-procedure)))) + + (test-case + "check-exn raises contract exception if given a procedure with incorrect arity" + (check-exn exn:fail:contract? + (lambda () + (check-exn exn:fail? (lambda (x) x))))) + + (test-case + "check-not-exn raises contract exception if not given a procedure" + (check-exn exn:fail:contract? + (lambda () + (check-not-exn 'not-a-procedure)))) + + (test-case + "check-not-exn raises contract exception if given a procedure with incorrect arity" + (check-exn exn:fail:contract? + (lambda () + (check-not-exn (lambda (x) x))))) + ;; Regression test ;; Uses of check (and derived forms) used to be un-compilable! ;; We check that (write (compile --code-using-check--)) works.