git.iko.soy/typeable/pandoc
on master help sign in
Docx writer tests: Use new golden framework
b3449a8Jesse Rosenthal
test/Tests/Writers/Docx.hs+113−127
@@ -1,161 +1,147 @@
1module Tests.Writers.Docx (tests) where
1module Tests.Writers.Docx (tests) where
2
2
3−import qualified Data.ByteString as BS
3+import Text.Pandoc
4−import System.FilePath ((</>))
5−import System.IO.Unsafe (unsafePerformIO)
6import Test.Tasty
4import Test.Tasty
7−import Tests.Helpers
5+import Tests.Writers.OOXML
8−import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
6+import Test.Tasty.HUnit
9−import Text.Pandoc.Definition
7+import Data.List (isPrefixOf)
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
16
8
17−type Options = (WriterOptions, ReaderOptions)
9+-- we add an extra check to make sure that we're not writing in the
18−
10+-- toplevel docx directory. We don't want to accidentally overwrite an
19−compareOutput :: Options
11+-- Word-generated docx file used to test the reader.
20− -> FilePath
12+docxTest :: String -> WriterOptions -> FilePath -> FilePath -> TestTree
21− -> FilePath
13+docxTest testName opts nativeFP goldenFP =
22− -> IO (Pandoc, Pandoc)
14+ if "docx/golden/" `isPrefixOf` goldenFP
23−compareOutput (wopts, ropts) nativeFileIn nativeFileOut = do
15+ then ooxmlTest writeDocx testName opts nativeFP goldenFP
24− nf <- UTF8.toText <$> BS.readFile nativeFileIn
16+ else testCase testName $
25− nf' <- UTF8.toText <$> BS.readFile nativeFileOut
17+ assertFailure $
26− runIOorExplode $ do
18+ goldenFP ++ " is not in `test/docx/golden`"
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
58
19
59tests :: [TestTree]
20tests :: [TestTree]
60tests = [ testGroup "inlines"
21tests = [ testGroup "inlines"
61− [ roundTripCompare
22+ [ docxTest
62 "font formatting"
23 "font formatting"
63− "docx/inline_formatting_writer.native"
24+ def
64− , roundTripCompare
25+ "docx/inline_formatting.native"
65− "font formatting with character styles"
26+ "docx/golden/inline_formatting.docx"
66− "docx/char_styles.native"
27+ , docxTest
67− , roundTripCompare
68 "hyperlinks"
28 "hyperlinks"
29+ def
69− "docx/links_writer.native"
30+ "docx/links.native"
70− , roundTripCompare
31+ "docx/golden/links.docx"
32+ , docxTest
71 "inline image"
33 "inline image"
72− "docx/image_no_embed_writer.native"
34+ def
73− , roundTripCompare
35+ "docx/image_writer_test.native"
36+ "docx/golden/image.docx"
37+ , docxTest
74− "inline image in links"
38+ "inline images"
75− "docx/inline_images_writer.native"
39+ def
76− , roundTripCompare
40+ "docx/inline_images_writer_test.native"
41+ "docx/golden/inline_images.docx"
42+ , docxTest
77 "handling unicode input"
43 "handling unicode input"
44+ def
78 "docx/unicode.native"
45 "docx/unicode.native"
79− , roundTripCompare
46+ "docx/golden/unicode.docx"
80− "literal tabs"
47+ , docxTest
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)"
48+ "inline code"
49+ def
93 "docx/inline_code.native"
50 "docx/inline_code.native"
94− , roundTripCompare
51+ "docx/golden/inline_code.docx"
52+ , docxTest
95 "inline code in subscript and superscript"
53 "inline code in subscript and superscript"
54+ def
96 "docx/verbatim_subsuper.native"
55 "docx/verbatim_subsuper.native"
56+ "docx/golden/verbatim_subsuper.docx"
97 ]
57 ]
98 , testGroup "blocks"
58 , testGroup "blocks"
99− [ roundTripCompare
59+ [ docxTest
100 "headers"
60 "headers"
61+ def
101 "docx/headers.native"
62 "docx/headers.native"
102− , roundTripCompare
63+ "docx/golden/headers.docx"
103− "headers already having auto identifiers"
64+ , docxTest
104− "docx/already_auto_ident.native"
65+ "nested anchor spans in header"
105− , roundTripCompare
66+ def
106− "numbered headers automatically made into list"
67+ "docx/nested_anchors_in_header.native"
107− "docx/numbered_header.native"
68+ "docx/golden/nested_anchors_in_header.docx"
108− , roundTripCompare
69+ , docxTest
109− "i18n blocks (headers and blockquotes)"
110− "docx/i18n_blocks.native"
111− -- Continuation does not survive round-trip
112− , roundTripCompare
113 "lists"
70 "lists"
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
114− "docx/lists_writer.native"
82+ "docx/lists_restarting.native"
115− , roundTripCompare
83+ "docx/golden/lists_restarting.docx"
84+ , docxTest
116 "definition lists"
85 "definition lists"
86+ def
117 "docx/definition_list.native"
87 "docx/definition_list.native"
118− , roundTripCompare
88+ "docx/golden/definition_list.docx"
119− "custom defined lists in styles"
89+ , docxTest
120− "docx/german_styled_lists.native"
121− , roundTripCompare
122 "footnotes and endnotes"
90 "footnotes and endnotes"
91+ def
123 "docx/notes.native"
92 "docx/notes.native"
124− , roundTripCompare
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
125− "blockquotes (parsing indent as blockquote)"
100+ "blockquotes"
101+ def
126 "docx/block_quotes_parse_indent.native"
102 "docx/block_quotes_parse_indent.native"
127− , roundTripCompare
103+ "docx/golden/block_quotes.docx"
128− "hanging indents"
104+ , docxTest
129− "docx/hanging_indent.native"
130− -- tables headers do not survive round-trip, should look into that
131− , roundTripCompare
132 "tables"
105 "tables"
106+ def
133 "docx/tables.native"
107 "docx/tables.native"
134− , roundTripCompare
108+ "docx/golden/tables.docx"
109+ , docxTest
135 "tables with lists in cells"
110 "tables with lists in cells"
111+ def
136 "docx/table_with_list_cell.native"
112 "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
138 "code block"
120 "code block"
121+ def
139 "docx/codeblock.native"
122 "docx/codeblock.native"
140− , roundTripCompare
123+ "docx/golden/codeblock.docx"
141− "dropcap paragraphs"
142− "docx/drop_cap.native"
143 ]
124 ]
144− , testGroup "metadata"
125+ , testGroup "track changes"
145− [ roundTripCompareWithOpts (def,def{readerStandalone=True})
126+ [ docxTest
146− "metadata fields"
127+ "insertion"
147− "docx/metadata.native"
128+ def
148− , roundTripCompareWithOpts (def,def{readerStandalone=True})
129+ "docx/track_changes_insertion_all.native"
149− "stop recording metadata with normal text"
130+ "docx/golden/track_changes_insertion.docx"
150− "docx/metadata_after_normal.native"
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"
151 ]
146 ]
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−
161 ]
147 ]
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."]]