Parse shell command functionality specifiers in Falderal files.
catseye
13 years ago
40 | 40 | format _ blocks = |
41 | 41 | prelude ++ (formatBlocks $ transformBlocks blocks "") ++ postlude |
42 | 42 | |
43 | formatBlocks ((commandString, test):rest) = | |
43 | formatBlocks ((commandString, test@(Test desc input expectation)):rest) = | |
44 | 44 | let |
45 | Test desc input expectation = test | |
46 | -- ExpectedResult expected = expectation | |
47 | expected = show expectation | |
45 | Output expected = expectation | |
48 | 46 | inputHereDoc = hereDoc "input.txt" input |
49 | 47 | expectedHereDoc = hereDoc "expected.txt" expected |
50 | command = commandString ++ "\n" | |
48 | command = commandString ++ " <input.txt >output.txt\n" | |
49 | diff = "diff -u expected.txt output.txt\n" | |
50 | formattedBlock = inputHereDoc ++ expectedHereDoc ++ command ++ diff | |
51 | 51 | in |
52 | inputHereDoc ++ expectedHereDoc ++ command ++ formatBlocks rest | |
52 | formattedBlock ++ "\n" ++ formatBlocks rest | |
53 | 53 | formatBlocks (_:rest) = |
54 | 54 | formatBlocks rest |
55 | 55 | formatBlocks [] = |
67 | 67 | -- XXX derive sentinel from text |
68 | 68 | |
69 | 69 | hereDoc filename text = |
70 | "cat >" ++ filename ++ "<<EOF\n" ++ text ++ "\nEOF\n" | |
70 | "cat >" ++ filename ++ " <<EOF\n" ++ text ++ "\nEOF\n" | |
71 | 71 | |
72 | 72 | prelude = |
73 | 73 | "#!/bin/sh\n\ |
160 | 160 | -- Parse a pragma. |
161 | 161 | -- |
162 | 162 | |
163 | parseHaskellFunctionality text = | |
164 | case stripPrefix "Haskell function " text of | |
165 | Just specifier -> | |
166 | let | |
167 | (moduleName, functionName) = parseSpecifier specifier | |
168 | in | |
169 | Just $ HaskellDirective moduleName functionName | |
170 | Nothing -> | |
171 | Nothing | |
172 | ||
173 | parseShellFunctionality text = | |
174 | case stripPrefix "shell command " text of | |
175 | Just specifier -> | |
176 | Just $ ShellDirective $ parseQuotedString specifier | |
177 | Nothing -> | |
178 | Nothing | |
179 | ||
180 | functionalities = [ | |
181 | parseHaskellFunctionality, | |
182 | parseShellFunctionality | |
183 | ] | |
184 | ||
163 | 185 | parsePragma text = |
164 | 186 | case stripPrefix "Tests for " text of |
165 | 187 | Just rest -> |
166 | case stripPrefix "Haskell function " rest of | |
167 | Just specifier -> | |
168 | let | |
169 | (moduleName, functionName) = parseSpecifier specifier | |
170 | in | |
171 | HaskellDirective moduleName functionName | |
172 | Nothing -> error "bad pragma" | |
188 | tryFunctionalities functionalities rest | |
173 | 189 | Nothing -> |
174 | error "bad pragma" | |
190 | error ("bad pragma: " ++ text) | |
191 | ||
192 | tryFunctionalities [] text = | |
193 | error ("bad functionality: " ++ text) | |
194 | tryFunctionalities (func:rest) text = | |
195 | case func text of | |
196 | Just x -> x | |
197 | Nothing -> tryFunctionalities rest text | |
175 | 198 | |
176 | 199 | parseSpecifier specifier = |
177 | 200 | let |
178 | 201 | (m, f) = break (\y -> y == ':') specifier |
179 | 202 | in |
180 | 203 | (m, stripLeading ':' f) |
204 | ||
205 | parseQuotedString ('"':rest) = | |
206 | parseQuotedString' rest | |
207 | parseQuotedString (_:rest) = | |
208 | parseQuotedString rest | |
209 | ||
210 | parseQuotedString' ('"':rest) = | |
211 | "" | |
212 | parseQuotedString' (char:rest) = | |
213 | (char:parseQuotedString' rest) |
0 | Tests for wc | |
1 | ============ | |
2 | ||
3 | This Falderal document was written by Chris Pressey. It is hereby placed in | |
4 | the public domain. | |
5 | ||
6 | -> Tests for shell command "wc -l" | |
7 | ||
8 | | These are eight words | |
9 | | that span | |
10 | | three lines. | |
11 | = 3 | |
12 | ||
13 | -> Tests for shell command "wc -w" | |
14 | ||
15 | | These are eight words | |
16 | | that span | |
17 | | three lines. | |
18 | = 8 | |
19 | ||
20 | | Here are eight words on a single line. | |
21 | = 8 | |
22 | ||
23 | An intentionally failing test. | |
24 | ||
25 | | Not four words! | |
26 | = 4 |