From 6fafde62449fa8a5cb8405d6270caa5e1ddac613 Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Sun, 14 Oct 2012 01:51:32 +0200 Subject: Organize unite tests hierarchically --- tests/unit-tests/Haddock/ParseSpec.hs | 79 +++++++++++++++++++++++++++++++++++ tests/unit-tests/Spec.hs | 9 ++++ tests/unit-tests/parsetests.hs | 79 ----------------------------------- 3 files changed, 88 insertions(+), 79 deletions(-) create mode 100644 tests/unit-tests/Haddock/ParseSpec.hs create mode 100644 tests/unit-tests/Spec.hs delete mode 100644 tests/unit-tests/parsetests.hs (limited to 'tests') diff --git a/tests/unit-tests/Haddock/ParseSpec.hs b/tests/unit-tests/Haddock/ParseSpec.hs new file mode 100644 index 00000000..0c959982 --- /dev/null +++ b/tests/unit-tests/Haddock/ParseSpec.hs @@ -0,0 +1,79 @@ +{-# LANGUAGE StandaloneDeriving, FlexibleInstances, UndecidableInstances, IncoherentInstances #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} +module Haddock.ParseSpec (main, spec) where + +import Test.Hspec +import RdrName (RdrName) +import DynFlags (DynFlags, defaultDynFlags) +import Haddock.Lex (tokenise) +import Haddock.Parse (parseParas) +import Haddock.Types +import Outputable +import Data.Monoid + +dynFlags :: DynFlags +dynFlags = defaultDynFlags (error "dynFlags for Haddock tests: undefined") + +instance Outputable a => Show a where + show = showSDoc dynFlags . ppr + +deriving instance Show a => Show (Doc a) +deriving instance Eq a =>Eq (Doc a) + +parse :: String -> Maybe (Doc RdrName) +parse s = parseParas $ tokenise dynFlags s (0,0) + +main :: IO () +main = hspec spec + +spec :: Spec +spec = do + describe "parseParas" $ do + + it "parses a paragraph" $ do + parse "foobar" `shouldBe` (Just . DocParagraph . DocString) "foobar\n" + + context "when parsing an example" $ do + + it "requires an example to be separated from a previous paragrap by an empty line" $ do + parse "foobar\n\n>>> fib 10\n55" `shouldBe` + (Just $ DocAppend (DocParagraph $ DocString "foobar\n") (DocExamples $ [Example "fib 10" ["55"]])) + + -- parse error + parse "foobar\n>>> fib 10\n55" `shouldBe` Nothing + + it "parses a result line that only contains as an emptly line" $ do + parse ">>> putFooBar\nfoo\n\nbar" `shouldBe` + (Just $ DocExamples $ [Example "putFooBar" ["foo","","bar"]]) + + context "when parsing a code block" $ do + it "requires a code blocks to be separated from a previous paragrap by an empty line" $ do + parse "foobar\n\n> some code" `shouldBe` + Just (DocAppend (DocParagraph (DocString "foobar\n")) (DocCodeBlock (DocString " some code\n"))) + + -- parse error + parse "foobar\n> some code" `shouldBe` Nothing + + + context "when parsing a URL" $ do + it "parses a URL" $ do + parse "" `shouldBe` + (Just . DocParagraph $ hyperlink "http://example.com/" Nothing `mappend` DocString "\n") + + it "accepts an optional label" $ do + parse "" `shouldBe` + (Just . DocParagraph $ hyperlink "http://example.com/" (Just "some link") `mappend` DocString "\n") + + context "when parsing properties" $ do + it "can parse a single property" $ do + parse "prop> 23 == 23" `shouldBe` (Just $ DocProperty "23 == 23") + + it "can parse a multiple subsequent properties" $ do + let input = unlines [ + "prop> 23 == 23" + , "prop> 42 == 42" + ] + parse input `shouldBe` (Just $ DocProperty "23 == 23" `DocAppend` DocProperty "42 == 42") + where + hyperlink :: String -> Maybe String -> Doc RdrName + hyperlink url = DocHyperlink . Hyperlink url diff --git a/tests/unit-tests/Spec.hs b/tests/unit-tests/Spec.hs new file mode 100644 index 00000000..68521c03 --- /dev/null +++ b/tests/unit-tests/Spec.hs @@ -0,0 +1,9 @@ +module Main where + +import Test.Hspec + +import qualified Haddock.ParseSpec + +main :: IO () +main = hspec $ do + describe "Haddock.Parse" Haddock.ParseSpec.spec diff --git a/tests/unit-tests/parsetests.hs b/tests/unit-tests/parsetests.hs deleted file mode 100644 index 1f923aa0..00000000 --- a/tests/unit-tests/parsetests.hs +++ /dev/null @@ -1,79 +0,0 @@ -{-# LANGUAGE StandaloneDeriving, FlexibleInstances, UndecidableInstances, IncoherentInstances #-} -{-# OPTIONS_GHC -fno-warn-orphans #-} -module Main (main, spec) where - -import Test.Hspec -import RdrName (RdrName) -import DynFlags (DynFlags, defaultDynFlags) -import Haddock.Lex (tokenise) -import Haddock.Parse (parseParas) -import Haddock.Types -import Outputable -import Data.Monoid - -dynFlags :: DynFlags -dynFlags = defaultDynFlags (error "dynFlags for Haddock tests: undefined") - -instance Outputable a => Show a where - show = showSDoc dynFlags . ppr - -deriving instance Show a => Show (Doc a) -deriving instance Eq a =>Eq (Doc a) - -parse :: String -> Maybe (Doc RdrName) -parse s = parseParas $ tokenise dynFlags s (0,0) - -main :: IO () -main = hspec spec - -spec :: Spec -spec = do - describe "parseParas" $ do - - it "parses a paragraph" $ do - parse "foobar" `shouldBe` (Just . DocParagraph . DocString) "foobar\n" - - context "when parsing an example" $ do - - it "requires an example to be separated from a previous paragrap by an empty line" $ do - parse "foobar\n\n>>> fib 10\n55" `shouldBe` - (Just $ DocAppend (DocParagraph $ DocString "foobar\n") (DocExamples $ [Example "fib 10" ["55"]])) - - -- parse error - parse "foobar\n>>> fib 10\n55" `shouldBe` Nothing - - it "parses a result line that only contains as an emptly line" $ do - parse ">>> putFooBar\nfoo\n\nbar" `shouldBe` - (Just $ DocExamples $ [Example "putFooBar" ["foo","","bar"]]) - - context "when parsing a code block" $ do - it "requires a code blocks to be separated from a previous paragrap by an empty line" $ do - parse "foobar\n\n> some code" `shouldBe` - Just (DocAppend (DocParagraph (DocString "foobar\n")) (DocCodeBlock (DocString " some code\n"))) - - -- parse error - parse "foobar\n> some code" `shouldBe` Nothing - - - context "when parsing a URL" $ do - it "parses a URL" $ do - parse "" `shouldBe` - (Just . DocParagraph $ hyperlink "http://example.com/" Nothing `mappend` DocString "\n") - - it "accepts an optional label" $ do - parse "" `shouldBe` - (Just . DocParagraph $ hyperlink "http://example.com/" (Just "some link") `mappend` DocString "\n") - - context "when parsing properties" $ do - it "can parse a single property" $ do - parse "prop> 23 == 23" `shouldBe` (Just $ DocProperty "23 == 23") - - it "can parse a multiple subsequent properties" $ do - let input = unlines [ - "prop> 23 == 23" - , "prop> 42 == 42" - ] - parse input `shouldBe` (Just $ DocProperty "23 == 23" `DocAppend` DocProperty "42 == 42") - where - hyperlink :: String -> Maybe String -> Doc RdrName - hyperlink url = DocHyperlink . Hyperlink url -- cgit v1.2.3