diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2019-08-30 11:46:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-30 11:46:13 +0200 |
commit | 036c583ea243869f05a5a311c90b94943a2b635c (patch) | |
tree | aadee7988980544f84b83d808707080481568cc5 /tests/Hakyll | |
parent | 779fa66c7b1719e071dc3f4d38a4cc2feb9492c6 (diff) | |
download | hakyll-036c583ea243869f05a5a311c90b94943a2b635c.tar.gz |
Improve error messages
Diffstat (limited to 'tests/Hakyll')
-rw-r--r-- | tests/Hakyll/Core/UnixFilter/Tests.hs | 22 | ||||
-rw-r--r-- | tests/Hakyll/Web/Template/Context/Tests.hs | 4 | ||||
-rw-r--r-- | tests/Hakyll/Web/Template/Tests.hs | 64 |
3 files changed, 56 insertions, 34 deletions
diff --git a/tests/Hakyll/Core/UnixFilter/Tests.hs b/tests/Hakyll/Core/UnixFilter/Tests.hs index 29e2cbf..e4e0f23 100644 --- a/tests/Hakyll/Core/UnixFilter/Tests.hs +++ b/tests/Hakyll/Core/UnixFilter/Tests.hs @@ -6,18 +6,16 @@ module Hakyll.Core.UnixFilter.Tests -------------------------------------------------------------------------------- -import Data.List (isInfixOf) -import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit (testCase) -import qualified Test.Tasty.HUnit as H +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit (testCase) +import qualified Test.Tasty.HUnit as H -------------------------------------------------------------------------------- import Hakyll.Core.Compiler -import Hakyll.Core.Compiler.Internal +import Hakyll.Core.Identifier import Hakyll.Core.Item import Hakyll.Core.UnixFilter -import Hakyll.Core.Identifier import TestSuite.Util @@ -51,10 +49,7 @@ unixFilterFalse :: H.Assertion unixFilterFalse = do store <- newTestStore provider <- newTestProvider store - result <- testCompiler store provider testMarkdown compiler - case result of - CompilerError es -> True H.@=? any ("exit code" `isInfixOf`) es - _ -> H.assertFailure "Expecting CompilerError" + testCompilerError store provider testMarkdown compiler "exit code" cleanTestEnv where compiler = getResourceString >>= withItemBody (unixFilter "false" []) @@ -65,12 +60,7 @@ unixFilterError :: H.Assertion unixFilterError = do store <- newTestStore provider <- newTestProvider store - result <- testCompiler store provider testMarkdown compiler - case result of - CompilerError es -> True H.@=? any containsIncorrectOptionMessage es - _ -> H.assertFailure "Expecting CompilerError" + testCompilerError store provider testMarkdown compiler "option" cleanTestEnv where compiler = getResourceString >>= withItemBody (unixFilter "head" ["-#"]) - incorrectOptionMessages = ["invalid option", "illegal option"] - containsIncorrectOptionMessage output = any (`isInfixOf` output) incorrectOptionMessages diff --git a/tests/Hakyll/Web/Template/Context/Tests.hs b/tests/Hakyll/Web/Template/Context/Tests.hs index 3adedd8..66460b6 100644 --- a/tests/Hakyll/Web/Template/Context/Tests.hs +++ b/tests/Hakyll/Web/Template/Context/Tests.hs @@ -62,6 +62,6 @@ testContextDone store provider identifier key context = cf <- unContext context key [] item case cf of StringField str -> return str - ListField _ _ -> error $ + _ -> error $ "Hakyll.Web.Template.Context.Tests.testContextDone: " ++ - "Didn't expect ListField" + "expected StringField" diff --git a/tests/Hakyll/Web/Template/Tests.hs b/tests/Hakyll/Web/Template/Tests.hs index bd794c7..a73b92d 100644 --- a/tests/Hakyll/Web/Template/Tests.hs +++ b/tests/Hakyll/Web/Template/Tests.hs @@ -1,5 +1,6 @@ -------------------------------------------------------------------------------- {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskell #-} module Hakyll.Web.Template.Tests ( tests ) where @@ -7,9 +8,10 @@ module Hakyll.Web.Template.Tests -------------------------------------------------------------------------------- import Test.Tasty (TestTree, testGroup) -import Test.Tasty.HUnit (Assertion, testCase, (@=?), - (@?=)) +import Test.Tasty.HUnit (Assertion, assertBool, testCase, + (@=?), (@?=)) +import Data.Either (isLeft) -------------------------------------------------------------------------------- import Hakyll.Core.Compiler @@ -32,13 +34,13 @@ tests = testGroup "Hakyll.Web.Template.Tests" $ concat , testCase "applyJoinTemplateList" testApplyJoinTemplateList ] - , fromAssertions "readTemplate" - [ [Chunk "Hello ", Expr (Call "guest" [])] - @=? readTemplateElems "Hello $guest()$" - , [If (Call "a" [StringLiteral "bar"]) [Chunk "foo"] Nothing] - @=? readTemplateElems "$if(a(\"bar\"))$foo$endif$" + , fromAssertions "parseTemplate" + [ Right [Chunk "Hello ", Expr (Call "guest" [])] + @=? parse "Hello $guest()$" + , Right [If (Call "a" [StringLiteral "bar"]) [Chunk "foo"] Nothing] + @=? parse "$if(a(\"bar\"))$foo$endif$" -- 'If' trim check. - , [ TrimL + , Right [ TrimL , If (Ident (TemplateKey "body")) [ TrimR , Chunk "\n" @@ -54,29 +56,39 @@ tests = testGroup "Hakyll.Web.Template.Tests" $ concat ]) , TrimR ] - @=? readTemplateElems "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$" + @=? parse "$-if(body)-$\n$body$\n$-else-$\n$body$\n$-endif-$" -- 'For' trim check. - , [ TrimL + , Right [ TrimL , For (Ident (TemplateKey "authors")) [TrimR, Chunk "\n body \n", TrimL] Nothing , TrimR ] - @=? readTemplateElems "$-for(authors)-$\n body \n$-endfor-$" + @=? parse "$-for(authors)-$\n body \n$-endfor-$" -- 'Partial' trim check. - , [ TrimL + , Right [ TrimL , Partial (StringLiteral "path") , TrimR ] - @=? readTemplateElems "$-partial(\"path\")-$" + @=? parse "$-partial(\"path\")-$" -- 'Expr' trim check. - , [ TrimL + , Right [ TrimL , Expr (Ident (TemplateKey "foo")) , TrimR ] - @=? readTemplateElems "$-foo-$" + @=? parse "$-foo-$" + -- fail on incomplete template. + , assertBool "did not yield error" $ isLeft $ + parse "a$b" + -- fail on mismatched template syntax. + , assertBool "did not fail to parse" $ isLeft $ + parse "$for(xs)$\n <p>foo</p>\n$endif$" ] + + , [testCase "embeddedTemplate" testEmbeddedTemplate] ] + where + parse = parseTemplateElemsFile "" -------------------------------------------------------------------------------- @@ -113,6 +125,8 @@ testApplyJoinTemplateList :: Assertion testApplyJoinTemplateList = do store <- newTestStore provider <- newTestProvider store + tpl <- testCompilerDone store provider "tpl" $ + compileTemplateItem (Item "tpl" "<b>$body$</b>") str <- testCompilerDone store provider "item3" $ applyJoinTemplateList ", " tpl defaultContext [i1, i2] @@ -121,4 +135,22 @@ testApplyJoinTemplateList = do where i1 = Item "item1" "Hello" i2 = Item "item2" "World" - tpl = readTemplate "<b>$body$</b>" + + +-------------------------------------------------------------------------------- +embeddedTemplate :: Template +embeddedTemplate = $(embedTemplate "tests/data/embed.html") + +-------------------------------------------------------------------------------- +testEmbeddedTemplate :: Assertion +testEmbeddedTemplate = do + store <- newTestStore + provider <- newTestProvider store + str <- testCompilerDone store provider "item3" $ + applyTemplate embeddedTemplate defaultContext item + + itemBody str @?= "<p>Hello, world</p>\n" + cleanTestEnv + where + item = Item "item1" "Hello, world" + |