]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/test/test.factor
3dc7b8740b171b1da5c60b78f47ef5c796333923
[factor.git] / basis / tools / test / test.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators compiler.units
4 continuations debugger effects fry generalizations io io.files
5 io.styles kernel lexer locals macros math.parser namespaces
6 parser prettyprint quotations sequences source-files splitting
7 stack-checker summary unicode.case vectors vocabs vocabs.loader
8 vocabs.files words tools.errors source-files.errors
9 io.streams.string make compiler.errors ;
10 IN: tools.test
11
12 TUPLE: test-failure < source-file-error continuation ;
13
14 SYMBOL: +test-failure+
15
16 M: test-failure error-type drop +test-failure+ ;
17
18 SYMBOL: test-failures
19
20 test-failures [ V{ } clone ] initialize
21
22 T{ error-type
23    { type +test-failure+ }
24    { word ":test-failures" }
25    { plural "unit test failures" }
26    { icon "vocab:ui/tools/error-list/icons/unit-test-error.tiff" }
27    { quot [ test-failures get ] }
28 } define-error-type
29
30 <PRIVATE
31
32 : <test-failure> ( error experiment file line# -- triple )
33     test-failure new
34         swap >>line#
35         swap >>file
36         swap >>asset
37         swap >>error
38         error-continuation get >>continuation ;
39
40 : failure ( error experiment file line# -- )
41     "--> test failed!" print
42     <test-failure> test-failures get push
43     notify-error-observers ;
44
45 SYMBOL: file
46
47 : file-failure ( error -- )
48     f file get f failure ;
49
50 :: (unit-test) ( output input -- error ? )
51     [ { } input with-datastack output assert-sequence= f f ] [ t ] recover ;
52
53 : short-effect ( effect -- pair )
54     [ in>> length ] [ out>> length ] bi 2array ;
55
56 :: (must-infer-as) ( effect quot -- error ? )
57     [ quot infer short-effect effect assert= f f ] [ t ] recover ;
58
59 :: (must-infer) ( quot -- error ? )
60     [ quot infer drop f f ] [ t ] recover ;
61
62 TUPLE: did-not-fail ;
63 CONSTANT: did-not-fail T{ did-not-fail }
64
65 M: did-not-fail summary drop "Did not fail" ;
66
67 :: (must-fail-with) ( quot pred -- error ? )
68     [ { } quot with-datastack drop did-not-fail t ]
69     [ dup pred call( error -- ? ) [ drop f f ] [ t ] if ] recover ;
70
71 :: (must-fail) ( quot -- error ? )
72     [ { } quot with-datastack drop did-not-fail t ] [ drop f f ] recover ;
73
74 : experiment-title ( word -- string )
75     "(" ?head drop ")" ?tail drop { { CHAR: - CHAR: \s } } substitute >title ;
76
77 MACRO: <experiment> ( word -- )
78     [ stack-effect in>> length dup ]
79     [ name>> experiment-title ] bi
80     '[ _ ndup _ narray _ prefix ] ;
81
82 : experiment. ( seq -- )
83     [ first write ": " write ] [ rest . ] bi ;
84
85 :: experiment ( word: ( -- error ? ) line# -- )
86     word <experiment> :> e
87     e experiment.
88     word execute [
89         file get [
90             e file get line# failure
91         ] [ rethrow ] if
92     ] [ drop ] if ; inline
93
94 : parse-test ( accum word -- accum )
95     literalize parsed
96     lexer get line>> parsed
97     \ experiment parsed ; inline
98
99 <<
100
101 SYNTAX: TEST:
102     scan
103     [ create-in ]
104     [ "(" ")" surround search '[ _ parse-test ] ] bi
105     define-syntax ;
106
107 >>
108
109 : run-test-file ( path -- )
110     dup file [
111         test-failures get file get +test-failure+ delete-file-errors
112         '[ _ run-file ] [ file-failure ] recover
113     ] with-variable ;
114
115 : run-vocab-tests ( vocab -- )
116     dup vocab source-loaded?>> [
117         vocab-tests [ run-test-file ] each
118     ] [ drop ] if ;
119
120 : traceback-button. ( failure -- )
121     "[" write [ "Traceback" ] dip continuation>> write-object "]" print ;
122
123 PRIVATE>
124
125 TEST: unit-test
126 TEST: must-infer-as
127 TEST: must-infer
128 TEST: must-fail-with
129 TEST: must-fail
130
131 M: test-failure error. ( error -- )
132     {
133         [ summary print nl ]
134         [ asset>> [ experiment. nl ] when* ]
135         [ error>> error. ]
136         [ traceback-button. ]
137     } cleave ;
138
139 : :test-failures ( -- ) test-failures get errors. ;
140
141 : test ( prefix -- )
142     child-vocabs [ run-vocab-tests ] each ;
143
144 : test-all ( -- ) "" test ;