Skip to content

Commit

Permalink
Improve check-match failure message (#164)
Browse files Browse the repository at this point in the history
* Improve check-match failure message

* Test check-match failure message

* parameterize test-log-enabled? #false
  • Loading branch information
AlexKnauth committed Nov 17, 2023
1 parent eee279e commit fb96819
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 3 additions & 1 deletion rackunit-lib/rackunit/private/check.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@
(syntax->location (quote-syntax #,(datum->syntax #f 'loc stx))))
(make-check-expression '#,(syntax->datum stx))
(make-check-actual actual-val)
(make-check-expected 'expected))
(make-check-info 'pattern 'expected)
#,@(cond [(eq? (syntax-e #'pred) #t) '()]
[else #'((make-check-info 'condition 'pred))]))
(lambda ()
(check-true (match actual-val
[expected pred]
Expand Down
45 changes: 43 additions & 2 deletions rackunit-test/tests/rackunit/check-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
rackunit
rackunit/private/check
rackunit/private/result
rackunit/private/test-suite)
rackunit/private/test-suite
(only-in rackunit/log test-log-enabled?)
(only-in rackunit/text-ui run-tests)
(only-in rackunit/private/check-info current-check-info))

(define (make-failure-test name pred . args)
(test-case
Expand Down Expand Up @@ -138,7 +141,45 @@
(check-match (data 1 2 (data 1 2 3))
(data _ _ (data x y z))
(equal? (+ x y z) 6))))


(test-case "check-match failure message"
(define-check (check-failure-message rx thunk)
(define actual
(call-with-output-string
(lambda (e)
(parameterize ([current-error-port e]
[current-check-info '()]
[test-log-enabled? #false])
(run-tests (test-suite "check-failure-message" (thunk)))))))
(with-check-info*
(list (make-check-info 'actual (string-info actual)))
(lambda () (check-regexp-match rx actual))))
(define (rx . strs) (regexp (apply string-append strs)))
(check-failure-message
(rx
"name: *check-match\n.*"
"actual: *1\n"
"pattern: *a\n"
"condition: *" (regexp-quote "(symbol? a)"))
(lambda ()
(check-match 1 a (symbol? a))))
(check-failure-message
(rx
"name: *check-match\n.*"
"actual: *" (regexp-quote "'(1)") "\n"
"pattern: *" (regexp-quote "(quasiquote ((unquote a)))") "\n"
"condition: *" (regexp-quote "(symbol? a)"))
(lambda ()
(check-match `(1) `(,a) (symbol? a))))
(check-failure-message
(rx
"name: *check-match\n.*"
"actual: *" (regexp-quote "'a") "\n"
"pattern: *" (regexp-quote "a") "\n"
"condition: *" (regexp-quote "(integer? a)"))
(lambda ()
(check-match 'a a (integer? a)))))

;; Failures
(make-failure-test "check-equal? failure"
check-equal? 1 2)
Expand Down

0 comments on commit fb96819

Please sign in to comment.