@@ -15,13 +15,40 @@ module Tests.Readers.FB2 (tests) where
1515import Prelude
1616import Test.Tasty
1717import 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+
1923import qualified Data.ByteString as BS
2024import Text.Pandoc
2125import Text.Pandoc.UTF8 (toText, fromStringLazy)
2226import Data.Text (Text, unpack)
2327import System.FilePath (replaceExtension)
2428
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+
2552fb2ToNative :: Text -> Text
2653fb2ToNative = purely (writeNative def{ writerTemplate = Just mempty }) . purely (readFB2 def)
2754