▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Filesystem
10 | Find
11 | Flonums
12 | Futures
13 | Functions
14 | Hash Tables
15 | Interface-Oriented Programming for Classes
16 | Lazy Require
17 | Lists
18 | Logging
19 | Macro Testing
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Options
24 | Parameter Groups
25 | Pretty-Printing
26 | Re-Contracting Identifiers
27 | Sandbox
28 | Sequences
29 | Strings
30 | Structs
31 | Struct Printing
32 | Syntax
33 | Temporal Contracts: Explicit Contract Monitors
34 | Unix Domain Sockets
35 | 2D Syntax
36 | GUI Libraries

On this page:

phase1-eval
convert-compile-time-error
convert-syntax-error

5.3.6
top ← prev up next →

### 19`` Macro Testing

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

(require unstable/macro-testing)

> > syntax

> > (phase1-eval ct-expr maybe-quote maybe-catch?)

> maybe-quote

> =

> |  | | |  | #:quote quote-id

> maybe-catch?

> =

> |  | | |  | #:catch? catch?

Evaluates ct-expr at compile time and quotes the result using quote-id, which defaults to quote. Another suitable argument for quote-id is quote-syntax.

If catch? is #t, then if the evaluation of ct-expr raises a compile-time exception, it is caught and converted to a run-time exception.

Examples:

> > (struct point (x y))

> > (phase1-eval (extract-struct-info (syntax-local-value #'point)))
> '(struct:point point point? (point-y point-x) (#f #f) #t)

> > (phase1-eval (extract-struct-info (syntax-local-value #'point))
> #:quote quote-syntax)

> #<syntax (struct:point point point? (p...>

> > syntax

> > (convert-compile-time-error expr)

Equivalent to (#%expression expr) except if expansion of expr causes a compile-time exception to be raised; in that case, the compile-time exception is converted to a run-time exception raised when the expression is evaluated.

Use convert-compile-time-error to write tests for compile-time error checking like syntax errors:

Examples:

> > (check-exn #rx"missing an \"else\" expression"
> (lambda () (convert-compile-time-error (if 1 2))))

> > (check-exn #rx"missing formals and body"
> (lambda () (convert-compile-time-error (lambda))))

> --------------------
> FAILURE
> message: "Wrong exception raised"
> "eval:6:0: lambda: bad syntax\n in: (lambda)"
> exn: #(struct:exn:fail:syntax "eval:6:0: lambda: bad syntax\n in: (lambda)" #<continuation-mark-set> (#<syntax:6:0 (lambda)>))
> name: check-exn
> location: (eval 6 0 6 1)
> expression: (check-exn #rx"missing formals and body" (lambda () (convert-compile-time-error (lambda))))
> params: (#rx"missing formals and body" #<procedure:temp10>)
> Check failure
> --------------------

Without the use of convert-compile-time-error, the checks above would not be executed because the test program would not compile.

> > syntax

> > (convert-syntax-error expr)

Like convert-compile-time-error, but only catches compile-time exn:fail:syntax? exceptions and sets error-print-source-location to #f around the expansion of expr to make the message easier to match exactly.

Example:

> > (check-exn #rx"^lambda: bad syntax$"
> (lambda () (convert-syntax-error (lambda))))

top ← prev up next →