factor/basis/tools/test/test.factor

98 lines
2.3 KiB
Factor
Raw Normal View History

2008-01-09 19:13:26 -05:00
! Copyright (C) 2003, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-07-28 18:54:10 -04:00
USING: accessors namespaces arrays prettyprint sequences kernel
vectors quotations words parser assocs combinators continuations
2008-07-29 04:17:21 -04:00
debugger io io.styles io.files vocabs vocabs.loader source-files
2008-08-12 04:31:48 -04:00
compiler.units summary stack-checker effects tools.vocabs ;
2007-09-20 18:09:08 -04:00
IN: tools.test
SYMBOL: failures
: <failure> ( error what -- triple )
error-continuation get 3array ;
: failure ( error what -- )
2008-10-20 22:07:46 -04:00
"--> test failed!" print
<failure> failures get push ;
2007-09-20 18:09:08 -04:00
SYMBOL: this-test
: (unit-test) ( what quot -- )
swap dup . flush this-test set
2008-05-07 18:42:41 -04:00
failures get [
2007-12-11 22:36:40 -05:00
[ this-test get failure ] recover
2007-09-20 18:09:08 -04:00
] [
call
] if ;
: unit-test ( output input -- )
[ 2array ] 2keep [
{ } swap with-datastack swap >array assert=
] 2curry (unit-test) ;
: short-effect ( effect -- pair )
2008-07-28 18:54:10 -04:00
[ in>> length ] [ out>> length ] bi 2array ;
: must-infer-as ( effect quot -- )
>r 1quotation r> [ infer short-effect ] curry unit-test ;
: must-infer ( word/quot -- )
dup word? [ 1quotation ] when
[ infer drop ] curry [ ] swap unit-test ;
: must-fail-with ( quot pred -- )
2008-02-10 02:38:58 -05:00
>r [ f ] compose r>
2008-02-06 14:15:15 -05:00
[ recover ] 2curry
2008-02-06 16:00:10 -05:00
[ t ] swap unit-test ;
2008-02-06 14:15:15 -05:00
: must-fail ( quot -- )
[ drop t ] must-fail-with ;
2007-09-20 18:09:08 -04:00
: (run-test) ( vocab -- )
dup vocab-source-loaded? [
2008-03-01 17:00:45 -05:00
vocab-tests [ run-file ] each
] [ drop ] if ;
: run-test ( vocab -- failures )
V{ } clone [
failures [
[ (run-test) ] [ swap failure ] recover
] with-variable
] keep ;
2007-09-20 18:09:08 -04:00
: failure. ( triple -- )
dup second .
dup first print-error
"Traceback" swap third write-object ;
2008-02-07 19:03:01 -05:00
: test-failures. ( assoc -- )
2008-02-08 02:49:05 -05:00
[
2007-09-20 18:09:08 -04:00
nl
2008-09-06 20:13:59 -04:00
[
2008-01-11 03:32:25 -05:00
"==== ALL TESTS PASSED" print
] [
"==== FAILING TESTS:" print
2008-02-06 22:15:33 -05:00
[
swap vocab-heading.
2008-02-08 02:49:05 -05:00
[ failure. nl ] each
2008-02-06 22:15:33 -05:00
] assoc-each
2008-09-06 20:13:59 -04:00
] if-empty
2008-02-06 22:15:33 -05:00
] [
2008-02-08 02:49:05 -05:00
"==== NOTHING TO TEST" print
] if* ;
2007-09-20 18:09:08 -04:00
: run-tests ( prefix -- failures )
2008-09-06 20:13:59 -04:00
child-vocabs [ f ] [
2008-02-06 22:15:33 -05:00
[ dup run-test ] { } map>assoc
[ second empty? not ] filter
2008-09-06 20:13:59 -04:00
] if-empty ;
2007-09-20 18:09:08 -04:00
2008-02-06 22:15:33 -05:00
: test ( prefix -- )
2008-02-07 19:03:01 -05:00
run-tests test-failures. ;
2008-02-06 22:15:33 -05:00
: run-all-tests ( prefix -- failures )
"" run-tests ;
2007-09-20 18:09:08 -04:00
2008-02-06 22:15:33 -05:00
: test-all ( -- )
2008-02-07 19:03:01 -05:00
run-all-tests test-failures. ;