factor/basis/tools/test/test.factor

132 lines
3.6 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
compiler.errors ;
2007-09-20 18:09:08 -04:00
IN: tools.test
TUPLE: test-failure < source-file-error continuation ;
2007-09-20 18:09:08 -04:00
SYMBOL: +test-failure+
M: test-failure source-file-error-type drop +test-failure+ ;
SYMBOL: test-failures
test-failures [ V{ } clone ] initialize
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 >>asset
2009-04-09 05:50:47 -04:00
swap >>error
error-continuation get >>continuation ;
: failure ( error experiment file line# -- )
2008-10-20 22:07:46 -04:00
"--> test failed!" print
<test-failure> test-failures 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 ] 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 -- )
[ [ test-failures get ] dip '[ file>> _ = not ] filter-here ]
[ [ run-file ] [ swap file-failure ] recover ] bi ;
2009-04-09 05:50:47 -04:00
: 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
[ asset>> 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 ;
: :failures ( -- ) test-failures get errors. ;
2007-09-20 18:09:08 -04:00
2008-02-06 22:15:33 -05:00
: test ( prefix -- )
[ child-vocabs [ run-vocab-tests ] each ] with-compiler-errors
test-failures get [
":failures - show " write length pprint " failing tests." print
] unless-empty ;
2007-09-20 18:09:08 -04:00
: test-all ( -- ) "" test ;