factor/basis/tools/test/test.factor

148 lines
3.9 KiB
Factor
Raw Normal View History

2009-04-09 05:50:47 -04:00
! Copyright (C) 2003, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2009-04-09 05:50:47 -04:00
USING: accessors arrays assocs combinators compiler.units
continuations debugger effects fry generalizations io io.files
io.styles kernel lexer locals macros math.parser namespaces
parser prettyprint quotations sequences source-files splitting
stack-checker summary unicode.case vectors vocabs vocabs.loader words
tools.vocabs tools.errors source-files.errors io.streams.string make ;
2007-09-20 18:09:08 -04:00
IN: tools.test
2009-04-09 05:50:47 -04:00
TUPLE: test-failure < source-file-error experiment continuation ;
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
SYMBOL: passed-tests
SYMBOL: failed-tests
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
<PRIVATE
: <test-failure> ( error experiment file line# -- triple )
test-failure new
swap >>line#
swap >>file
swap >>experiment
swap >>error
error-continuation get >>continuation ;
: failure ( error experiment file line# -- )
2008-10-20 22:07:46 -04:00
"--> test failed!" print
2009-04-09 05:50:47 -04:00
<test-failure> failed-tests get push ;
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
: success ( experiment -- ) passed-tests get push ;
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
: file-failure ( error file -- )
[ f ] [ f ] bi* failure ;
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
:: (unit-test) ( output input -- error ? )
[ { } input with-datastack output assert-sequence= f f ] [ t ] recover ; inline
2007-09-20 18:09:08 -04:00
: short-effect ( effect -- pair )
2008-07-28 18:54:10 -04:00
[ in>> length ] [ out>> length ] bi 2array ;
2009-04-09 05:50:47 -04:00
:: (must-infer-as) ( effect quot -- error ? )
[ quot infer short-effect effect assert= f f ] [ t ] recover ; inline
:: (must-infer) ( word/quot -- error ? )
word/quot dup word? [ '[ _ execute ] ] when :> quot
[ quot infer drop f f ] [ t ] recover ; inline
2009-04-09 09:17:41 -04:00
TUPLE: did-not-fail ;
CONSTANT: did-not-fail T{ did-not-fail }
2009-04-09 05:50:47 -04:00
M: did-not-fail summary drop "Did not fail" ;
:: (must-fail-with) ( quot pred -- error ? )
[ quot call did-not-fail t ]
[ dup pred call [ drop f f ] [ t ] if ] recover ; inline
:: (must-fail) ( quot -- error ? )
[ quot call did-not-fail t ] [ drop f f ] recover ; inline
: experiment-title ( word -- string )
"(" ?head drop ")" ?tail drop { { CHAR: - CHAR: \s } } substitute >title ;
MACRO: <experiment> ( word -- )
[ stack-effect in>> length dup ]
[ name>> experiment-title ] bi
'[ _ ndup _ narray _ prefix ] ;
: experiment. ( seq -- )
[ first write ": " write ] [ rest . ] bi ;
2009-04-09 05:50:47 -04:00
:: experiment ( word: ( -- error ? ) file line# -- )
word <experiment> :> e
e experiment.
word execute [ e file line# failure ] [ drop e success ] if ; inline
2009-04-09 05:50:47 -04:00
: parse-test ( accum word -- accum )
literalize parsed
file get dup [ path>> ] when parsed
lexer get line>> parsed
\ experiment parsed ; inline
2008-02-06 14:15:15 -05:00
2009-04-09 05:50:47 -04:00
<<
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
SYNTAX: TEST:
scan
[ create-in ]
[ "(" ")" surround search '[ _ parse-test ] ] bi
define-syntax ;
>>
: run-test-file ( path -- )
[ run-file ] [ swap file-failure ] recover ;
: collect-results ( quot -- failed passed )
[
V{ } clone failed-tests set
V{ } clone passed-tests set
call
failed-tests get
passed-tests get
] with-scope ; inline
: run-vocab-tests ( vocab -- )
dup vocab source-loaded?>> [
2009-04-09 05:50:47 -04:00
vocab-tests [ run-test-file ] each
2008-03-01 17:00:45 -05:00
] [ drop ] if ;
2009-04-09 05:50:47 -04:00
: traceback-button. ( failure -- )
"[" write [ "Traceback" ] dip continuation>> write-object "]" print ;
PRIVATE>
TEST: unit-test
TEST: must-infer-as
TEST: must-infer
TEST: must-fail-with
TEST: must-fail
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
M: test-failure summary
[ experiment>> experiment. ] with-string-writer ;
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
M: test-failure error. ( error -- )
[ call-next-method ]
[ traceback-button. ]
bi ;
: results. ( failed passed -- )
2008-02-08 02:49:05 -05:00
[
2008-09-06 20:13:59 -04:00
[
2009-04-09 05:50:47 -04:00
[ length # " tests failed, " % ]
[ length # " tests passed." % ]
bi*
2009-04-09 09:17:41 -04:00
] "" make nl print nl
2009-04-09 05:50:47 -04:00
] [ drop errors. ] 2bi ;
: run-tests ( prefix -- failed passed )
[ child-vocabs [ run-vocab-tests ] each ] collect-results ;
2007-09-20 18:09:08 -04:00
2008-02-06 22:15:33 -05:00
: test ( prefix -- )
2009-04-09 05:50:47 -04:00
run-tests results. ;
2008-02-06 22:15:33 -05:00
2009-04-09 05:50:47 -04:00
: run-all-tests ( -- failed passed )
2008-02-06 22:15:33 -05:00
"" run-tests ;
2007-09-20 18:09:08 -04:00
2008-02-06 22:15:33 -05:00
: test-all ( -- )
2009-04-09 05:50:47 -04:00
run-all-tests results. ;