宏中的 RackUnit 源位置

RackUnit source location inside of macros

我正在构建一组机架单元测试,其中实际的 test-case and check-equal? 函数是在宏中定义的。代码看起来像这样:

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

但是,当我 运行 这段代码时,我得到以下输出:

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

其中第 11 行是宏内部 check-equal? 函数的行:(check-equal? case1 case2)))]))

有什么方法可以让 rackunit 在使用 my-test=? 的行上显示错误:(my-test=? 'a 'b)))?

您可以将语法位置直接放在 check-equal? 表达式上以获得您想要的行为。这是一个例子:

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

将语法位置放在外部表达式上通常不会自动传播它。

进行此更改后,我的位置报告为“15:4”(而不是“11:9”),这是 (my-test=? 'a 'b) 表达式出现的位置。