factor/basis/tools/test/test.factor

177 lines
4.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs combinators command-line
compiler.units continuations debugger effects fry
generalizations io kernel lexer locals macros namespaces parser
prettyprint quotations sequences sequences.generalizations
source-files source-files.errors source-files.errors.debugger
splitting stack-checker summary tools.errors unicode.case vocabs
vocabs.files vocabs.metadata vocabs.parser words ;
FROM: vocabs.hierarchy => load ;
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 error-type drop +test-failure+ ;
SYMBOL: test-failures
test-failures [ V{ } clone ] initialize
2007-09-20 18:09:08 -04:00
2013-03-24 01:06:27 -04:00
T{ error-type-holder
{ type +test-failure+ }
{ word ":test-failures" }
{ plural "unit test failures" }
{ icon "vocab:ui/tools/error-list/icons/unit-test-error.tiff" }
{ quot [ test-failures get ] }
} define-error-type
SYMBOL: verbose-tests?
t verbose-tests? set-global
2009-04-09 05:50:47 -04:00
<PRIVATE
: <test-failure> ( error experiment file line# -- triple )
test-failure new
swap >>line#
swap >>path
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
2009-04-13 15:40:03 -04:00
<test-failure> test-failures get push
notify-error-observers ;
2007-09-20 18:09:08 -04:00
SYMBOL: current-test-file
: file-failure ( error -- )
[ f current-test-file get ] keep error-line failure ;
2007-09-20 18:09:08 -04:00
2009-04-09 05:50:47 -04:00
:: (unit-test) ( output input -- error ? )
2009-04-19 19:21:25 -04:00
[ { } input with-datastack output assert-sequence= f f ] [ t ] recover ;
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 ? )
2009-04-19 19:21:25 -04:00
[ quot infer short-effect effect assert= f f ] [ t ] recover ;
2009-04-09 05:50:47 -04:00
:: (must-infer) ( quot -- error ? )
2009-04-19 19:21:25 -04:00
[ quot infer drop f f ] [ t ] recover ;
2009-04-09 05:50:47 -04:00
2009-04-09 09:17:41 -04:00
TUPLE: did-not-fail ;
2013-03-23 20:09:19 -04:00
CONSTANT: did-not-fail-literal 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 ? )
2013-03-23 20:09:19 -04:00
[ { } quot with-datastack drop did-not-fail-literal t ]
2009-04-19 19:21:25 -04:00
[ dup pred call( error -- ? ) [ drop f f ] [ t ] if ] recover ;
2009-04-09 05:50:47 -04:00
:: (must-fail) ( quot -- error ? )
2013-03-23 20:09:19 -04:00
[ { } quot with-datastack drop did-not-fail-literal t ] [ drop f f ] recover ;
2009-04-09 05:50:47 -04:00
: experiment-title ( word -- string )
"(" ?head drop ")" ?tail drop
H{ { CHAR: - CHAR: \s } } substitute >title ;
2009-04-09 05:50:47 -04:00
MACRO: <experiment> ( word -- quot )
2009-04-09 05:50:47 -04:00
[ stack-effect in>> length dup ]
[ name>> experiment-title ] bi
'[ _ ndup _ narray _ prefix ] ;
: experiment. ( seq -- )
[ first write ": " write ]
[ rest verbose-tests? get [ . ] [ short. ] if flush ] bi ;
:: experiment ( word: ( -- error ? ) line# -- )
2009-04-09 05:50:47 -04:00
word <experiment> :> e
e experiment.
word execute [
current-test-file get [
e current-test-file get line# failure
] [ rethrow ] if
] [ drop ] if ; inline
2009-04-09 05:50:47 -04:00
: parse-test ( accum word -- accum )
2009-10-28 14:38:27 -04:00
literalize suffix!
lexer get line>> suffix!
\ experiment suffix! ; 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-token
[ create-word-in ]
2009-04-09 05:50:47 -04:00
[ "(" ")" surround search '[ _ parse-test ] ] bi
define-syntax ;
>>
: fake-unit-test ( quot -- test-failures )
[
"fake" current-test-file set
V{ } clone test-failures set
call
test-failures get
] with-scope ; inline
PRIVATE>
2009-04-09 05:50:47 -04:00
: run-test-file ( path -- )
dup current-test-file [
test-failures get current-test-file get +test-failure+ delete-file-errors
'[ _ run-file ] [ file-failure ] recover
] with-variable ;
2009-04-09 05:50:47 -04:00
SYMBOL: forget-tests?
<PRIVATE
: forget-tests ( files -- )
forget-tests? get
[ [ [ forget-source ] each ] with-compilation-unit ] [ drop ] if ;
: test-vocab ( vocab -- )
2011-10-24 06:37:47 -04:00
lookup-vocab dup [
dup source-loaded?>> [
vocab-tests
[ [ run-test-file ] each ]
[ forget-tests ]
bi
] [ drop ] if
2008-03-01 17:00:45 -05:00
] [ drop ] if ;
: test-vocabs ( vocabs -- ) [ test-vocab ] each ;
2009-04-09 05:50:47 -04:00
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 error. ( error -- )
2009-04-17 16:50:11 -04:00
{
[ error-location print nl ]
2009-04-17 16:50:11 -04:00
[ asset>> [ experiment. nl ] when* ]
[ error>> error. ]
[ continuation>> traceback-link. ]
2009-04-17 16:50:11 -04:00
} cleave ;
2009-04-09 05:50:47 -04:00
: :test-failures ( -- ) test-failures get errors. ;
2007-09-20 18:09:08 -04:00
: test ( prefix -- ) loaded-child-vocab-names test-vocabs ;
2007-09-20 18:09:08 -04:00
: test-all ( -- ) loaded-vocab-names filter-don't-test test-vocabs ;
: test-main ( -- )
command-line get [ [ load ] [ test ] bi ] each ;
MAIN: test-main