]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/test/test.factor
Create basis vocab root
[factor.git] / basis / tools / test / test.factor
1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors namespaces arrays prettyprint sequences kernel
4 vectors quotations words parser assocs combinators continuations
5 debugger io io.files vocabs vocabs.loader source-files
6 compiler.units summary inference effects tools.vocabs ;
7 IN: tools.test
8
9 SYMBOL: failures
10
11 : <failure> ( error what -- triple )
12     error-continuation get 3array ;
13
14 : failure ( error what -- )
15     <failure> failures get push ;
16
17 SYMBOL: this-test
18
19 : (unit-test) ( what quot -- )
20     swap dup . flush this-test set
21     failures get [
22         [ this-test get failure ] recover
23     ] [
24         call
25     ] if ;
26
27 : unit-test ( output input -- )
28     [ 2array ] 2keep [
29         { } swap with-datastack swap >array assert=
30     ] 2curry (unit-test) ;
31
32 : short-effect ( effect -- pair )
33     [ in>> length ] [ out>> length ] bi 2array ;
34
35 : must-infer-as ( effect quot -- )
36     >r 1quotation r> [ infer short-effect ] curry unit-test ;
37
38 : must-infer ( word/quot -- )
39     dup word? [ 1quotation ] when
40     [ infer drop ] curry [ ] swap unit-test ;
41
42 : must-fail-with ( quot pred -- )
43     >r [ f ] compose r>
44     [ recover ] 2curry
45     [ t ] swap unit-test ;
46
47 : must-fail ( quot -- )
48     [ drop t ] must-fail-with ;
49
50 : (run-test) ( vocab -- )
51     dup vocab-source-loaded? [
52         vocab-tests [ run-file ] each
53     ] [ drop ] if ;
54
55 : run-test ( vocab -- failures )
56     V{ } clone [
57         failures [
58             [ (run-test) ] [ swap failure ] recover
59         ] with-variable
60     ] keep ;
61
62 : failure. ( triple -- )
63     dup second .
64     dup first print-error
65     "Traceback" swap third write-object ;
66
67 : test-failures. ( assoc -- )
68     [
69         nl
70         dup empty? [
71             drop
72             "==== ALL TESTS PASSED" print
73         ] [
74             "==== FAILING TESTS:" print
75             [
76                 swap vocab-heading.
77                 [ failure. nl ] each
78             ] assoc-each
79         ] if
80     ] [
81         "==== NOTHING TO TEST" print
82     ] if* ;
83
84 : run-tests ( prefix -- failures )
85     child-vocabs dup empty? [ drop f ] [
86         [ dup run-test ] { } map>assoc
87         [ second empty? not ] filter
88     ] if ;
89
90 : test ( prefix -- )
91     run-tests test-failures. ;
92
93 : run-all-tests ( prefix -- failures )
94     "" run-tests ;
95
96 : test-all ( -- )
97     run-all-tests test-failures. ;