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 parser
6 vocabs.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 io.streams.string
12 TUPLE: test-failure < source-file-error continuation ;
14 SYMBOL: +test-failure+
16 M: test-failure error-type drop +test-failure+ ;
20 test-failures [ V{ } clone ] initialize
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 ] }
30 SYMBOL: verbose-tests?
31 t verbose-tests? set-global
35 : <test-failure> ( error experiment file line# -- triple )
41 error-continuation get >>continuation ;
43 : failure ( error experiment file line# -- )
44 "--> test failed!" print
45 <test-failure> test-failures get push
46 notify-error-observers ;
50 : file-failure ( error -- )
51 [ f file get ] keep error-line failure ;
53 :: (unit-test) ( output input -- error ? )
54 [ { } input with-datastack output assert-sequence= f f ] [ t ] recover ;
56 : short-effect ( effect -- pair )
57 [ in>> length ] [ out>> length ] bi 2array ;
59 :: (must-infer-as) ( effect quot -- error ? )
60 [ quot infer short-effect effect assert= f f ] [ t ] recover ;
62 :: (must-infer) ( quot -- error ? )
63 [ quot infer drop f f ] [ t ] recover ;
66 CONSTANT: did-not-fail T{ did-not-fail }
68 M: did-not-fail summary drop "Did not fail" ;
70 :: (must-fail-with) ( quot pred -- error ? )
71 [ { } quot with-datastack drop did-not-fail t ]
72 [ dup pred call( error -- ? ) [ drop f f ] [ t ] if ] recover ;
74 :: (must-fail) ( quot -- error ? )
75 [ { } quot with-datastack drop did-not-fail t ] [ drop f f ] recover ;
77 : experiment-title ( word -- string )
78 "(" ?head drop ")" ?tail drop { { CHAR: - CHAR: \s } } substitute >title ;
80 MACRO: <experiment> ( word -- )
81 [ stack-effect in>> length dup ]
82 [ name>> experiment-title ] bi
83 '[ _ ndup _ narray _ prefix ] ;
85 : experiment. ( seq -- )
86 [ first write ": " write ]
87 [ rest verbose-tests? get [ . ] [ short. ] if flush ] bi ;
89 :: experiment ( word: ( -- error ? ) line# -- )
90 word <experiment> :> e
94 e file get line# failure
96 ] [ drop ] if ; inline
98 : parse-test ( accum word -- accum )
100 lexer get line>> suffix!
101 \ experiment suffix! ; inline
108 [ "(" ")" surround search '[ _ parse-test ] ] bi
113 : run-test-file ( path -- )
115 test-failures get file get +test-failure+ delete-file-errors
116 '[ _ run-file ] [ file-failure ] recover
119 : run-vocab-tests ( vocab -- )
120 dup vocab source-loaded?>> [
121 vocab-tests [ run-test-file ] each
124 : traceback-button. ( failure -- )
125 "[" write [ "Traceback" ] dip continuation>> write-object "]" print ;
135 M: test-failure error. ( error -- )
137 [ error-location print nl ]
138 [ asset>> [ experiment. nl ] when* ]
140 [ traceback-button. ]
143 : :test-failures ( -- ) test-failures get errors. ;
146 child-vocabs [ run-vocab-tests ] each ;
148 : test-all ( -- ) "" test ;