summaryrefslogtreecommitdiff
path: root/tests/Tex/LogParse.hs
blob: 5cef0b28fbd9a43e51adfac28657894429852756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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

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)
--