aboutsummaryrefslogtreecommitdiff
path: root/test/Tests/Readers/RST.hs
blob: 68241b7f94f2c9d9edff64943bc9807e6b422f1a (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{- |
   Module      : Tests.Readers.RST
   Copyright   : © 2006-2021 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Tests for the RST reader.
-}
module Tests.Readers.RST (tests) where

import Prelude
import Data.Text (Text)
import qualified Data.Text as T
import Test.Tasty
import Tests.Helpers
import Text.Pandoc
import Text.Pandoc.Arbitrary ()
import Text.Pandoc.Builder

rst :: Text -> Pandoc
rst = purely $ readRST def{ readerStandalone = True }

infix 4 =:
(=:) :: ToString c
     => String -> (Text, c) -> TestTree
(=:) = test rst

tests :: [TestTree]
tests = [ "line block with blank line" =:
          "| a\n|\n|  b" =?> lineBlock [ "a", mempty, "\160b" ]
        , testGroup "field list"
          [ "general" =: T.unlines
             [ "para"
             , ""
             , ":Hostname: media08"
             , ":IP address: 10.0.0.19"
             , ":Size: 3ru"
             , ":Version: 1"
             , ":Indentation: Since the field marker may be quite long, the second"
             , "   and subsequent lines of the field body do not have to line up"
             , "   with the first line, but they must be indented relative to the"
             , "   field name marker, and they must line up with each other."
             , ":Parameter i: integer"
             , ":Final: item"
             , "  on two lines" ]
             =?>
              doc (para "para" <>
                   definitionList [ (str "Hostname", [para "media08"])
                                  , (text "IP address", [para "10.0.0.19"])
                                  , (str "Size", [para "3ru"])
                                  , (str "Version", [para "1"])
                                  , (str "Indentation", [para "Since the field marker may be quite long, the second\nand subsequent lines of the field body do not have to line up\nwith the first line, but they must be indented relative to the\nfield name marker, and they must line up with each other."])
                                  , (text "Parameter i", [para "integer"])
                                  , (str "Final", [para "item\non two lines"])
                                  ])
          , "metadata" =: T.unlines
             [ "====="
             , "Title"
             , "====="
             , "--------"
             , "Subtitle"
             , "--------"
             , ""
             , ":Version: 1"
             ]
             =?>
              setMeta "version" (para "1") (setMeta "title" ("Title" :: Inlines)
                 $ setMeta "subtitle" ("Subtitle" :: Inlines)
                 $ doc mempty)
          , "with inline markup" =: T.unlines
             [ ":*Date*: today"
             , ""
             , ".."
             , ""
             , ":*one*: emphasis"
             , ":two_: reference"
             , ":`three`_: another one"
             , ":``four``: literal"
             , ""
             , ".. _two: http://example.com"
             , ".. _three: http://example.org"
             ]
             =?>
              setMeta "date" (str "today") (doc
                 $ definitionList [ (emph "one", [para "emphasis"])
                                  , (link "http://example.com" "" "two", [para "reference"])
                                  , (link "http://example.org" "" "three", [para "another one"])
                                  , (code "four", [para "literal"])
                                  ])
          ]
        , "URLs with following punctuation" =:
          ("http://google.com, http://yahoo.com; http://foo.bar.baz.\n" <>
           "http://foo.bar/baz_(bam) (http://foo.bar)") =?>
          para (link "http://google.com" "" "http://google.com" <> ", " <>
                link "http://yahoo.com" "" "http://yahoo.com" <> "; " <>
                link "http://foo.bar.baz" "" "http://foo.bar.baz" <> ". " <>
                softbreak <>
                link "http://foo.bar/baz_(bam)" "" "http://foo.bar/baz_(bam)"
                <> " (" <> link "http://foo.bar" "" "http://foo.bar" <> ")")
        , "Reference names with special characters" =:
                   ("A-1-B_2_C:3:D+4+E.5.F_\n\n" <>
                   ".. _A-1-B_2_C:3:D+4+E.5.F: https://example.com\n") =?>
                   para (link "https://example.com" "" "A-1-B_2_C:3:D+4+E.5.F")
        , "Code directive with class and number-lines" =: T.unlines
            [ ".. code::python"
            , "   :number-lines: 34"
            , "   :class: class1 class2 class3"
            , ""
            , "  def func(x):"
            , "    return y"
            ]  =?>
              doc (codeBlockWith
                  ( ""
                  , ["python", "numberLines", "class1", "class2", "class3"]
                  , [ ("startFrom", "34") ]
                  )
                  "def func(x):\n  return y")
        , "Code directive with number-lines, no line specified" =: T.unlines
            [ ".. code::python"
            , "   :number-lines:"
            , ""
            , "  def func(x):"
            , "    return y"
            ]  =?>
              doc (codeBlockWith
                  ( ""
                  , ["python", "numberLines"]
                  , []
                  )
                  "def func(x):\n  return y")
        , testGroup "literal / line / code blocks"
          [ "indented literal block" =: T.unlines
            [ "::"
            , ""
            , "  block quotes"
            , ""
            , "  can go on for many lines"
            , "but must stop here"]
            =?>
              doc (
                 codeBlock "block quotes\n\ncan go on for many lines" <>
                 para "but must stop here")
          , "line block with 3 lines" =: "| a\n| b\n| c"
            =?> lineBlock ["a", "b", "c"]
          , "line blocks with blank lines" =: T.unlines
            [ "|"
            , ""
            , "|"
            , "| a"
            , "| b"
            , "|"
            , ""
            , "|"
            ] =?>
            lineBlock [""] <>
            lineBlock ["", "a", "b", ""] <>
            lineBlock [""]
          , "quoted literal block using >" =: "::\n\n> quoted\n> block\n\nOrdinary paragraph"
            =?> codeBlock "> quoted\n> block" <> para "Ordinary paragraph"
          , "quoted literal block using | (not  a line block)" =: "::\n\n| quoted\n| block\n\nOrdinary paragraph"
            =?> codeBlock "| quoted\n| block" <> para "Ordinary paragraph"
          , "class directive with single paragraph" =: ".. class:: special\n\nThis is a \"special\" paragraph."
            =?> divWith ("", ["special"], []) (para "This is a \"special\" paragraph.")
          , "class directive with two paragraphs" =: ".. class:: exceptional remarkable\n\n    First paragraph.\n\n    Second paragraph."
            =?> divWith ("", ["exceptional", "remarkable"], []) (para "First paragraph." <> para "Second paragraph.")
          , "class directive around literal block" =: ".. class:: classy\n\n::\n\n    a\n    b"
            =?> divWith ("", ["classy"], []) (codeBlock "a\nb")]
        , testGroup "interpreted text roles"
          [ "literal role prefix" =: ":literal:`a`" =?> para (code "a")
          , "literal role postfix" =: "`a`:literal:" =?> para (code "a")
          , "literal text" =: "``text``" =?> para (code "text")
          , "code role" =: ":code:`a`" =?> para (codeWith ("", [], []) "a")
          , "inherited code role" =: ".. role:: codeLike(code)\n\n:codeLike:`a`"
            =?> para (codeWith ("", ["codeLike"], []) "a")
          , "custom code role with language field"
            =: ".. role:: lhs(code)\n    :language: haskell\n\n:lhs:`a`"
            =?> para (codeWith ("", ["lhs", "haskell"], []) "a")
          , "custom role with unspecified parent role"
            =: ".. role:: classy\n\n:classy:`text`"
            =?> para (spanWith ("", ["classy"], []) "text")
          , "role with recursive inheritance"
            =: ".. role:: haskell(code)\n.. role:: lhs(haskell)\n\n:lhs:`text`"
            =?> para (codeWith ("", ["lhs", "haskell"], []) "text")
          , "unknown role" =: ":unknown:`text`" =?>
              para (codeWith ("",["interpreted-text"],[("role","unknown")]) "text")
          ]
        , testGroup "footnotes"
          [ "remove space before note" =: T.unlines
            [ "foo [1]_"
            , ""
            , ".. [1]"
            , "   bar"
            ] =?>
              para ("foo" <> note (para "bar"))
          ]
        , testGroup "inlines"
          [ "links can contain an URI without being parsed twice (#4581)" =:
            "`http://loc <http://loc>`__" =?>
            para (link "http://loc" "" "http://loc")
          , "inline markup cannot be nested" =:
            "**a*b*c**" =?>
            para (strong "a*b*c")
          , "bare URI parsing disabled inside emphasis (#4561)" =:
            "*http://location*" =?>
            para (emph (text "http://location"))
          , "include newlines" =:
            "**before\nafter**" =?>
            para (strong (text "before\nafter"))
          ]
        ]