aboutsummaryrefslogtreecommitdiff
path: root/tests/unit-tests/parsetests.hs
blob: 58348a59bc7c4c380c090a662366d0d1231b4e57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{-# LANGUAGE StandaloneDeriving, FlexibleInstances, UndecidableInstances, IncoherentInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main (main) where

import Test.HUnit
import RdrName (RdrName)
import DynFlags (defaultDynFlags)
import Haddock.Lex (tokenise)
import Haddock.Parse (parseParas)
import Haddock.Types
import Outputable
import Data.Monoid

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)

data ParseTest = ParseTest {
    input   :: String
  , result  :: (Maybe (Doc RdrName))
  }

tests :: [ParseTest]
tests = [
    ParseTest {
      input  = "foobar"
    , result = Just $ DocParagraph $ DocString "foobar\n"
    }

  , ParseTest {
      input  = "foobar\n\n>>> fib 10\n55"
    , result = Just $ DocAppend (DocParagraph $ DocString "foobar\n") (DocExamples $ [Example "fib 10" ["55"]])
    }

  , ParseTest {
      input  = "foobar\n>>> fib 10\n55"
    , result = Nothing -- parse error
    }

  , ParseTest {
      input  = "foobar\n\n> some code"
    , result = Just (DocAppend (DocParagraph (DocString "foobar\n")) (DocCodeBlock (DocString " some code\n")))
    }

  , ParseTest {
      input  = "foobar\n> some code"
    , result = Nothing -- parse error
    }

  -- test <BLANKLINE> support
  , ParseTest {
      input  = ">>> putFooBar\nfoo\n<BLANKLINE>\nbar"
    , result = Just $ DocExamples $ [Example "putFooBar" ["foo","","bar"]]
    }

  -- tests for links
  , ParseTest {
      input  = "<http://example.com/>"
    , result = Just . DocParagraph $ hyperlink "http://example.com/" Nothing `mappend` DocString "\n"
    }

  , ParseTest {
      input  = "<http://example.com/ some link>"
    , result = Just . DocParagraph $ hyperlink "http://example.com/" (Just "some link") `mappend` DocString "\n"
    }
  ]

hyperlink :: String -> Maybe String -> Doc RdrName
hyperlink url = DocHyperlink . Hyperlink url

main :: IO ()
main = do
  _ <- runTestTT $ TestList $ map toTestCase tests
  return ();
  where

    toTestCase :: ParseTest -> Test
    toTestCase (ParseTest s r) = TestCase $ assertEqual s r (parse s)

    parse :: String -> Maybe (Doc RdrName)
    parse s = parseParas $ tokenise dynFlags s (0,0)