15 Macro Testing

▼ |  | Syntax: Meta-Programming Helpers

1 | Parsing and Specifying Syntax
2 | Syntax Object Helpers
3 | Datum Pattern Matching
4 | Module-Processing Helpers
5 | Macro Transformer Helpers
6 | Reader Helpers
7 | Parsing for Bodies
8 | Unsafe for Clause Transforms
9 | Source Locations
10 | Preserving Source Locations
11 | Non-Module Compilation And Expansion
12 | Trusting Standard Recertifying Transformers
13 | Attaching Documentation to Exports
14 | Contracts for Macro Subexpressions
15 | Macro Testing
16 | Internal-Definition Context Helpers
| Index

On this page:

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

8.18
top contents ← prev up next →

## 15 Macro Testing🔗ℹ

(require syntax/macro-testing) | package: base

> 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? (point-y point-x) (#f #f) #t)>

Added in version 6.3 of package base.

> 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
> name: check-exn
> location: eval:6:0
> params: '(#rx"missing formals and body" #<procedure>)
> message: "Wrong exception raised"
> exn-message: "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:eval:6:0 (lambda)>))
> --------------------

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

Added in version 6.3 of package base.

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

Added in version 6.3 of package base.

top contents ← prev up next →