git @ Cat's Eye Technologies Falderal / fbb6d85
Format multiline failure output more nicely. Add test harness. Chris Pressey 10 years ago
4 changed file(s) with 101 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
1919 > parseBits [] = []
2020 > parseBits ('0':rest) = (False:parseBits rest)
2121 > parseBits ('1':rest) = (True:parseBits rest)
22 > parseBits ('\n':rest) = parseBits rest
2223
2324 The Falderal Driver
2425 -------------------
9697
9798 |
9899 = []
100
101 Input can consist of multiple lines of text. These are joined together
102 with intervening newline characters.
103
104 | 00
105 | 11
106 = [False,False,True,True]
107
108 An intentionally failing test to demonstrate show what a failure
109 looks like on multi-line input.
110
111 | 01
112 | 10
113 = [False,False,True,True]
1212 import qualified Control.Exception as Exc
1313
1414 --
15 -- collecting TODOs here because I don't have access to the issue tracker atm
16 --
17 -- TODO: in convertLinesToBlocks, Invalid sequences (such as an expected
18 -- result without any preceding test input) should be flagged as errors
19 -- instead of being silently ignored
20 --
21 -- TODO: selectTestFun ought to be more forgiving: if no fun could be found
22 -- in this section, skip the tests. This necessitates a "skip" result.
23 --
24
25 --
1526 -- Definitions.
1627 --
1728
1930 | ExpectedResult String
2031 | ExpectedError String
2132 | LiteralText String
33 | QuotedCode String
2234 | SectionHeading String
2335 deriving (Show, Eq, Ord)
2436
88100 | prefix == "| " = TestInput suffix
89101 | prefix == "= " = ExpectedResult suffix
90102 | prefix == "? " = ExpectedError suffix
103 | prefix == "> " = QuotedCode suffix
91104 | otherwise = LiteralText line
92105 where
93106 prefix = take 2 line
132145 coalesceLines lines (ExpectedResult (last ++ "\n" ++ more))
133146 coalesceLines ((LiteralText more):lines) (LiteralText last) =
134147 coalesceLines lines (LiteralText (last ++ "\n" ++ more))
148 coalesceLines ((QuotedCode more):lines) (QuotedCode last) =
149 coalesceLines lines (QuotedCode (last ++ "\n" ++ more))
135150 coalesceLines (line:lines) last =
136151 (last:coalesceLines lines line)
137152
147162 ((Section text):convertLinesToBlocks rest)
148163 convertLinesToBlocks ((LiteralText _):(SectionHeading text):rest) =
149164 ((Section text):convertLinesToBlocks rest)
150
151 -- Invalid sequences (such as an expected result without any preceding test
152 -- input) are silently ignored for now, but should be flagged as errors.
153165
154166 convertLinesToBlocks (_:rest) =
155167 convertLinesToBlocks rest
174186 remainder <- runTests funMap testFun rest
175187 return ((Failure literalText inputText expected actual):remainder)
176188
177 -- This ought to be more forgiving;
178 -- if no fun could be found in this section, skip the tests.
179189 selectTestFun ((text, fun):rest) sectionText
180190 | text == sectionText = fun
181191 | otherwise = selectTestFun rest sectionText
203213 reportEachTest [] = do
204214 return ()
205215 reportEachTest ((Failure literalText testText expected actual):rest) = do
206 putStrLn ("FAILED: " ++ literalText)
207 putStrLn ("Input : " ++ testText)
208 putStrLn ("Expected: " ++ (show expected))
209 putStrLn ("Actual : " ++ (show actual))
216 reportText 8 "FAILED" (stripLeading '\n' literalText)
217 reportText 8 "Input" testText
218 reportText 8 "Expected" (show expected)
219 reportText 8 "Actual" (show actual)
210220 putStrLn ""
211221 reportEachTest rest
222
223 stripLeading y all@(x:xs)
224 | x == y = stripLeading y xs
225 | otherwise = all
226
227 reportText width fieldName text =
228 if
229 contains text '\n'
230 then do
231 putStrLn (fieldName ++ ":")
232 putStrLn text
233 else do
234 putStrLn ((pad fieldName width) ++ ": " ++ text)
235
236 contains [] _ = False
237 contains (x:xs) y
238 | x == y = True
239 | otherwise = contains xs y
240
241 pad s n = padFrom s (n-(length s))
242 padFrom s n
243 | n <= 0 = s
244 | otherwise = padFrom (s ++ " ") (n-1)
0 --------------------------------
1 Total tests: 12, failures: 4
2 --------------------------------
3
4 FAILED:
5 This is an intentionally failing test, to demonstrate how Falderal will
6 present it.
7
8 Input : Something
9 Expected: Output "Anything"
10 Actual : Output "oehn"
11
12 FAILED:
13 Another intentionally failing test to demonstrate how Falderal will
14 present expecting an exception and not getting one.
15
16 Input : ridiculous
17 Expected: Exception "Prelude.head: empty list"
18 Actual : Output "iiuos"
19
20 FAILED:
21 An intentionally failing test to demonstrate that it is important
22 to get the formatting of the output right, when testing with show.
23
24 Input : 01
25 Expected: Output "[False, True]"
26 Actual : Output "[False,True]"
27
28 FAILED:
29 An intentionally failing test to demonstrate show what a failure
30 looks like on multi-line input.
31
32 Input:
33 01
34 10
35 Expected: Output "[False,False,True,True]"
36 Actual : Output "[False,True,True,False]"
37
0 #!/bin/sh
1
2 # A tiny test harness for Falderal itself.
3
4 ghc Test/Falderal/Demo.lhs -e test >actual.txt
5 diff -u expected.txt actual.txt