2011-01-22 05:50:18 +01:00
{- # LANGUAGE OverloadedStrings # -}
2011-01-12 19:10:56 +01:00
module Tests.Readers.LaTeX ( tests ) where
2011-01-12 14:16:35 +01:00
import Text.Pandoc.Definition
import Test.Framework
2011-01-13 19:59:44 +01:00
import Tests.Helpers
2011-01-22 22:40:18 +01:00
import Tests.Arbitrary ()
2011-01-19 08:34:34 +01:00
import Text.Pandoc.Builder
import Text.Pandoc
2015-03-08 15:17:09 +01:00
import Data.Monoid ( mempty )
2015-02-18 20:57:48 +01:00
import Text.Pandoc.Error
2011-01-19 08:34:34 +01:00
2011-01-22 05:50:18 +01:00
latex :: String -> Pandoc
2015-02-18 20:57:48 +01:00
latex = handleError . readLaTeX def
2011-01-22 05:50:18 +01:00
2012-02-05 22:23:06 +01:00
infix 4 =:
2011-01-22 05:50:18 +01:00
( =: ) :: ToString c
=> String -> ( String , c ) -> Test
( =: ) = test latex
2011-01-12 14:16:35 +01:00
2015-03-08 15:17:09 +01:00
simpleTable' :: [ Alignment ] -> [ [ Blocks ] ] -> Blocks
simpleTable' aligns = table " " ( zip aligns ( repeat 0.0 ) )
( map ( const mempty ) aligns )
2011-01-12 14:16:35 +01:00
tests :: [ Test ]
2011-01-13 20:11:55 +01:00
tests = [ testGroup " basic "
2011-01-19 08:34:34 +01:00
[ " simple " =:
2011-01-23 01:01:14 +01:00
" word " =?> para " word "
2011-01-19 08:34:34 +01:00
, " space " =:
2013-12-20 02:43:25 +01:00
" some text " =?> para " some text "
2011-01-19 08:34:34 +01:00
, " emphasized " =:
2011-01-22 05:50:18 +01:00
" \ \ emph{emphasized} " =?> para ( emph " emphasized " )
2011-01-13 20:11:55 +01:00
]
, testGroup " headers "
2011-01-19 08:34:34 +01:00
[ " level 1 " =:
2013-09-01 18:13:31 +02:00
" \ \ section{header} " =?> headerWith ( " header " , [] , [] ) 1 " header "
2011-01-19 08:34:34 +01:00
, " level 2 " =:
2013-09-01 18:13:31 +02:00
" \ \ subsection{header} " =?> headerWith ( " header " , [] , [] ) 2 " header "
2011-01-19 08:34:34 +01:00
, " level 3 " =:
2013-09-01 18:13:31 +02:00
" \ \ subsubsection{header} " =?> headerWith ( " header " , [] , [] ) 3 " header "
2011-01-19 08:34:34 +01:00
, " emph " =:
2011-01-22 05:50:18 +01:00
" \ \ section{text \ \ emph{emph}} " =?>
2013-09-01 18:13:31 +02:00
headerWith ( " text-emph " , [] , [] ) 1 ( " text " <> space <> emph " emph " )
2011-01-19 08:34:34 +01:00
, " link " =:
2011-01-22 05:50:18 +01:00
" \ \ section{text \ \ href{/url}{link}} " =?>
2013-09-01 18:13:31 +02:00
headerWith ( " text-link " , [] , [] ) 1 ( " text " <> space <> link " /url " " " " link " )
2011-01-13 20:11:55 +01:00
]
2011-01-29 17:47:00 +01:00
2012-02-08 04:24:09 +01:00
, testGroup " math "
[ " escaped $ " =:
2012-02-08 04:24:09 +01:00
" $x= \ \ $4$ " =?> para ( math " x= \ \ $4 " )
2012-02-08 04:24:09 +01:00
]
2011-01-30 17:21:48 +01:00
, testGroup " space and comments "
[ " blank lines + space at beginning " =:
" \ n \ n hi " =?> para " hi "
, " blank lines + space + comments " =:
" % my comment \ n \ n \ n % another \ n \ n hi " =?> para " hi "
, " comment in paragraph " =:
" hi % this is a comment \ n there \ n " =?> para " hi there "
]
2013-08-22 20:15:36 +02:00
, testGroup " code blocks "
[ " identifier " =:
" \ \ begin{lstlisting}[label=test] \ \ end{lstlisting} " =?> codeBlockWith ( " test " , [] , [ ( " label " , " test " ) ] ) " "
, " no identifier " =:
" \ \ begin{lstlisting} \ \ end{lstlisting} " =?> codeBlock " "
]
2015-03-08 15:17:09 +01:00
, testGroup " tables "
[ " Single cell table " =:
" \ \ begin{tabular}{|l|}Test \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignLeft ] [ [ plain " Test " ] ]
, " Multi cell table " =:
" \ \ begin{tabular}{|rl|}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignRight , AlignLeft ] [ [ plain " One " , plain " Two " ] ]
, " Multi line table " =:
unlines [ " \ \ begin{tabular}{|c|} "
, " One \ \ \ \ "
, " Two \ \ \ \ "
, " Three \ \ \ \ "
, " \ \ end{tabular} " ] =?>
simpleTable' [ AlignCenter ]
[ [ plain " One " ] , [ plain " Two " ] , [ plain " Three " ] ]
, " Empty table " =:
" \ \ begin{tabular}{} \ \ end{tabular} " =?>
simpleTable' [] []
, " Table with fixed column width " =:
" \ \ begin{tabular}{|p{5cm}r|}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignLeft , AlignRight ] [ [ plain " One " , plain " Two " ] ]
, " Table with empty column separators " =:
" \ \ begin{tabular}{@{}r@{}l}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignRight , AlignLeft ] [ [ plain " One " , plain " Two " ] ]
2015-03-08 15:47:39 +01:00
, " Table with custom column separators " =:
unlines [ " \ \ begin{tabular}{@{($ \ \ to$)}r@{ \ \ hspace{2cm}}l} "
, " One&Two \ \ \ \ "
, " \ \ end{tabular} " ] =?>
simpleTable' [ AlignRight , AlignLeft ] [ [ plain " One " , plain " Two " ] ]
2015-03-08 15:30:05 +01:00
, " Table with vertical alignment argument " =:
" \ \ begin{tabular}[t]{r|r}One & Two \ \ \ \ \ \ end{tabular} " =?>
simpleTable' [ AlignRight , AlignRight ] [ [ plain " One " , plain " Two " ] ]
2015-03-08 15:17:09 +01:00
]
2011-01-29 17:47:00 +01:00
, testGroup " citations "
[ natbibCitations
, biblatexCitations
]
2015-04-13 02:22:39 +02:00
, let hex = [ '0' .. '9' ] ++ [ 'a' .. 'f' ] in
testGroup " Character Escapes "
[ " Two-character escapes " =:
concat [ " ^^ " ++ [ i , j ] | i <- hex , j <- hex ] =?>
para ( str [ '\ 0 '.. '\ 255 ' ] )
, " One-character escapes " =:
concat [ " ^^ " ++ [ i ] | i <- hex ] =?>
para ( str $ [ 'p' .. 'y' ] ++ [ '!' .. '&' ] )
]
2011-01-12 14:16:35 +01:00
]
2011-01-29 17:47:00 +01:00
baseCitation :: Citation
2012-10-22 08:16:23 +02:00
baseCitation = Citation { citationId = " item1 "
, citationPrefix = []
, citationSuffix = []
, citationMode = AuthorInText
, citationNoteNum = 0
2012-10-23 04:11:56 +02:00
, citationHash = 0
2012-10-28 17:36:15 +01:00
}
2011-01-29 17:47:00 +01:00
2012-02-06 21:41:34 +01:00
rt :: String -> Inlines
2013-03-17 16:48:29 +01:00
rt = rawInline " latex "
2012-02-06 21:41:34 +01:00
2011-01-29 17:47:00 +01:00
natbibCitations :: Test
natbibCitations = testGroup " natbib "
[ " citet " =: " \ \ citet{item1} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation ] ( rt " \ \ citet{item1} " ) )
2011-01-29 17:47:00 +01:00
, " suffix " =: " \ \ citet[p.~30]{item1} "
=?> para
2013-07-21 20:44:49 +02:00
( cite [ baseCitation { citationSuffix = toList $ text " p. \ 160 \ & 30 " } ] ( rt " \ \ citet[p.~30]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " suffix long " =: " \ \ citet[p.~30, with suffix]{item1} "
=?> para ( cite [ baseCitation { citationSuffix =
2013-07-21 20:44:49 +02:00
toList $ text " p. \ 160 \ & 30, with suffix " } ] ( rt " \ \ citet[p.~30, with suffix]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " multiple " =: " \ \ citeauthor{item1} \ \ citetext{ \ \ citeyear{item1}; \ \ citeyear[p.~30]{item2}; \ \ citealp[see also][]{item3}} "
=?> para ( cite [ baseCitation { citationMode = AuthorInText }
, baseCitation { citationMode = SuppressAuthor
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 30 " ]
2011-01-29 17:47:00 +01:00
, citationId = " item2 " }
, baseCitation { citationId = " item3 "
, citationPrefix = [ Str " see " , Space , Str " also " ]
, citationMode = NormalCitation }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ citetext{ \ \ citeyear{item1}; \ \ citeyear[p.~30]{item2}; \ \ citealp[see also][]{item3}} " ) )
2011-01-29 17:47:00 +01:00
, " group " =: " \ \ citetext{ \ \ citealp[see][p.~34--35]{item1}; \ \ citealp[also][chap. 3]{item3}} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Str " see " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 34 \ 8211 \ & 35 " ] }
2011-01-29 17:47:00 +01:00
, baseCitation { citationMode = NormalCitation
, citationId = " item3 "
, citationPrefix = [ Str " also " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " chap. " , Space , Str " 3 " ] }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ citetext{ \ \ citealp[see][p.~34--35]{item1}; \ \ citealp[also][chap. 3]{item3}} " ) )
2011-01-29 17:47:00 +01:00
, " suffix and locator " =: " \ \ citep[pp.~33, 35--37, and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " pp. \ 160 \ & 33, " , Space , Str " 35 \ 8211 \ & 37, " , Space , Str " and " , Space , Str " nowhere " , Space , Str " else " ] } ] ( rt " \ \ citep[pp.~33, 35--37, and nowhere else]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " suffix only " =: " \ \ citep[and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = toList $ text " and nowhere else " } ] ( rt " \ \ citep[and nowhere else]{item1} " ) )
2011-01-29 17:47:00 +01:00
, " no author " =: " \ \ citeyearpar{item1}, and now Doe with a locator \ \ citeyearpar[p.~44]{item2} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation { citationMode = SuppressAuthor } ] ( rt " \ \ citeyearpar{item1} " ) <>
2011-12-13 23:29:07 +01:00
text " , and now Doe with a locator " <>
2011-01-29 17:47:00 +01:00
cite [ baseCitation { citationMode = SuppressAuthor
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 44 " ]
2012-02-06 21:41:34 +01:00
, citationId = " item2 " } ] ( rt " \ \ citeyearpar[p.~44]{item2} " ) )
2011-01-29 17:47:00 +01:00
, " markup " =: " \ \ citep[ \ \ emph{see}][p. \ \ textbf{32}]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Emph [ Str " see " ] ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. " , Space ,
2012-02-06 21:41:34 +01:00
Strong [ Str " 32 " ] ] } ] ( rt " \ \ citep[ \ \ emph{see}][p. \ \ textbf{32}]{item1} " ) )
2011-01-29 17:47:00 +01:00
]
biblatexCitations :: Test
biblatexCitations = testGroup " biblatex "
2011-01-29 18:01:30 +01:00
[ " textcite " =: " \ \ textcite{item1} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation ] ( rt " \ \ textcite{item1} " ) )
2011-01-29 18:01:30 +01:00
, " suffix " =: " \ \ textcite[p.~30]{item1} "
=?> para
2013-07-21 20:44:49 +02:00
( cite [ baseCitation { citationSuffix = toList $ text " p. \ 160 \ & 30 " } ] ( rt " \ \ textcite[p.~30]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " suffix long " =: " \ \ textcite[p.~30, with suffix]{item1} "
=?> para ( cite [ baseCitation { citationSuffix =
2013-07-21 20:44:49 +02:00
toList $ text " p. \ 160 \ & 30, with suffix " } ] ( rt " \ \ textcite[p.~30, with suffix]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " multiple " =: " \ \ textcites{item1}[p.~30]{item2}[see also][]{item3} "
=?> para ( cite [ baseCitation { citationMode = AuthorInText }
, baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 30 " ]
2011-01-29 18:01:30 +01:00
, citationId = " item2 " }
, baseCitation { citationId = " item3 "
, citationPrefix = [ Str " see " , Space , Str " also " ]
, citationMode = NormalCitation }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ textcites{item1}[p.~30]{item2}[see also][]{item3} " ) )
2011-01-29 18:01:30 +01:00
, " group " =: " \ \ autocites[see][p.~34--35]{item1}[also][chap. 3]{item3} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Str " see " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 34 \ 8211 \ & 35 " ] }
2011-01-29 18:01:30 +01:00
, baseCitation { citationMode = NormalCitation
, citationId = " item3 "
, citationPrefix = [ Str " also " ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " chap. " , Space , Str " 3 " ] }
2012-02-06 21:41:34 +01:00
] ( rt " \ \ autocites[see][p.~34--35]{item1}[also][chap. 3]{item3} " ) )
2011-01-29 18:01:30 +01:00
, " suffix and locator " =: " \ \ autocite[pp.~33, 35--37, and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " pp. \ 160 \ & 33, " , Space , Str " 35 \ 8211 \ & 37, " , Space , Str " and " , Space , Str " nowhere " , Space , Str " else " ] } ] ( rt " \ \ autocite[pp.~33, 35--37, and nowhere else]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " suffix only " =: " \ \ autocite[and nowhere else]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
2013-07-21 20:44:49 +02:00
, citationSuffix = toList $ text " and nowhere else " } ] ( rt " \ \ autocite[and nowhere else]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " no author " =: " \ \ autocite*{item1}, and now Doe with a locator \ \ autocite*[p.~44]{item2} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation { citationMode = SuppressAuthor } ] ( rt " \ \ autocite*{item1} " ) <>
2011-12-13 23:29:07 +01:00
text " , and now Doe with a locator " <>
2011-01-29 18:01:30 +01:00
cite [ baseCitation { citationMode = SuppressAuthor
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. \ 160 \ & 44 " ]
2012-02-06 21:41:34 +01:00
, citationId = " item2 " } ] ( rt " \ \ autocite*[p.~44]{item2} " ) )
2011-01-29 18:01:30 +01:00
, " markup " =: " \ \ autocite[ \ \ emph{see}][p. \ \ textbf{32}]{item1} "
=?> para ( cite [ baseCitation { citationMode = NormalCitation
, citationPrefix = [ Emph [ Str " see " ] ]
2013-07-21 20:44:49 +02:00
, citationSuffix = [ Str " p. " , Space ,
2012-02-06 21:41:34 +01:00
Strong [ Str " 32 " ] ] } ] ( rt " \ \ autocite[ \ \ emph{see}][p. \ \ textbf{32}]{item1} " ) )
2011-01-29 18:01:30 +01:00
, " parencite " =: " \ \ parencite{item1} "
2012-02-06 21:41:34 +01:00
=?> para ( cite [ baseCitation { citationMode = NormalCitation } ] ( rt " \ \ parencite{item1} " ) )
2011-01-29 17:47:00 +01:00
]