summaryrefslogtreecommitdiff
path: root/tests/Tex/LogParse.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Tex/LogParse.hs')
-rw-r--r--tests/Tex/LogParse.hs160
1 files changed, 160 insertions, 0 deletions
diff --git a/tests/Tex/LogParse.hs b/tests/Tex/LogParse.hs
new file mode 100644
index 0000000..6c3e49a
--- /dev/null
+++ b/tests/Tex/LogParse.hs
@@ -0,0 +1,160 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
+module Tex.LogParse where
+
+import Data.ByteString.Lazy.Char8 as B (unlines, ByteString, writeFile)
+import Data.Monoid
+
+import Test.HUnit
+import Test.Framework.Providers.HUnit
+import Test.Framework as F
+
+import System.Texrunner
+import System.Texrunner.Parse
+import Control.Lens
+import Data.Foldable (for_)
+
+tests = texTests ++ latexTests ++ contextTests
+
+texTests = [checkErrors "tex error parse" tex]
+latexTests = [checkErrors "latex error parse" latex]
+contextTests = [checkErrors "context error parse" context]
+
+withHead :: Monad m => [a] -> (a -> m ()) -> m ()
+withHead (a:_) f = f a
+withHead _ _ = return ()
+
+tex e code = testCase ("tex" ++ show e) $ do
+ (exitCode, texLog, mPDF) <- runTex "pdftex" [] [] code
+ take 1 (map error' (texErrors texLog)) @?= [e]
+
+latexHeader, latexBye :: ByteString
+latexHeader = B.unlines
+ [ "\\documentclass{article}"
+ , "\\begin{document}"
+ ]
+latexBye = "\\end{document}"
+
+latex e code = testCase ("latex" ++ show e) $ do
+ (exitCode, texLog, mPDF) <- runTex "pdflatex" [] [] (latexHeader <> code)
+ head (map error' $ texErrors texLog) @?= e
+
+contextHeader, contextBye :: ByteString
+contextHeader = "\\starttext"
+contextBye = "\\stoptext"
+
+context e code = testCase ("context" ++ show e) $ do
+ (exitCode, texLog, mPDF) <- runTex "context" [] [] (contextHeader <> code)
+ take 1 (map error' (texErrors texLog)) @?= [e]
+ -- head (map error' $ texErrors texLog) @?= e
+ -- assertBool ("context" ++ show e) $ texLog `containsError` e
+
+-- Generating tex sample tex files -------------------------------------
+
+-- plain tex
+
+genTexFiles :: IO ()
+genTexFiles = for_ labeledErrors mkFile
+ where
+ mkFile (nm, (_err, xs)) = ifor_ xs $ \i x -> do
+ let doc = latexHeader <> x <> latexBye
+ name | length xs == 1 = nm
+ | otherwise = nm <> "-" <> show (i+1)
+ B.writeFile ("tests/samples/tex/" <> name <> ".tex") doc
+
+-- latex
+
+-- pdflatex -draftmode --interaction=nonstopmode $i
+
+genLatexFiles :: IO ()
+genLatexFiles = for_ labeledErrors mkFile
+ where
+ mkFile (nm, (_err, xs)) = ifor_ xs $ \i x -> do
+ let doc = latexHeader <> x <> latexBye
+ name | length xs == 1 = nm
+ | otherwise = nm <> "-" <> show (i+1)
+ B.writeFile ("tests/samples/latex/" <> name <> ".tex") doc
+
+-- context tex
+
+genContextFiles :: IO ()
+genContextFiles = for_ labeledErrors mkFile
+ where
+ mkFile (nm, (_err, xs)) = ifor_ xs $ \i x -> do
+ let doc = contextHeader <> x <> contextBye
+ name | length xs == 1 = nm
+ | otherwise = nm <> "-" <> show (i+1)
+ B.writeFile ("tests/samples/context/" <> name <> ".tex") doc
+
+labeledErrors =
+ [ ("missing-dollar", missingDollar)
+ , ("dimention-too-large", dimensionTooLarge)
+ , ("illegal-unit", illegalUnit)
+ , ("missing-number", missingNumber)
+ , ("undefined-control-sequence", undefinedControlSequence)
+ ]
+
+-- Checking error parsing ----------------------------------------------
+
+containsError :: TexLog -> TexError -> Bool
+containsError log (TexError _ err) = err `elem` map error' (texErrors log)
+
+checkError :: (TexError' -> ByteString -> F.Test) -> (TexError', [ByteString]) -> F.Test
+checkError f (e, codes) = testGroup (show e) $ map (f e) codes
+
+checkErrors :: TestName -> (TexError' -> ByteString -> F.Test) -> F.Test
+checkErrors name f = testGroup name $ map (checkError f) texErrs
+
+-- Sample errors -------------------------------------------------------
+
+texErrs =
+ [ missingDollar
+ , dimensionTooLarge
+ , illegalUnit
+ , missingNumber
+ , undefinedControlSequence
+ ]
+
+missingDollar = (,) (Missing '$')
+ [ "$x+1=2\n\n"
+ , "$$x+1=2\n\n"
+ ]
+
+dimensionTooLarge = (,) DimensionTooLarge
+ [ "\\hskip100000em"
+ ]
+
+illegalUnit = (,) IllegalUnit
+ [ "\\hskip1cn"
+ ]
+
+missingNumber = (,) MissingNumber
+ [ "\\hskip hi"
+ ]
+
+undefinedControlSequence = (,) (UndefinedControlSequence "\\hobx")
+ [ "\\hobx"
+ ]
+
+
+
+--
+-- missingDollarExample2= "x_1"
+--
+-- missingDollarExample3= "
+--
+-- numberTooBig = "10000000000"
+--
+-- overfull = "\\hbox to 1em{overfill box}"
+--
+-- underfill = "\\hbox to 20em{underfill box}"
+--
+-- illegalUnit = "\\hskip{1cn}"
+--
+-- undefinedControlSequence = "\\hobx"
+--
+-- missingNumber = "\\hskip"
+--
+--
+-- missingDollarTest = (texPutStrLn missingDollarExample, MissingDollar)
+--