From 036c583ea243869f05a5a311c90b94943a2b635c Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Fri, 30 Aug 2019 11:46:13 +0200 Subject: Improve error messages --- tests/Hakyll/Core/UnixFilter/Tests.hs | 22 +++------- tests/Hakyll/Web/Template/Context/Tests.hs | 4 +- tests/Hakyll/Web/Template/Tests.hs | 64 ++++++++++++++++++++++-------- tests/TestSuite/Util.hs | 13 +++++- tests/data/embed.html | 1 + 5 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 tests/data/embed.html (limited to 'tests') 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

foo

\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" "$body$") 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 "$body$" + + +-------------------------------------------------------------------------------- +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 @?= "

Hello, world

\n" + cleanTestEnv + where + item = Item "item1" "Hello, world" + diff --git a/tests/TestSuite/Util.hs b/tests/TestSuite/Util.hs index fa411f8..2678fea 100644 --- a/tests/TestSuite/Util.hs +++ b/tests/TestSuite/Util.hs @@ -6,6 +6,7 @@ module TestSuite.Util , newTestProvider , testCompiler , testCompilerDone + , testCompilerError , testConfiguration , cleanTestEnv , renderParagraphs @@ -13,7 +14,7 @@ module TestSuite.Util -------------------------------------------------------------------------------- -import Data.List (intercalate) +import Data.List (intercalate, isInfixOf) import Data.Monoid (mempty) import qualified Data.Set as S import Test.Tasty @@ -80,13 +81,21 @@ testCompilerDone store provider underlying compiler = do CompilerDone x _ -> return x CompilerError e -> fail $ "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++ - " threw: " ++ intercalate "; " e + " threw: " ++ intercalate "; " (compilerErrorMessages e) CompilerRequire i _ -> fail $ "TestSuite.Util.testCompilerDone: compiler " ++ show underlying ++ " requires: " ++ show i CompilerSnapshot _ _ -> fail "TestSuite.Util.testCompilerDone: unexpected CompilerSnapshot" +testCompilerError :: Store -> Provider -> Identifier -> Compiler a -> String -> IO () +testCompilerError store provider underlying compiler expectedMessage = do + result <- testCompiler store provider underlying compiler + case result of + CompilerError e -> + any (expectedMessage `isInfixOf`) (compilerErrorMessages e) @? + "Expecting '" ++ expectedMessage ++ "' error" + _ -> assertFailure "Expecting CompilerError" -------------------------------------------------------------------------------- testConfiguration :: Configuration diff --git a/tests/data/embed.html b/tests/data/embed.html new file mode 100644 index 0000000..6860e73 --- /dev/null +++ b/tests/data/embed.html @@ -0,0 +1 @@ +

$body$

-- cgit v1.2.3