-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.rkt
59 lines (49 loc) · 2.02 KB
/
base.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#lang racket/base
(require racket/contract/base)
(provide
define-fixture
(contract-out
[fixture (->* (symbol? disposable?)
(#:info-proc (-> any/c any/c))
fixture?)]
[fixture? predicate/c]
[fixture-initialized? (-> fixture? boolean?)]
[fixture-name (-> fixture? symbol?)]
[fixture-value (-> (and/c fixture? fixture-initialized?) any/c)]
[fixture-info (-> (and/c fixture? fixture-initialized?) any/c)]
[call/fixture (-> fixture? (-> any) any)]))
(require (for-syntax racket/base
racket/syntax)
disposable
syntax/parse/define)
(struct fixture (name disp info-proc param init-param)
#:constructor-name make-fixture
#:omit-define-syntaxes)
(define (fixture name disp #:info-proc [info-proc values])
(make-fixture name disp info-proc (make-parameter #f) (make-parameter #f)))
(define (fixture-initialized? fix) ((fixture-init-param fix)))
(define (fixture-value fix) ((fixture-param fix)))
(define (fixture-info fix) ((fixture-info-proc fix) (fixture-value fix)))
(define (call/fixture fix thnk)
(with-disposable ([v (fixture-disp fix)])
(parameterize ([(fixture-param fix) v] [(fixture-init-param fix) #t])
(thnk))))
(begin-for-syntax
(define (format-accessor id-stx)
(format-id id-stx "current-~a" (syntax-e id-stx) #:source id-stx)))
(define-simple-macro
(define-fixture id:id disp
(~alt (~optional (~seq #:info-proc info-proc)
#:defaults ([info-proc.c #'values]))
(~optional (~seq #:accessor-name accessor-id)
#:defaults ([accessor-id (format-accessor #'id)])))
...)
#:declare disp (expr/c #'disposable? #:name "disposable argument")
#:declare info-proc (expr/c #'(-> any/c any/c) #:name "info-proc argument")
(begin
(define id (fixture 'id disp.c #:info-proc info-proc.c))
(define (accessor-id)
(unless (fixture-initialized? id)
(raise-argument-error 'accessor-id "fixture not initialized"
"fixture" id))
(fixture-value id))))