factor/basis/tools/test/test.factor

218 lines
6.0 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 io.files.temp io.files.unique kernel lexer
locals macros math.functions math.vectors namespaces parser
prettyprint quotations sequences sequences.generalizations
source-files source-files.errors source-files.errors.debugger
splitting stack-checker summary system tools.errors unicode
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
SYMBOL: restartable-tests?
t restartable-tests? set-global
: <test-failure> ( error experiment path line# -- test-failure )
2009-04-09 05:50:47 -04:00
test-failure new
swap >>line#
swap >>path
swap >>asset
2009-04-09 05:50:47 -04:00
swap >>error
error-continuation get >>continuation ;
SYMBOL: long-unit-tests-enabled?
long-unit-tests-enabled? [ t ] initialize
<PRIVATE
: notify-test-failed ( error experiment path 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
: notify-test-file-failed ( error -- )
[ f current-test-file get ] keep error-line notify-test-failed ;
2007-09-20 18:09:08 -04:00
:: (unit-test) ( output input -- error/f failed? tested? )
[ { } input with-datastack output assert-sequence= f f ] [ t ] recover t ;
: (long-unit-test) ( output input -- error/f failed? tested? )
long-unit-tests-enabled? get [ (unit-test) ] [ 2drop f f f ] if ;
2007-09-20 18:09:08 -04:00
: (unit-test-comparator) ( output input comparator -- error/f failed? tested? )
swapd '[
{ } _ with-datastack _ >quotation
[ 3dup @ [ 3drop t ] [ drop assert ] if ] compose
with-datastack first dup not
] [ t ] recover t ; inline
: (unit-test~) ( output input -- error/f failed? tested? )
[ ~ ] (unit-test-comparator) ;
: (unit-test-v~) ( output input -- error/f failed? tested? )
[ v~ ] (unit-test-comparator) ;
: short-effect ( effect -- pair )
2008-07-28 18:54:10 -04:00
[ in>> length ] [ out>> length ] bi 2array ;
:: (must-infer-as) ( effect quot -- error/f failed? tested? )
[ quot infer short-effect effect assert= f f ] [ t ] recover t ;
2009-04-09 05:50:47 -04:00
:: (must-infer) ( quot -- error/f failed? tested? )
[ quot infer drop f f ] [ t ] recover t ;
2009-04-09 05:50:47 -04:00
SINGLETON: 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/f failed? tested? )
[ { } quot with-datastack drop did-not-fail t ]
[ dup pred call( error -- ? ) [ drop f f ] [ t ] if ] recover t ;
2009-04-09 05:50:47 -04:00
:: (must-fail) ( quot -- error/f failed? tested? )
[ { } quot with-datastack drop did-not-fail t ] [ drop f f ] recover t ;
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/f failed? tested? ) 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# notify-test-failed
] [ rethrow ] if
] [ drop ] if
] [ 2drop "Warning: test skipped!" print ] 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 notify-error-observers ; 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 ] [
restartable-tests? get
[ dup compute-restarts empty? not ] [ f ] if
[ rethrow ] [ notify-test-file-failed ] if
] 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>
: with-test-file ( ..a quot: ( ..a path -- ..b ) -- ..b )
'[ "" "" _ cleanup-unique-file ] with-temp-directory ; inline
: with-test-directory ( ..a quot: ( ..a -- ..b ) -- ..b )
[ cleanup-unique-directory ] with-temp-directory ; inline
2009-04-09 05:50:47 -04:00
TEST: unit-test
TEST: unit-test~
TEST: unit-test-v~
TEST: unit-test-comparator
TEST: long-unit-test
2009-04-09 05:50:47 -04:00
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>> call>> callstack. ]
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 [ don't-test? ] reject test-vocabs ;
: test-main ( -- )
command-line get [ [ load ] [ test ] bi ] each
test-failures get empty? [ 0 ] [ 1 ] if exit ;
MAIN: test-main