[plt-scheme] check-expect not allowed in expression position?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Wed Mar 31 01:41:36 EDT 2010

I was trying to use test-engine's check-expect to write a test case,
and I found myself wanting to use it in an expression position.  I
came across the error:

    check-expect: found a test that is not at the top level

I understand that, as a mechanism, all the test cases are
syntactically re-adjusted so that they're valid even if they're
referring to definitions defined after the tests.  However, I thought
I might try to work around the limitation with the following macro.
The idea of my macro is to rearrange things enough to make
check-expect happy, but it doesn't work:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax (check-expect* stx)
  (syntax-case stx ()
    [(_ observed expected)
     (with-syntax ([observed-thunk
                    (syntax-local-lift-expression #'#f)]
                   [expected-thunk
                    (syntax-local-lift-expression #'#f)])
       (begin
         (syntax-local-lift-module-end-declaration
          (syntax/loc stx
            (check-expect (observed-thunk) (expected-thunk))))
         (syntax/loc stx
           (begin
             (set! observed-thunk (lambda () observed))
             (set! expected-thunk (lambda () expected))
             (void)))))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


After seeing the error, I now understand that I misused
syntax-local-lift-module-end-declaration, which says that, in the way
I'm using it, eventually the thing being lifted will be expanded in
expression context.  Of course, this isn't what I want: I want to lift
a syntax up to the module or top-level context.  I can't find the
right primitive that does this.  Am I missing something obvious?


Posted on the users mailing list.