Debian Codegraph @

tests/Hakyll/Core/Provider/Metadata/Tests.hs

raw
1 --------------------------------------------------------------------------------
2 {-# LANGUAGE CPP #-}
3 module Hakyll.Core.Provider.Metadata.Tests
4 ( tests
5 ) where
6
7
8 --------------------------------------------------------------------------------
9 #if MIN_VERSION_aeson(2,0,0)
10 import qualified Data.Aeson.KeyMap as KeyMap
11 import qualified Data.Aeson.Key as AK
12 #else
13 import qualified Data.HashMap.Strict as KeyMap
14 import qualified Data.Text as T
15 #endif
16 import qualified Data.Yaml as Yaml
17 import Hakyll.Core.Metadata
18 import Hakyll.Core.Provider.Metadata
19 import Test.Tasty (TestTree, testGroup)
20 import Test.Tasty.HUnit (Assertion, assertFailure, (@=?))
21 import TestSuite.Util
22
23
24 --------------------------------------------------------------------------------
25 tests :: TestTree
26 tests = testGroup "Hakyll.Core.Provider.Metadata.Tests" $
27 fromAssertions "page" [testPage01, testPage02]
28
29
30
31 --------------------------------------------------------------------------------
32 testPage01 :: Assertion
33 testPage01 =
34 (meta [("foo", "bar")], "qux\n") `expectRight` parsePage
35 "---\nfoo: bar\n---\nqux\n"
36
37
38 --------------------------------------------------------------------------------
39 testPage02 :: Assertion
40 testPage02 =
41 (meta [("description", descr)], "Hello I am dog\n") `expectRight`
42 parsePage
43 "---\ndescription: A long description that would look better if it\n spanned multiple lines and was indented\n---\nHello I am dog\n"
44 where
45 descr :: String
46 descr =
47 "A long description that would look better if it spanned multiple lines and was indented"
48
49
50 --------------------------------------------------------------------------------
51 meta :: Yaml.ToJSON a => [(String, a)] -> Metadata
52 meta pairs = KeyMap.fromList [(keyFromString k, Yaml.toJSON v) | (k, v) <- pairs]
53 where
54 #if MIN_VERSION_aeson(2,0,0)
55 keyFromString = AK.fromString
56 #else
57 keyFromString = T.pack
58 #endif
59
60
61 --------------------------------------------------------------------------------
62 -- | This is useful when the 'Left' side of 'Either' doesn't have an 'Eq'
63 -- instance.
64 expectRight :: (Eq b, Show a, Show b) => b -> Either a b -> Assertion
65 expectRight _ (Left err) = assertFailure (show err)
66 expectRight expected (Right res) = expected @=? res