git.iko.soy/typeable/pandoc
on master help sign in
FB2 reader test: better diagnostics on failure.
c65af7dJohn MacFarlane
test/Tests/Readers/FB2.hs+28−1
@@ -15,13 +15,40 @@ module Tests.Readers.FB2 (tests) where
15import Prelude
15import Prelude
16import Test.Tasty
16import Test.Tasty
17import Tests.Helpers
17import Tests.Helpers
18−import Test.Tasty.Golden (goldenVsString)
18+-- import Test.Tasty.Golden (goldenVsString)
19+import Test.Tasty.Golden.Advanced (goldenTest)
20+import Text.Printf
21+import qualified Data.ByteString.Lazy as LBS
22+
19import qualified Data.ByteString as BS
23import qualified Data.ByteString as BS
20import Text.Pandoc
24import Text.Pandoc
21import Text.Pandoc.UTF8 (toText, fromStringLazy)
25import Text.Pandoc.UTF8 (toText, fromStringLazy)
22import Data.Text (Text, unpack)
26import Data.Text (Text, unpack)
23import System.FilePath (replaceExtension)
27import System.FilePath (replaceExtension)
24
28
29+simpleCmp :: Eq a => String -> a -> a -> IO (Maybe String)
30+simpleCmp e x y =
31+ return $ if x == y then Nothing else Just e
32+
33+
34+goldenVsString
35+ :: TestName -- ^ test name
36+ -> FilePath -- ^ path to the «golden» file (the file that contains correct output)
37+ -> IO LBS.ByteString -- ^ action that returns a string
38+ -> TestTree -- ^ the test verifies that the returned string is the same as the golden file contents
39+goldenVsString name ref act =
40+ goldenTest
41+ name
42+ (BS.readFile ref)
43+ (LBS.toStrict <$> act)
44+ cmp
45+ upd
46+ where
47+ cmp x y = simpleCmp msg x y
48+ where
49+ msg = printf "Test output from %s was different from %s. It was: %s" ref (show x) (show y)
50+ upd = BS.writeFile ref
51+
25fb2ToNative :: Text -> Text
52fb2ToNative :: Text -> Text
26fb2ToNative = purely (writeNative def{ writerTemplate = Just mempty }) . purely (readFB2 def)
53fb2ToNative = purely (writeNative def{ writerTemplate = Just mempty }) . purely (readFB2 def)
27
54