git.iko.soy/typeable/pandoc
on master help sign in
Docx writer tests: Use new golden framework

These are based off the reader tests, with some removed (where the reader output was identical, based on different docx inputs). There are still more to be added. In particular, tests for custom-styles need to be added. All golden docx files have been checked in MS Word 2013 (windows). There is no corruption. There is questionable output in the `tables` test: the three tables seemed to be joined. This will be addressed in a future commit, and the golden docx file will be changed.

commitb3449a84aa19edd3b31e2f49fddb924b54d99df6 authorJesse Rosenthal <jrosenthal@jhu.edu> dateFri 26 Jan 2018, 14:23 (9 years ago) parent9cf9f1f
27 files changed+120−127
test/Tests/Writers/Docx.hs+113−127 test/docx/golden/block_quotes.docx test/docx/golden/codeblock.docx test/docx/golden/comments.docx test/docx/golden/definition_list.docx test/docx/golden/headers.docx test/docx/golden/image.docx test/docx/golden/inline_code.docx test/docx/golden/inline_formatting.docx test/docx/golden/inline_images.docx test/docx/golden/link_in_notes.docx test/docx/golden/links.docx test/docx/golden/lists.docx test/docx/golden/lists_continuing.docx test/docx/golden/lists_restarting.docx test/docx/golden/nested_anchors_in_header.docx test/docx/golden/notes.docx test/docx/golden/table_one_row.docx test/docx/golden/table_with_list_cell.docx test/docx/golden/tables.docx test/docx/golden/track_changes_deletion.docx test/docx/golden/track_changes_insertion.docx test/docx/golden/track_changes_move.docx test/docx/golden/unicode.docx test/docx/golden/verbatim_subsuper.docx test/docx/image_writer_test.native+5 test/docx/inline_images_writer_test.native+2
test/Tests/Writers/Docx.hs+113−127
@@ -1,161 +1,147 @@
11module Tests.Writers.Docx (tests) where
22
3−import qualified Data.ByteString as BS
4−import System.FilePath ((</>))
5−import System.IO.Unsafe (unsafePerformIO)
3+import Text.Pandoc
64import Test.Tasty
7−import Tests.Helpers
8−import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
9−import Text.Pandoc.Definition
10−import Text.Pandoc.Options
11−import Text.Pandoc.Readers.Docx
12−import Text.Pandoc.Readers.Native
13−import qualified Text.Pandoc.UTF8 as UTF8
14−import Text.Pandoc.Walk
15−import Text.Pandoc.Writers.Docx
5+import Tests.Writers.OOXML
6+import Test.Tasty.HUnit
7+import Data.List (isPrefixOf)
168
17−type Options = (WriterOptions, ReaderOptions)
18−
19−compareOutput :: Options
20− -> FilePath
21− -> FilePath
22− -> IO (Pandoc, Pandoc)
23−compareOutput (wopts, ropts) nativeFileIn nativeFileOut = do
24− nf <- UTF8.toText <$> BS.readFile nativeFileIn
25− nf' <- UTF8.toText <$> BS.readFile nativeFileOut
26− runIOorExplode $ do
27− setUserDataDir $ Just (".." </> "data")
28− roundtripped <- readNative def nf >>=
29− writeDocx wopts >>= readDocx ropts
30− orig <- readNative def nf'
31− return (walk fixImages roundtripped, walk fixImages orig)
32−
33−-- make all image filenames "image", since otherwise round-trip
34−-- tests fail because of different behavior of Data.Unique in
35−-- different ghc versions...
36−fixImages :: Inline -> Inline
37−fixImages (Image attr alt (_,tit)) = Image attr alt ("image",tit)
38−fixImages x = x
39−
40−testCompareWithOptsIO :: Options -> String -> FilePath -> FilePath -> IO TestTree
41−testCompareWithOptsIO opts name nativeFileIn nativeFileOut = do
42− (dp, np) <- compareOutput opts nativeFileIn nativeFileOut
43− return $ test id name (dp, np)
44−
45−testCompareWithOpts :: Options -> String -> FilePath -> FilePath -> TestTree
46−testCompareWithOpts opts name nativeFileIn nativeFileOut =
47− unsafePerformIO $ testCompareWithOptsIO opts name nativeFileIn nativeFileOut
48−
49−roundTripCompareWithOpts :: Options -> String -> FilePath -> TestTree
50−roundTripCompareWithOpts opts name nativeFile =
51− testCompareWithOpts opts name nativeFile nativeFile
52−
53−-- testCompare :: String -> FilePath -> FilePath -> TestTree
54−-- testCompare = testCompareWithOpts def
55−
56−roundTripCompare :: String -> FilePath -> TestTree
57−roundTripCompare = roundTripCompareWithOpts def
9+-- we add an extra check to make sure that we're not writing in the
10+-- toplevel docx directory. We don't want to accidentally overwrite an
11+-- Word-generated docx file used to test the reader.
12+docxTest :: String -> WriterOptions -> FilePath -> FilePath -> TestTree
13+docxTest testName opts nativeFP goldenFP =
14+ if "docx/golden/" `isPrefixOf` goldenFP
15+ then ooxmlTest writeDocx testName opts nativeFP goldenFP
16+ else testCase testName $
17+ assertFailure $
18+ goldenFP ++ " is not in `test/docx/golden`"
5819
5920tests :: [TestTree]
6021tests = [ testGroup "inlines"
61− [ roundTripCompare
22+ [ docxTest
6223 "font formatting"
63− "docx/inline_formatting_writer.native"
64− , roundTripCompare
65− "font formatting with character styles"
66− "docx/char_styles.native"
67− , roundTripCompare
24+ def
25+ "docx/inline_formatting.native"
26+ "docx/golden/inline_formatting.docx"
27+ , docxTest
6828 "hyperlinks"
69− "docx/links_writer.native"
70− , roundTripCompare
29+ def
30+ "docx/links.native"
31+ "docx/golden/links.docx"
32+ , docxTest
7133 "inline image"
72− "docx/image_no_embed_writer.native"
73− , roundTripCompare
74− "inline image in links"
75− "docx/inline_images_writer.native"
76− , roundTripCompare
34+ def
35+ "docx/image_writer_test.native"
36+ "docx/golden/image.docx"
37+ , docxTest
38+ "inline images"
39+ def
40+ "docx/inline_images_writer_test.native"
41+ "docx/golden/inline_images.docx"
42+ , docxTest
7743 "handling unicode input"
44+ def
7845 "docx/unicode.native"
79− , roundTripCompare
80− "literal tabs"
81− "docx/tabs.native"
82− , roundTripCompare
83− "normalizing inlines"
84− "docx/normalize.native"
85− , roundTripCompare
86− "normalizing inlines deep inside blocks"
87− "docx/deep_normalize.native"
88− , roundTripCompare
89− "move trailing spaces outside of formatting"
90− "docx/trailing_spaces_in_formatting.native"
91− , roundTripCompare
92− "inline code (with VerbatimChar style)"
46+ "docx/golden/unicode.docx"
47+ , docxTest
48+ "inline code"
49+ def
9350 "docx/inline_code.native"
94− , roundTripCompare
51+ "docx/golden/inline_code.docx"
52+ , docxTest
9553 "inline code in subscript and superscript"
54+ def
9655 "docx/verbatim_subsuper.native"
56+ "docx/golden/verbatim_subsuper.docx"
9757 ]
9858 , testGroup "blocks"
99− [ roundTripCompare
59+ [ docxTest
10060 "headers"
61+ def
10162 "docx/headers.native"
102− , roundTripCompare
103− "headers already having auto identifiers"
104− "docx/already_auto_ident.native"
105− , roundTripCompare
106− "numbered headers automatically made into list"
107− "docx/numbered_header.native"
108− , roundTripCompare
109− "i18n blocks (headers and blockquotes)"
110− "docx/i18n_blocks.native"
111− -- Continuation does not survive round-trip
112− , roundTripCompare
63+ "docx/golden/headers.docx"
64+ , docxTest
65+ "nested anchor spans in header"
66+ def
67+ "docx/nested_anchors_in_header.native"
68+ "docx/golden/nested_anchors_in_header.docx"
69+ , docxTest
11370 "lists"
114− "docx/lists_writer.native"
115− , roundTripCompare
71+ def
72+ "docx/lists.native"
73+ "docx/golden/lists.docx"
74+ , docxTest
75+ "lists continuing after interruption"
76+ def
77+ "docx/lists_continuing.native"
78+ "docx/golden/lists_continuing.docx"
79+ , docxTest
80+ "lists restarting after interruption"
81+ def
82+ "docx/lists_restarting.native"
83+ "docx/golden/lists_restarting.docx"
84+ , docxTest
11685 "definition lists"
86+ def
11787 "docx/definition_list.native"
118− , roundTripCompare
119− "custom defined lists in styles"
120− "docx/german_styled_lists.native"
121− , roundTripCompare
88+ "docx/golden/definition_list.docx"
89+ , docxTest
12290 "footnotes and endnotes"
91+ def
12392 "docx/notes.native"
124− , roundTripCompare
125− "blockquotes (parsing indent as blockquote)"
93+ "docx/golden/notes.docx"
94+ , docxTest
95+ "links in footnotes and endnotes"
96+ def
97+ "docx/link_in_notes.native"
98+ "docx/golden/link_in_notes.docx"
99+ , docxTest
100+ "blockquotes"
101+ def
126102 "docx/block_quotes_parse_indent.native"
127− , roundTripCompare
128− "hanging indents"
129− "docx/hanging_indent.native"
130− -- tables headers do not survive round-trip, should look into that
131− , roundTripCompare
103+ "docx/golden/block_quotes.docx"
104+ , docxTest
132105 "tables"
106+ def
133107 "docx/tables.native"
134− , roundTripCompare
108+ "docx/golden/tables.docx"
109+ , docxTest
135110 "tables with lists in cells"
111+ def
136112 "docx/table_with_list_cell.native"
137− , roundTripCompare
113+ "docx/golden/table_with_list_cell.docx"
114+ , docxTest
115+ "tables with one row"
116+ def
117+ "docx/table_one_row.native"
118+ "docx/golden/table_one_row.docx"
119+ , docxTest
138120 "code block"
121+ def
139122 "docx/codeblock.native"
140− , roundTripCompare
141− "dropcap paragraphs"
142− "docx/drop_cap.native"
123+ "docx/golden/codeblock.docx"
143124 ]
144− , testGroup "metadata"
145− [ roundTripCompareWithOpts (def,def{readerStandalone=True})
146− "metadata fields"
147− "docx/metadata.native"
148− , roundTripCompareWithOpts (def,def{readerStandalone=True})
149− "stop recording metadata with normal text"
150− "docx/metadata_after_normal.native"
125+ , testGroup "track changes"
126+ [ docxTest
127+ "insertion"
128+ def
129+ "docx/track_changes_insertion_all.native"
130+ "docx/golden/track_changes_insertion.docx"
131+ , docxTest
132+ "deletion"
133+ def
134+ "docx/track_changes_deletion_all.native"
135+ "docx/golden/track_changes_deletion.docx"
136+ , docxTest
137+ "move text"
138+ def
139+ "docx/track_changes_move_all.native"
140+ "docx/golden/track_changes_move.docx"
141+ , docxTest
142+ "comments"
143+ def
144+ "docx/comments.native"
145+ "docx/golden/comments.docx"
151146 ]
152− , testGroup "customized styles"
153− [ testCompareWithOpts
154− ( def{writerReferenceDoc=Just "docx/custom-style-reference.docx"}
155− , def)
156− "simple customized blocks and inlines"
157− "docx/custom-style-roundtrip-start.native"
158− "docx/custom-style-roundtrip-end.native"
159− ]
160−
161147 ]
test/docx/golden/block_quotes.docxnew file

Binary file not shown.

test/docx/golden/codeblock.docxnew file

Binary file not shown.

test/docx/golden/comments.docxnew file

Binary file not shown.

test/docx/golden/definition_list.docxnew file

Binary file not shown.

test/docx/golden/headers.docxnew file

Binary file not shown.

test/docx/golden/image.docxnew file

Binary file not shown.

test/docx/golden/inline_code.docxnew file

Binary file not shown.

test/docx/golden/inline_formatting.docxnew file

Binary file not shown.

test/docx/golden/inline_images.docxnew file

Binary file not shown.

test/docx/golden/link_in_notes.docxnew file

Binary file not shown.

test/docx/golden/links.docxnew file

Binary file not shown.

test/docx/golden/lists.docxnew file

Binary file not shown.

test/docx/golden/lists_continuing.docxnew file

Binary file not shown.

test/docx/golden/lists_restarting.docxnew file

Binary file not shown.

test/docx/golden/nested_anchors_in_header.docxnew file

Binary file not shown.

test/docx/golden/notes.docxnew file

Binary file not shown.

test/docx/golden/table_one_row.docxnew file

Binary file not shown.

test/docx/golden/table_with_list_cell.docxnew file

Binary file not shown.

test/docx/golden/tables.docxnew file

Binary file not shown.

test/docx/golden/track_changes_deletion.docxnew file

Binary file not shown.

test/docx/golden/track_changes_insertion.docxnew file

Binary file not shown.

test/docx/golden/track_changes_move.docxnew file

Binary file not shown.

test/docx/golden/unicode.docxnew file

Binary file not shown.

test/docx/golden/verbatim_subsuper.docxnew file

Binary file not shown.

test/docx/image_writer_test.nativenew file+5
@@ -0,0 +1,5 @@
1+Pandoc (Meta {unMeta = fromList []})
2+[Para [Image ("",[],[]) [] ("lalune.jpg","")]
3+,Para [Image ("",[],[]) [Str "The",Space,Str "Moon"] ("lalune.jpg","fig:")]
4+,Header 1 ("one-more",[],[]) [Str "One",Space,Str "More"]
5+,Para [Image ("",[],[]) [Str "The",Space,Str "Moon"] ("lalune.jpg","fig:")]]
test/docx/inline_images_writer_test.nativenew file+2
@@ -0,0 +1,2 @@
1+[Para [Str "This",Space,Str "picture",Space,Image ("",[],[("width","0.8888888888888888in"),("height","0.8888888888888888in")]) [Str "This",Space,Str "one",Space,Str "is",Space,Str "green",Space,Str "and",Space,Str "looks",Space,Str "like",Space,Str "Sideshow",Space,Str "Bob."] ("lalune.jpg","First identicon"),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2+,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[("width","0.8888888888888888in"),("height","0.8888888888888888in")]) [Str "This",Space,Str "one",Space,Str "is",Space,Str "reddish,",Space,Str "and",Space,Str "looks",Space,Str "like",Space,Str "a",Space,Str "heart",Space,Str "that",Space,Str "has",Space,Str "leaked",Space,Str "out."] ("lalune.jpg","Second identicon"),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]