Skip to content

Commit 6aa3ab5

Browse files
committed
Added proper cabal test suite.
1 parent 0b06ab2 commit 6aa3ab5

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

Setup.lhs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env runhaskell
2+
> import Distribution.Simple
3+
> main = defaultMain

tests/test-texmath.hs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import System.Directory
2+
import System.FilePath
3+
import Text.XML.Light
4+
import System.IO
5+
import Text.TeXMath
6+
import Text.TeXMath.Macros
7+
import System.Exit
8+
import Control.Applicative
9+
import GHC.IO.Encoding (setLocaleEncoding)
10+
11+
data Status = Pass
12+
| Fail FilePath FilePath String
13+
| Error FilePath FilePath String
14+
deriving (Eq, Show)
15+
16+
main :: IO ()
17+
main = do
18+
setLocaleEncoding utf8
19+
setCurrentDirectory "tests"
20+
texs <- filter (\x -> takeExtension x == ".tex") <$> getDirectoryContents "."
21+
statuses <- concat <$> mapM runTeXTests texs
22+
let passes = length $ filter (== Pass) statuses
23+
let failures = length statuses - passes
24+
putStrLn $ show passes ++ " tests passed, " ++ show failures ++ " failed."
25+
if all (== Pass) statuses
26+
then do
27+
exitWith ExitSuccess
28+
else do
29+
mapM_ printStatus statuses
30+
exitWith $ ExitFailure failures
31+
32+
printStatus :: Status -> IO ()
33+
printStatus Pass = return ()
34+
printStatus (Fail inp out actual) = do
35+
putStrLn $ "FAILED: " ++ inp ++ " ==> " ++ out
36+
putStrLn "------ input ---------"
37+
readFile inp >>= putStr
38+
putStrLn "------ expected ------"
39+
readFile out >>= putStr
40+
putStrLn "------ actual --------"
41+
putStr actual
42+
putStrLn "----------------------"
43+
printStatus (Error inp out msg) =
44+
putStrLn $ "ERROR: " ++ inp ++ " ==> " ++ out ++ "\n" ++ msg
45+
46+
runTeXTests :: FilePath -> IO [Status]
47+
runTeXTests path = do
48+
xhtmlStatus <- runTest
49+
(fmap (ppTopElement . inHtml) . texMathToMathML DisplayBlock . parseMacros)
50+
path (replaceExtension path "xhtml")
51+
ommlStatus <- runTest
52+
(fmap ppTopElement . texMathToOMML DisplayBlock . parseMacros)
53+
path (replaceExtension path "omml")
54+
return [xhtmlStatus, ommlStatus]
55+
56+
parseMacros :: String -> String
57+
parseMacros inp =
58+
let (ms, rest) = parseMacroDefinitions inp
59+
in applyMacros ms rest
60+
61+
inHtml :: Element -> Element
62+
inHtml x =
63+
add_attr (Attr (unqual "xmlns") "http://www.w3.org/1999/xhtml") $
64+
unode "html"
65+
[ unode "head" $
66+
add_attr (Attr (unqual "content") "application/xhtml+xml; charset=UTF-8")
67+
$ add_attr (Attr (unqual "http-equiv") "Content-Type")
68+
$ unode "meta" ()
69+
, unode "body" x ]
70+
71+
runTest :: (String -> Either String String)
72+
-> FilePath
73+
-> FilePath
74+
-> IO Status
75+
runTest fn input output = do
76+
inp_t <- readFile input
77+
out_t <- readFile output
78+
case fn inp_t of
79+
Left msg -> return $ Error input output msg
80+
Right r ->
81+
if r == out_t
82+
then return Pass
83+
else return $ Fail input output r

texmath.cabal

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Name: texmath
22
Version: 0.6.6.3
3-
Cabal-Version: >= 1.6
4-
Build-type: Custom
3+
Cabal-Version: >= 1.10
4+
Build-type: Simple
55
Synopsis: Conversion of LaTeX math formulas to MathML or OMML.
66
Description: The texmathml library provides functions to convert LaTeX
77
math formulas to presentation MathML (which can be used
@@ -83,7 +83,7 @@ Flag test
8383
Library
8484
Build-depends: xml, parsec >= 3, containers, pandoc-types
8585
if impl(ghc >= 6.10)
86-
Build-depends: base >= 4 && < 5, syb
86+
Build-depends: base >= 4.5 && < 5, syb
8787
else
8888
Build-depends: base >= 3 && < 4
8989
Exposed-modules: Text.TeXMath
@@ -100,8 +100,10 @@ Library
100100
else
101101
Ghc-Options: -Wall
102102
Ghc-Prof-Options: -auto-all -caf-all
103+
Default-Language: Haskell2010
103104

104105
Executable texmath
106+
Default-Language: Haskell2010
105107
Main-is: texmath.hs
106108
if impl(ghc >= 6.12)
107109
Ghc-Options: -Wall -fno-warn-unused-do-bind
@@ -115,6 +117,7 @@ Executable texmath
115117

116118
Executable texmath-cgi
117119
Main-is: cgi/texmath-cgi.hs
120+
Default-Language: Haskell2010
118121
if impl(ghc >= 6.12)
119122
Ghc-Options: -Wall -fno-warn-unused-do-bind
120123
else
@@ -125,3 +128,12 @@ Executable texmath-cgi
125128
Build-depends: cgi, json, utf8-string
126129
else
127130
Buildable: False
131+
132+
Test-Suite test-texmath
133+
Type: exitcode-stdio-1.0
134+
Main-Is: test-texmath.hs
135+
Hs-Source-Dirs: tests
136+
Build-Depends: base >= 4.2 && < 5, process, directory, filepath,
137+
texmath, xml, utf8-string, bytestring
138+
Default-Language: Haskell2010
139+
Ghc-Options: -Wall

0 commit comments

Comments
 (0)