Browse Source

Dedup spec code

image-sizing
Logan McGrath 12 months ago
parent
commit
5edabf84a5
  1. 3
      logans-blog.cabal
  2. 1
      package.yaml
  3. 61
      test/Site/RouteSpec.hs
  4. 4
      test/Site/Spec/Util.hs

3
logans-blog.cabal

@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 635b48f2a29cf3a538d8222465c4322a8f12851cc98648de5fae3eeb6e85f8e2
-- hash: 8e0ce69427e4325772aa57f58eda9c6ab8fc5077a280e21247168723e13ef5d9
name: logans-blog
version: 0.1.0.0
@ -342,6 +342,7 @@ test-suite logans-blog-test
, hakyll ==4.14.*
, hjsmin ==0.2.*
, hspec ==2.7.*
, hspec-core ==2.7.*
, language-javascript ==0.7.*
, logans-blog
, microlens ==0.4.*

1
package.yaml

@ -119,3 +119,4 @@ tests:
dependencies:
- logans-blog
- hspec == 2.7.*
- hspec-core == 2.7.*

61
test/Site/RouteSpec.hs

@ -1,6 +1,5 @@
module Site.RouteSpec where
import Control.Exception (bracket)
import Data.Bifunctor
import Hakyll.Core.Identifier
import Site.Common
@ -11,44 +10,34 @@ spec :: Spec
spec = do
around withRunRoutes do
describe "indexRoute" do
it "appends the suffix /index.html if not there already" $
runSpec indexRoute $
[ ("info/tag-cloud.html", "info/tag-cloud/index.html"),
("contact.html", "contact/index.html"),
("contact/index.html", "contact/index.html")
]
runTable indexRoute $
[ ("info/tag-cloud.html", "info/tag-cloud/index.html"),
("contact.html", "contact/index.html"),
("contact/index.html", "contact/index.html")
]
describe "stripPrefixRoute" do
it "strips the prefix pages/ if it exists" $
runSpec (stripPrefixRoute "^pages/") $
[ ("pages/contact.md", "contact.md"),
("pages/archives.html", "archives.html"),
("404.md", "404.md")
]
runTable (stripPrefixRoute "^pages/") $
[ ("pages/contact.md", "contact.md"),
("pages/archives.html", "archives.html"),
("404.md", "404.md")
]
describe "htmlPageRoute" do
it "sets the .html extension, strips the pages/ prefix, and adds /index.html suffix" $
runSpec htmlPageRoute $
[ ("pages/contact.md", "contacts/index.html"),
("pages/archives.md", "archives/index.html"),
("pages/about-me.md", "about-me/index.html")
]
runTable htmlPageRoute $
[ ("pages/contact.md", "contacts/index.html"),
("pages/archives.md", "archives/index.html"),
("pages/about-me.md", "about-me/index.html")
]
runTable :: Routes -> [(String, String)] -> SpecWith RunRoutes
runTable routes = foldl (>>) (return ()) . fmap makeExample
where
makeExample inputOutput = it ("routes " ++ fst inputOutput ++ " to " ++ snd inputOutput) $ runExample inputOutput
runExample inputOutput runRoutes' =
let applyRoutes = fmap fst . runRoutes' routes . fromFilePath
(actual, expected) = first applyRoutes inputOutput
in actual >>= (`shouldBe` Just expected)
type RunRoutes = Routes -> Identifier -> IO (Maybe FilePath, UsedMetadata)
runSpec :: Routes -> [(String, String)] -> RunRoutes -> Expectation
runSpec routes testInputOutput runRoutes' =
runExpectations setExpectations
where
setExpectations = expect . bimap runRoutes'' Just <$> testInputOutput
expect (actualM, expected) = actualM >>= (`shouldBe` expected)
runRoutes'' = fmap fst . runRoutes' routes . fromFilePath
withRunRoutes :: (RunRoutes -> IO a) -> IO a
withRunRoutes = bracket acquire release
where
acquire = flip runRoutes . snd <$> createStoreAndProvider
release = const (return ())
withRunRoutes' :: (RunRoutes -> IO a) -> IO a
withRunRoutes' f = withStoreAndProvider \(_, provider) -> f (`runRoutes` provider)
withRunRoutes f = withStoreAndProvider \(_, provider) -> f (`runRoutes` provider)

4
test/Site/Spec/Util.hs

@ -10,10 +10,6 @@ import Hakyll.Core.Provider
import Hakyll.Core.Store (Store)
import qualified Hakyll.Core.Store as Store
import Hakyll.Core.Util.File
import Test.Hspec
runExpectations :: [Expectation] -> Expectation
runExpectations = foldl (>>) (return ())
createStoreAndProvider :: IO (Store, Provider)
createStoreAndProvider = do

Loading…
Cancel
Save