Skip to content

Commit bbd7b60

Browse files
committed
Use Tasty.Golden for Docx reader tests.
This way we can update them with `--accept`.
1 parent e13aa5c commit bbd7b60

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

test/Tests/Readers/Docx.hs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,52 @@ Tests for the word docx reader.
1212
-}
1313
module Tests.Readers.Docx (tests) where
1414

15+
import Data.Algorithm.Diff (getDiff)
1516
import Codec.Archive.Zip
1617
import qualified Data.ByteString as BS
1718
import qualified Data.ByteString.Lazy as B
18-
import qualified Data.Map as M
1919
import qualified Data.Text as T
2020
import Data.Maybe
2121
import System.IO.Unsafe
2222
import Test.Tasty
23+
import Test.Tasty.Golden.Advanced
2324
import Test.Tasty.HUnit
2425
import Tests.Helpers
2526
import Text.Pandoc
2627
import qualified Text.Pandoc.Class as P
2728
import qualified Text.Pandoc.MediaBag as MB
2829
import Text.Pandoc.UTF8 as UTF8
2930

30-
-- We define a wrapper around pandoc that doesn't normalize in the
31-
-- tests. Since we do our own normalization, we want to make sure
32-
-- we're doing it right.
33-
34-
newtype NoNormPandoc = NoNormPandoc {unNoNorm :: Pandoc}
35-
deriving Show
36-
37-
noNorm :: Pandoc -> NoNormPandoc
38-
noNorm = NoNormPandoc
39-
4031
defopts :: ReaderOptions
4132
defopts = def{ readerExtensions = getDefaultExtensions "docx" }
4233

43-
instance ToString NoNormPandoc where
44-
toString d = T.unpack $ purely (writeNative def{ writerTemplate = s }) $ toPandoc d
45-
where s = case d of
46-
NoNormPandoc (Pandoc (Meta m) _)
47-
| M.null m -> Nothing
48-
| otherwise -> Just mempty -- need this to get meta output
49-
50-
instance ToPandoc NoNormPandoc where
51-
toPandoc = unNoNorm
34+
testCompare :: String -> FilePath -> FilePath -> TestTree
35+
testCompare = testCompareWithOpts defopts
5236

53-
compareOutput :: ReaderOptions
54-
-> FilePath
55-
-> FilePath
56-
-> IO (NoNormPandoc, NoNormPandoc)
57-
compareOutput opts docxFile nativeFile = do
58-
df <- B.readFile docxFile
59-
nf <- UTF8.toText <$> BS.readFile nativeFile
60-
p <- runIOorExplode $ readDocx opts df
61-
df' <- runIOorExplode $ readNative def nf
62-
return (noNorm p, noNorm df')
6337

64-
testCompareWithOptsIO :: ReaderOptions -> String -> FilePath -> FilePath -> IO TestTree
65-
testCompareWithOptsIO opts name docxFile nativeFile = do
66-
(dp, np) <- compareOutput opts docxFile nativeFile
67-
return $ test id name (dp, np)
38+
nativeDiff :: FilePath -> Pandoc -> Pandoc -> IO (Maybe String)
39+
nativeDiff normPath expectedNative actualNative
40+
| expectedNative == actualNative = return Nothing
41+
| otherwise = Just <$> do
42+
expected <- T.unpack <$> runIOorExplode (writeNative def expectedNative)
43+
actual <- T.unpack <$> runIOorExplode (writeNative def actualNative)
44+
let dash = replicate 72 '-'
45+
let diff = getDiff (lines actual) (lines expected)
46+
return $ '\n' : dash ++
47+
"\n--- " ++ normPath ++
48+
"\n+++ " ++ "test" ++ "\n" ++
49+
showDiff (1,1) diff ++ dash
6850

6951
testCompareWithOpts :: ReaderOptions -> String -> FilePath -> FilePath -> TestTree
70-
testCompareWithOpts opts name docxFile nativeFile =
71-
unsafePerformIO $ testCompareWithOptsIO opts name docxFile nativeFile
72-
73-
testCompare :: String -> FilePath -> FilePath -> TestTree
74-
testCompare = testCompareWithOpts defopts
52+
testCompareWithOpts opts testName docxFP nativeFP =
53+
goldenTest
54+
testName
55+
(do nf <- UTF8.toText <$> BS.readFile nativeFP
56+
runIOorExplode (readNative def nf))
57+
(do df <- B.readFile docxFP
58+
runIOorExplode (readDocx opts df))
59+
(nativeDiff nativeFP)
60+
(\a -> runIOorExplode (writeNative def a) >>= BS.writeFile nativeFP . UTF8.fromText)
7561

7662
testForWarningsWithOptsIO :: ReaderOptions -> String -> FilePath -> [String] -> IO TestTree
7763
testForWarningsWithOptsIO opts name docxFile expected = do

0 commit comments

Comments
 (0)