Paste: expectations version 1
Author: | randy7 |
Mode: | factor |
Date: | Sat, 21 Feb 2009 17:22:42 |
Plain Text |
USING: namespaces kernel sequences combinators io math arrays
generalizations combinators.short-circuit ;
IN: expectations
SINGLETONS: +int+ +float+ +seq+ +pair+ +1pair-seq+ +hash+ +vector+ ;
ERROR: expectation-failed item expected ;
ERROR: no-such-singleton singleton ;
SYMBOL: expect?
expect? [ f ] initialize
: expect-result ( singleton item ? -- )
[
2drop
] [ expectation-failed ] if ;
: just-one ( seq -- ? )
length 1 = ;
: int-def ( x -- ? ) integer? ; inline
: pair-def ( x -- ? )
{ [ sequence? ]
[ array? ]
[ length 2 = ]
[ [ integer? ] all? ]
} 1&& ; inline
: 1pair-seq-def ( x -- ? ) { [ sequence? ] [ just-one ] [ [ pair-def ] all? ] } 1&& ;
: pairs-seq-def ( x -- ? ) { [ sequence? ] [ [ pair-def ] all? ] } 1&& ;
: seq-def ( x -- ? )
sequence? ; inline
: float-def ( x -- ? ) float? ; inline
: do-expect ( item expected -- )
2dup {
{ +int+ [ int-def ] }
{ +pair+ [ pair-def ] }
{ +seq+ [ seq-def ] }
{ +float+ [ float-def ] }
[ dup "no such singleton, please create one and its definition, and add to the case statement." print
no-such-singleton ]
} case expect-result ;
: expect ( item expected -- )
expect? get-global
[ do-expect ] [ 2drop ] if ;
: expecting ( objs... singletons-seq -- )
[ [ expect ] curry ] map
dup length [ ndup ] curry dip
spread ;
Author: | me |
Mode: | factor |
Date: | Sat, 21 Feb 2009 22:58:59 |
Plain Text |
note to self: use predicates
it's still useful. predicates will be the definitions, and the way to state what kind the item is.
throw singletons away.
they can be built from any code.
perhaps there is a list of all predicates, so the case statement of do-expect will potentially be built automatically.
predicate, and predicate ask word. { pair [ pair? ] }
etc.
.... get acquainted with the introspection features to generate or get something like this. maybe it's a simple 'instance?>>'
New Annotation