macrosracketrackunit

RackUnit source location inside of macros


I am building a set of rackunit tests, where the actual test-case and check-equal? function is defined in a macro. The code looks something like this:

#lang racket

(require rackunit
         rackunit/text-ui)

(define-syntax (my-test=? stx)
  (syntax-case stx ()
    [(_ case1 case2)
     (syntax/loc stx
       (test-case "tests"
         (check-equal? case1 case2)))]))

(define tests
  (test-suite "tests"
    (my-test=? 'a 'b)))

(run-tests tests)

However, when I run this code I get the following output:

--------------------
tests > tests
tests
FAILURE
name:       check-equal?
location:   unsaved-editor:11:9
actual:     'a
expected:   'b
. Check failure
--------------------
0 success(es) 1 failure(s) 0 error(s) 1 test(s) run

Where line 11 is the line of the check-equal? function inside of the macro: (check-equal? case1 case2)))]))

Is there any way I can rackunit to show the error on the line where my-test=? is used: (my-test=? 'a 'b)))?


Solution

  • You can put the syntax location directly on the check-equal? expression to get the behavior that you want. Here's an example:

    (define-syntax (my-test=? stx)
      (syntax-case stx ()
        [(_ case1 case2)
         (quasisyntax
           (test-case "tests"
             #,(syntax/loc stx (check-equal? case1 case2))))]))
    

    Putting the syntax location on the outer expression doesn't automatically propagate it in general.

    With this change, the location is reported as "15:4" for me (as opposed to "11:9") which is where the (my-test=? 'a 'b) expression occurs.