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
59 | 59 |
The idea is that you give multiple input strings in a set; if the
|
60 | 60 |
function does not map them all to the same value, that's a test
|
61 | 61 |
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 | |
|
71 | 62 |
* Test monadic functions somehow.
|
72 | 63 |
* Expand on the previous point to test external programs via
|
73 | 64 |
Unix subprocesses; after all, Unix programs are more or less
|
2 | 2 |
|
3 | 3 |
The Function to be Tested
|
4 | 4 |
-------------------------
|
|
5 |
|
|
6 |
A function taking Strings to Strings.
|
5 | 7 |
|
6 | 8 |
> everySecond :: String -> String
|
7 | 9 |
> everySecond [] = []
|
|
10 | 12 |
> everySecond "silliness" = error "silliness"
|
11 | 13 |
> everySecond (a : b : rest) = (b : everySecond rest)
|
12 | 14 |
|
|
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 |
|
13 | 23 |
The Falderal Driver
|
14 | 24 |
-------------------
|
15 | 25 |
|
16 | 26 |
> 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 |
> ]
|
19 | 30 |
|
20 | 31 |
Tests for everySecond
|
21 | 32 |
---------------------
|
|
58 | 69 |
|
59 | 70 |
| ridiculous
|
60 | 71 |
? 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 |
= []
|