git @ Cat's Eye Technologies Falderal / 9c3a585
Make Formatter dispatch to different individual formatters. --HG-- rename : Test/Falderal/Formatter.hs => Test/Falderal/Formatter/Identity.hs catseye 10 years ago
3 changed file(s) with 79 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
0 module Test.Falderal.Formatter.Identity where
1
2 -- TODO: export a more restricted interface
3
4 --
5 -- Test.Falderal.Formatter.Identity -- Identity formatter for Falderal format
6 -- Copyright (c)2011 Cat's Eye Technologies. All rights reserved.
7 --
8 -- Redistribution and use in source and binary forms, with or without
9 -- modification, are permitted provided that the following conditions
10 -- are met:
11 --
12 -- 1. Redistributions of source code must retain the above copyright
13 -- notices, this list of conditions and the following disclaimer.
14 -- 2. Redistributions in binary form must reproduce the above copyright
15 -- notices, this list of conditions, and the following disclaimer in
16 -- the documentation and/or other materials provided with the
17 -- distribution.
18 -- 3. Neither the names of the copyright holders nor the names of their
19 -- contributors may be used to endorse or promote products derived
20 -- from this software without specific prior written permission.
21 --
22 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 -- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 -- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 -- COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 -- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 -- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 -- POSSIBILITY OF SUCH DAMAGE.
34 --
35
36 import System
37
38 import Test.Falderal.Loader
39
40 --
41 -- Formatting function which formats a Falderal file to an identical
42 -- Falderal file.
43 --
44
45 formatLine (TestInput text) =
46 (prefixEachLine "| " text)
47 formatLine (ExpectedResult text) =
48 (prefixEachLine "= " text)
49 formatLine (ExpectedError text) =
50 (prefixEachLine "? " text)
51 formatLine (LiteralText text) =
52 (prefixEachLine "" text)
53 formatLine (QuotedCode text) =
54 (prefixEachLine "> " text)
55 formatLine (SectionHeading text) =
56 text ++ "\n" ++ (take (length text) (repeat '-')) ++ "\n"
57
58 -- Fix lines so that it acts on "" appropriately
59
60 allLines x =
61 case (lines x) of
62 [] -> [""]
63 other -> other
64
65 prefixEachLine prefix text =
66 foldl (++) "" (map (\x -> prefix ++ x ++ "\n") (allLines text))
3636 import System
3737
3838 import Test.Falderal.Loader
39 import qualified Test.Falderal.Formatter.Identity as Identity
3940
4041 --
41 -- File formatting functions. Just a crude throughput for now,
42 -- to show what we can do.
42 -- Driver for Falderal file formatting.
4343 --
44
45 --
46 -- Map from names of formats to formatter functions.
47 --
48
49 getFormatter "identity" = Identity.formatLine
50 getFormatter "dump" = \x -> (show x) ++ "\n"
4451
4552 --
4653 -- We format by (coalesced) lines, instead of by blocks, because by the time
4754 -- the file has been parsed into blocks, some content has been dropped.
4855 --
4956
50 formatFile fileName = do
57 formatFile format fileName = do
5158 testText <- readFile fileName
5259 lines <- return $ transformLines $ lines testText
53 outputText <- return $ formatLines (formatLine) lines
60 outputText <- return $ formatLines (getFormatter format) lines
5461 putStr outputText
5562
56 dumpLine x = (show x) ++ "\n"
57
5863 formatLines formatter lines = foldl (++) "" (map (formatter) lines)
59
60 formatLine (TestInput text) =
61 (prefixEachLine "| " text)
62 formatLine (ExpectedResult text) =
63 (prefixEachLine "= " text)
64 formatLine (ExpectedError text) =
65 (prefixEachLine "? " text)
66 formatLine (LiteralText text) =
67 (prefixEachLine "" text)
68 formatLine (QuotedCode text) =
69 (prefixEachLine "> " text)
70 formatLine (SectionHeading text) =
71 text ++ "\n" ++ (take (length text) (repeat '-')) ++ "\n"
72
73 -- Fix lines so that it acts on "" appropriately
74
75 allLines x =
76 case (lines x) of
77 [] -> [""]
78 other -> other
79
80 prefixEachLine prefix text =
81 foldl (++) "" (map (\x -> prefix ++ x ++ "\n") (allLines text))
5858 E=$?
5959 rm -f expected.txt actual.txt
6060
61 ghc Test/Falderal/Formatter.hs -e 'formatFile "Test/Falderal/Demo.lhs"' >formatted.txt
61 ghc Test/Falderal/Formatter.hs -e 'formatFile "identity" "Test/Falderal/Demo.lhs"' >formatted.txt
6262 diff -u Test/Falderal/Demo.lhs formatted.txt
6363 E=$?
6464 rm -f formatted.txt