7 Structure Expectations
syntax
(expect-struct id [accessor-id expect-expr] ...)
expect-expr : any/c
The id must have a transformer binding to a struct-info? value, and that value must supply the structure type’s predicate. Accessors of the struct’s supertypes are allowed. Faults found by the expectation in accessed fields have a struct-accessor-context value added to their contexts.
> (struct fish (color weight) #:transparent) > (expect! (fish 'red 5) (expect-struct fish [fish-color 'blue])) expected a different value
subject: (fish 'red 5)
in: the fish-color struct field
expected: equal? to 'blue
actual: 'red
syntax
(define-struct-expectation struct-maybe-id)
struct-maybe-id = struct-id | (id struct-id)
Like expect-struct, struct-id must have a transformer binding to a struct-info?, which is inspected by define-struct-expectation to determine what accessors to generate keyword arguments for. All accessors must be of the pattern struct-id-field-id (such as fish-color) or an "ambiguous keyword form" syntax error is reported. For accessors matching this pattern, the corresponding keyword used by the bound procedure is the symbol 'field-id converted to a keyword.
> (struct fish (color weight) #:transparent) > (define-struct-expectation fish) > (expect! (fish 'red 5) (expect-fish #:weight 20)) expected a different value
subject: (fish 'red 5)
in: the fish-weight struct field
expected: equal? to 20
actual: 5
struct
(struct struct-accessor-context context (accessor-id) #:transparent) accessor-id : identifier?
procedure
(make-struct-accessor-context accessor-id)
→ struct-field-context? accessor-id : identifier?
> (make-struct-accessor-context #'shape-area)
(struct-accessor-context
"the shape-area struct field"
#<syntax:eval:1:0 shape-area>)