git @ Cat's Eye Technologies Falderal / f3c6670
Move the literate Haskell demo to the examples directory. --HG-- rename : Test/Falderal/Demo.lhs => eg/LiterateHaskellDemo.lhs catseye 10 years ago
3 changed file(s) with 165 addition(s) and 163 deletion(s). Raw diff Collapse all Expand all
+0
-159
Test/Falderal/Demo.lhs less more
0 > module Test.Falderal.Demo where
1
2 Test.Falderal.Demo
3 ==================
4
5 This literate Haskell source is simply a demonstration of how Falderal
6 can be used to define and run some tests on some Haskell functions.
7
8 This module was written by Chris Pressey. It is hereby placed in
9 the public domain.
10
11 The Function to be Tested
12 -------------------------
13
14 A function taking Strings to Strings.
15
16 > everySecond :: String -> String
17 > everySecond [] = []
18 > everySecond [a] = []
19 > everySecond "silly" = head []
20 > everySecond "silliness" = error "silliness"
21 > everySecond "supercilious" = error "not\non\nmy\nwatch"
22 > everySecond "suoilic\nrepus" = "not\non\nmy\nwatch"
23 > everySecond (a : b : rest) = (b : everySecond rest)
24
25 A function taking Strings to Lists of Booleans. We test this by
26 composing it with show.
27
28 > parseBits :: String -> [Bool]
29 > parseBits [] = []
30 > parseBits ('0':rest) = (False:parseBits rest)
31 > parseBits ('1':rest) = (True:parseBits rest)
32 > parseBits ('\n':rest) = parseBits rest
33
34 > showParseBits = show . parseBits
35
36 Pragmas are able to extend over multiple lines, just like anything else.
37 In addition, spaces between words (outside of quotes strings) are not
38 significant; you can have as many as you like.
39
40 -> Functionality "Retain every second character"
41 -> is implemented
42 -> by Haskell function Test.Falderal.Demo:everySecond
43
44 Tests for everySecond
45 ---------------------
46
47 -> Tests for functionality "Retain every second character"
48
49 Every second symbol in the string is retained.
50
51 | Falderal
52 = adrl
53
54 Works for odd-length strings, too.
55
56 | Bandana
57 = adn
58
59 If there aren't even two symbols in the string, the result is
60 the empty string. Note that we have to precede the expected
61 empty string with "= ", that is, an equals sign and a space.
62
63 | A
64 =
65
66 This is an intentionally failing test, to demonstrate how Falderal will
67 present it.
68
69 | Something
70 = Anything
71
72 A test that expects an exception.
73
74 | silly
75 ? Prelude.head: empty list
76
77 Another test that expects an exception.
78
79 | silliness
80 ? silliness
81
82 The expected text of an exception can extend over several lines.
83
84 | supercilious
85 ? not
86 ? on
87 ? my
88 ? watch
89
90 The input and expected text and extend over several lines, too.
91
92 | suoilic
93 | repus
94 = not
95 = on
96 = my
97 = watch
98
99 Another intentionally failing test to demonstrate how Falderal will
100 present expecting an exception and not getting one.
101
102 | ridiculous
103 ? Prelude.head: empty list
104
105 Tests for parseBits
106 -------------------
107
108 -> Tests for Haskell function Test.Falderal.Demo:showParseBits
109
110 We can test functions of type
111
112 f :: (Show a) => String -> a
113
114 by simply composing them with show, i.e.
115
116 show . f :: String -> String
117
118 | 01
119 = [False,True]
120
121 An intentionally failing test to demonstrate that it is important
122 to get the formatting of the output right, when testing with show.
123
124 | 01
125 = [False, True]
126
127 |
128 = []
129
130 Input can consist of multiple lines of text. These are joined together
131 with intervening newline characters.
132
133 | 00
134 | 11
135 = [False,False,True,True]
136
137 An intentionally failing test to demonstrate show what a failure
138 looks like on multi-line input.
139
140 | 01
141 | 10
142 = [False,False,True,True]
143
144 If we have a set of tests where the tests after the first one have no
145 descriptions of their own, we can take this to suggest they are all
146 testing the same thing. In this case, the literal text that is displayed
147 when any of them fails is the text that comes before the first of them,
148 annotated with the number of the test in the set that failed. The
149 intentionally-failing third test below demonstrates this.
150
151 | 00
152 = [False,False]
153
154 | 000
155 = [False,False,False]
156
157 | 0000
158 = [False,False,False,Flse]
0 > module LiterateHaskellDemo where
1
2 Falderal in Literate Haskell
3 ============================
4
5 This file demonstrates how Falderal tests can be embedded in a
6 Bird-style Literate Haskell source file.
7
8 This module was written by Chris Pressey. It is hereby placed in
9 the public domain.
10
11 The Function to be Tested
12 -------------------------
13
14 A function taking Strings to Strings.
15
16 > everySecond :: String -> String
17 > everySecond [] = []
18 > everySecond [a] = []
19 > everySecond "silly" = head []
20 > everySecond "silliness" = error "silliness"
21 > everySecond "supercilious" = error "not\non\nmy\nwatch"
22 > everySecond "suoilic\nrepus" = "not\non\nmy\nwatch"
23 > everySecond (a : b : rest) = (b : everySecond rest)
24
25 A function taking Strings to Lists of Booleans. We test this by
26 composing it with show.
27
28 > parseBits :: String -> [Bool]
29 > parseBits [] = []
30 > parseBits ('0':rest) = (False:parseBits rest)
31 > parseBits ('1':rest) = (True:parseBits rest)
32 > parseBits ('\n':rest) = parseBits rest
33
34 > showParseBits = show . parseBits
35
36 Pragmas are able to extend over multiple lines, just like anything else.
37 In addition, spaces between words (outside of quotes strings) are not
38 significant; you can have as many as you like.
39
40 -> Functionality "Retain every second character"
41 -> is implemented
42 -> by Haskell function LiterateHaskellDemo:everySecond
43
44 Tests for everySecond
45 ---------------------
46
47 -> Tests for functionality "Retain every second character"
48
49 Every second symbol in the string is retained.
50
51 | Falderal
52 = adrl
53
54 Works for odd-length strings, too.
55
56 | Bandana
57 = adn
58
59 If there aren't even two symbols in the string, the result is
60 the empty string. Note that we have to precede the expected
61 empty string with "= ", that is, an equals sign and a space.
62
63 | A
64 =
65
66 This is an intentionally failing test, to demonstrate how Falderal will
67 present it.
68
69 | Something
70 = Anything
71
72 A test that expects an exception.
73
74 | silly
75 ? Prelude.head: empty list
76
77 Another test that expects an exception.
78
79 | silliness
80 ? silliness
81
82 The expected text of an exception can extend over several lines.
83
84 | supercilious
85 ? not
86 ? on
87 ? my
88 ? watch
89
90 The input and expected text and extend over several lines, too.
91
92 | suoilic
93 | repus
94 = not
95 = on
96 = my
97 = watch
98
99 Another intentionally failing test to demonstrate how Falderal will
100 present expecting an exception and not getting one.
101
102 | ridiculous
103 ? Prelude.head: empty list
104
105 Tests for parseBits
106 -------------------
107
108 -> Tests for Haskell function LiterateHaskellDemo:showParseBits
109
110 We can test functions of type
111
112 f :: (Show a) => String -> a
113
114 by simply composing them with show, i.e.
115
116 show . f :: String -> String
117
118 | 01
119 = [False,True]
120
121 An intentionally failing test to demonstrate that it is important
122 to get the formatting of the output right, when testing with show.
123
124 | 01
125 = [False, True]
126
127 |
128 = []
129
130 Input can consist of multiple lines of text. These are joined together
131 with intervening newline characters.
132
133 | 00
134 | 11
135 = [False,False,True,True]
136
137 An intentionally failing test to demonstrate show what a failure
138 looks like on multi-line input.
139
140 | 01
141 | 10
142 = [False,False,True,True]
143
144 If we have a set of tests where the tests after the first one have no
145 descriptions of their own, we can take this to suggest they are all
146 testing the same thing. In this case, the literal text that is displayed
147 when any of them fails is the text that comes before the first of them,
148 annotated with the number of the test in the set that failed. The
149 intentionally-failing third test below demonstrates this.
150
151 | 00
152 = [False,False]
153
154 | 000
155 = [False,False,False]
156
157 | 0000
158 = [False,False,False,Flse]
44 # installed via Cabal first:
55 # $ cabal clean && cabal install --prefix=$HOME --user
66
7 falderal format identity Test/Falderal/Demo.lhs >formatted.txt
8 diff -u Test/Falderal/Demo.lhs formatted.txt
7 falderal format identity eg/LiterateHaskellDemo.lhs >formatted.txt
8 diff -u eg/LiterateHaskellDemo.lhs formatted.txt
99 E1=$?
1010 rm -f formatted.txt
1111
6161 Actual : Output "[False,False,False,False]"
6262
6363 EOF
64 falderal test standard Test/Falderal/Demo.lhs >actual.txt
64 cd eg
65 falderal test standard LiterateHaskellDemo.lhs >../actual.txt
6566 E2=$?
66 rm -f expected.txt actual.txt GeneratedFalderalTests.hs
67 cd ..
68 rm -f expected.txt actual.txt
6769
6870 if [ $E1 != 0 -o $E2 != 0 ]
6971 then