git @ Cat's Eye Technologies Falderal / 53b628c
Demonstrate how to test functions whose ranges are of type (Show a) => a instead of String. (It's not difficult.) Chris Pressey 13 years ago
2 changed file(s) with 36 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
5959 The idea is that you give multiple input strings in a set; if the
6060 function does not map them all to the same value, that's a test
6161 failure.
62 * Test functions of type
63
64 (Show a) => String -> a
65
66 This is just a matter of wrapping them with an "adapter" like
67 the following before testing:
68
69 w f = \x -> show (f x)
70
7162 * Test monadic functions somehow.
7263 * Expand on the previous point to test external programs via
7364 Unix subprocesses; after all, Unix programs are more or less
22
33 The Function to be Tested
44 -------------------------
5
6 A function taking Strings to Strings.
57
68 > everySecond :: String -> String
79 > everySecond [] = []
1012 > everySecond "silliness" = error "silliness"
1113 > everySecond (a : b : rest) = (b : everySecond rest)
1214
15 A function taking Strings to Lists of Booleans. We test this by
16 composing it with show.
17
18 > parseBits :: String -> [Bool]
19 > parseBits [] = []
20 > parseBits ('0':rest) = (False:parseBits rest)
21 > parseBits ('1':rest) = (True:parseBits rest)
22
1323 The Falderal Driver
1424 -------------------
1525
1626 > testDemo = Runner.run "Test/Falderal/Demo.lhs" [
17 > ("Tests for everySecond", everySecond)
18 > ]
27 > ("Tests for everySecond", everySecond),
28 > ("Tests for parseBits", show . parseBits)
29 > ]
1930
2031 Tests for everySecond
2132 ---------------------
5869
5970 | ridiculous
6071 ? Prelude.head: empty list
72
73 Tests for parseBits
74 -------------------
75
76 We can test functions of type
77
78 f :: (Show a) => String -> a
79
80 by simply composing them with show, i.e.
81
82 show . f :: String -> String
83
84 | 01
85 = [False,True]
86
87 An intentionally failing test to demonstrate that it is important
88 to get the formatting of the output right, when testing with show.
89
90 | 01
91 = [False, True]
92
93 |
94 = []