10 Expectation Combinators
Due to the structured representation of faults, expectations can be composed and extended in a variety of ways. Several combinators are provided to make it easier to construct complex expectations out of simple ones while preserving error message quality.
procedure
(expect/context exp ctxt) → expectation?
exp : expectation? ctxt : context?
> (struct test-context context () #:transparent) > (define test-exp (expect/context (expect-eq? 'foo) (test-context "test"))) > (expect! 5 test-exp) expected a different value
subject: 5
in: test
expected: eq? to 'foo
actual: 5
procedure
(expect/proc exp proc) → expectation?
exp : expectation? proc : (-> any/c any/c)
> (define first-foo (expect/proc (expect-eq? 'foo) first)) > (expect! '(foo bar) first-foo) > (expect! '(bar foo) first-foo) expected a different value
subject: '(bar foo)
expected: eq? to 'foo
actual: 'bar
procedure
(expect/dependent exp-proc) → expectation?
exp-proc : (-> any/c expectation?)
> (define (last-string-expectation vs) (expect-list-ref (expect-pred string?) (sub1 (length vs)))) > (define expect-last-string (expect/dependent last-string-expectation)) > (expect! '(a b "foo") expect-last-string) > (expect! '(a b c) expect-last-string) expected a different kind of value
subject: '(a b c)
in: item at position 2
expected: string?
actual: 'c
procedure
(expect/singular fault-proc) → expectation?
fault-proc : (-> any/c (or/c fault? #f))
> (struct single-digit-attribute attribute () #:transparent)
> (define (single-digit-fault v) (and (not (<= 0 v 9)) (fault #:summary "a single digit positive integer" #:expected (single-digit-attribute "integer between 0 and 9") #:actual (make-self-attribute v)))) > (define expect-single-digit (expect/singular single-digit-fault)) > (expect! 5 expect-single-digit) > (expect! 123 expect-single-digit) expected a single digit positive integer
subject: 123
expected: integer between 0 and 9
actual: 123
procedure
(expect/around exp around-proc) → expectation?
exp : expectation? around-proc : (-> (-> (listof fault?)) (listof fault?))
> (define (log-num-faults thnk) (define fs (thnk)) (printf "found ~v faults\n" (length fs)) fs)
> (define exp-123/log (expect/around (expect-equal? '(1 2 3)) log-num-faults)) > (expect! '(1 2 3) exp-123/log) found 0 faults
> (expect! '(1 a b) exp-123/log) found 2 faults
multiple failures
subject: '(1 a b)
fault: expected a different value
in: item at position 1
expected: equal? to 2
actual: 'a
fault: expected a different value
in: item at position 2
expected: equal? to 3
actual: 'b