1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators command-line
4 compiler.units continuations debugger effects fry
5 generalizations io kernel lexer locals macros namespaces parser
6 prettyprint quotations sequences sequences.generalizations
7 source-files source-files.errors source-files.errors.debugger
8 splitting stack-checker summary tools.errors unicode.case vocabs
9 vocabs.files vocabs.metadata vocabs.parser words ;
10 FROM: vocabs.hierarchy => load ;
13 TUPLE: test-failure < source-file-error continuation ;
15 SYMBOL: +test-failure+
17 M: test-failure error-type drop +test-failure+ ;
21 test-failures [ V{ } clone ] initialize
24 { type +test-failure+ }
25 { word ":test-failures" }
26 { plural "unit test failures" }
27 { icon "vocab:ui/tools/error-list/icons/unit-test-error.tiff" }
28 { quot [ test-failures get ] }
31 SYMBOL: verbose-tests?
32 t verbose-tests? set-global
36 : <test-failure> ( error experiment file line# -- triple )
42 error-continuation get >>continuation ;
44 : failure ( error experiment file line# -- )
45 "--> test failed!" print
46 <test-failure> test-failures get push
47 notify-error-observers ;
51 : file-failure ( error -- )
52 [ f file get ] keep error-line failure ;
54 :: (unit-test) ( output input -- error ? )
55 [ { } input with-datastack output assert-sequence= f f ] [ t ] recover ;
57 : short-effect ( effect -- pair )
58 [ in>> length ] [ out>> length ] bi 2array ;
60 :: (must-infer-as) ( effect quot -- error ? )
61 [ quot infer short-effect effect assert= f f ] [ t ] recover ;
63 :: (must-infer) ( quot -- error ? )
64 [ quot infer drop f f ] [ t ] recover ;
67 CONSTANT: did-not-fail-literal T{ did-not-fail }
69 M: did-not-fail summary drop "Did not fail" ;
71 :: (must-fail-with) ( quot pred -- error ? )
72 [ { } quot with-datastack drop did-not-fail-literal t ]
73 [ dup pred call( error -- ? ) [ drop f f ] [ t ] if ] recover ;
75 :: (must-fail) ( quot -- error ? )
76 [ { } quot with-datastack drop did-not-fail-literal t ] [ drop f f ] recover ;
78 : experiment-title ( word -- string )
79 "(" ?head drop ")" ?tail drop
80 H{ { CHAR: - CHAR: \s } } substitute >title ;
82 MACRO: <experiment> ( word -- )
83 [ stack-effect in>> length dup ]
84 [ name>> experiment-title ] bi
85 '[ _ ndup _ narray _ prefix ] ;
87 : experiment. ( seq -- )
88 [ first write ": " write ]
89 [ rest verbose-tests? get [ . ] [ short. ] if flush ] bi ;
91 :: experiment ( word: ( -- error ? ) line# -- )
92 word <experiment> :> e
96 e file get line# failure
98 ] [ drop ] if ; inline
100 : parse-test ( accum word -- accum )
102 lexer get line>> suffix!
103 \ experiment suffix! ; inline
110 [ "(" ")" surround search '[ _ parse-test ] ] bi
115 : fake-unit-test ( quot -- test-failures )
118 V{ } clone test-failures set
121 ] with-scope ; inline
125 : run-test-file ( path -- )
127 test-failures get file get +test-failure+ delete-file-errors
128 '[ _ run-file ] [ file-failure ] recover
131 SYMBOL: forget-tests?
135 : forget-tests ( files -- )
137 [ [ [ forget-source ] each ] with-compilation-unit ] [ drop ] if ;
139 : test-vocab ( vocab -- )
141 dup source-loaded?>> [
143 [ [ run-test-file ] each ]
149 : test-vocabs ( vocabs -- ) [ test-vocab ] each ;
159 M: test-failure error. ( error -- )
161 [ error-location print nl ]
162 [ asset>> [ experiment. nl ] when* ]
164 [ continuation>> traceback-link. ]
167 : :test-failures ( -- ) test-failures get errors. ;
169 : test ( prefix -- ) child-vocabs test-vocabs ;
171 : test-all ( -- ) vocabs filter-don't-test test-vocabs ;
174 command-line get [ [ load ] [ test ] bi ] each ;