RackUnit source location inside of macros
Asked Answered
S

1

7

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)))?

Sandarac answered 26/8, 2015 at 18:10 Comment(0)
E
6

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.

Encage answered 26/8, 2015 at 19:33 Comment(1)
Yes, this has tripped me up before. It turns out the phrase "immediate syntax object" in the documentation is significant!Watercourse

© 2022 - 2024 — McMap. All rights reserved.