Lei*_*sen 7 macros racket rackunit
我正在构建一组rackunit测试,其中实际test-case和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)
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我得到以下输出:
--------------------
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
Run Code Online (Sandbox Code Playgroud)
其中第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))))]))
Run Code Online (Sandbox Code Playgroud)
将语法位置放在外部表达式上通常不会自动传播它.
通过此更改,我的位置报告为"15:4"(而不是"11:9"),这是(my-test=? 'a 'b)表达式发生的位置.