Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.

Commit d2c9fc8

Browse files
committed
Complete switch to Text for test-citeproc.
1 parent 26424ee commit d2c9fc8

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

‎tests/test-citeproc.hs‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import Text.Pandoc (Block (..), Format (..), Inline (..),
2828
Pandoc (..), bottomUp, nullMeta)
2929
import qualified Text.Pandoc.UTF8 as UTF8
3030
import Text.Printf
31+
import qualified Data.Text as T
32+
import Data.Text (Text)
3133

3234
data TestCase = TestCase{
3335
testMode :: Mode -- mode
@@ -37,7 +39,7 @@ data TestCase = TestCase{
3739
, testCsl :: Style -- csl
3840
, testAbbreviations :: Abbreviations -- abbreviations
3941
, testReferences :: [Reference] -- input
40-
, testResult :: String -- result
42+
, testResult :: Text -- result
4143
} deriving (Show)
4244

4345
data Mode = CitationMode
@@ -119,10 +121,10 @@ runTest path = E.handle (handler path) $ do
119121
let expected = adjustEntities $ fixBegins $ trimEnd $ testResult testCase
120122
let mode = testMode testCase
121123
let assemble BibliographyMode xs =
122-
"<div class=\"csl-bib-body\">\n" ++
123-
unlines (map (\x -> " <div class=\"csl-entry\">" ++ x ++
124-
"</div>") xs) ++ "</div>\n"
125-
assemble _ xs = unlines xs
124+
"<div class=\"csl-bib-body\">\n" <>
125+
T.unlines (map (\x -> " <div class=\"csl-entry\">" <> x <>
126+
"</div>") xs) <> "</div>\n"
127+
assemble _ xs = T.unlines xs
126128
case mode of
127129
BibliographyHeaderMode -> do
128130
putStrLn $ "[SKIPPED] " ++ path ++ "\n"
@@ -141,35 +143,35 @@ runTest path = E.handle (handler path) $ do
141143
return Passed
142144
else do
143145
putStrLn $ "[FAILED] " ++ path
144-
showDiff expected result
146+
showDiff (T.unpack expected) (T.unpack result)
145147
putStrLn ""
146148
return Failed
147149

148-
trimEnd :: String -> String
149-
trimEnd = reverse . ('\n':) . dropWhile isSpace . reverse
150+
trimEnd :: Text -> Text
151+
trimEnd t = T.stripEnd t <> "\n"
150152

151153
-- this is designed to mimic the test suite's output:
152-
inlinesToString :: [Inline] -> String
154+
inlinesToString :: [Inline] -> Text
153155
inlinesToString ils =
154156
writeHtmlString
155157
$ bottomUp (concatMap adjustSpans)
156158
$ Pandoc nullMeta [Plain ils]
157159

158160
-- We want &amp; instead of &#38; etc.
159-
adjustEntities :: String -> String
160-
adjustEntities ('&':'#':'3':'8':';':xs) = "&amp;" ++ adjustEntities xs
161-
adjustEntities (x:xs) = x : adjustEntities xs
162-
adjustEntities [] = []
161+
adjustEntities :: Text -> Text
162+
adjustEntities = T.replace "&#38;" "&amp;"
163163

164164
-- citeproc-js test suite expects "citations" to be formatted like
165165
-- .. [0] Smith (2007)
166166
-- >> [1] Jones (2008)
167167
-- To get a meaningful comparison, we remove this.
168-
fixBegins :: String -> String
169-
fixBegins = unlines . map fixLine . lines
170-
where fixLine ('.':'.':'[':xs) = dropWhile isSpace $ dropWhile (not . isSpace) xs
171-
fixLine ('>':'>':'[':xs) = dropWhile isSpace $ dropWhile (not . isSpace) xs
172-
fixLine xs = xs
168+
fixBegins :: Text -> Text
169+
fixBegins = T.unlines . map fixLine . T.lines
170+
where fixLine t =
171+
case T.stripPrefix "..[" t `mplus` T.stripPrefix ">>[" t of
172+
Just rest ->
173+
T.dropWhile isSpace . T.dropWhile (not . isSpace) $ rest
174+
Nothing -> t
173175

174176
-- adjust the spans so we fit what the test suite expects.
175177
adjustSpans :: Inline -> [Inline]

0 commit comments

Comments
 (0)