aboutsummaryrefslogtreecommitdiff
path: root/test/Tests/Readers/Mdoc.hs
blob: 32b8ae36e9db6167b5c8f9b66f451806fbc8aae2 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Tests.Readers.Mdoc
   Copyright   : © 2024 Evan Silberman
   License     : GNU GPL, version 2 or above

   Maintainer  : 
   Stability   : alpha
   Portability : portable

Tests for the Mdoc reader.
-}

module Tests.Readers.Mdoc (tests) where

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

mdoc :: Text -> Pandoc
mdoc = purely $ readMdoc def

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

cls :: Text -> Attr
cls x = (mempty, [x], mempty)

tests :: [TestTree]
tests = [
  testGroup "one-line enclosures"
    [ "Dq" =:
        ".Dq hello world" =?>
        para (doubleQuoted "hello world")
    , "Sq" =:
        ".Sq hello world" =?>
        para (singleQuoted "hello world")
    , "empty" =:
        ".Dq" =?>
        para (doubleQuoted mempty)
    , "nested" =:
        ".Dq Pq hello world" =?>
        para (doubleQuoted "(hello world)")
    , "nested with closing delimiters" =:
        ".Dq Pq hi mom !" =?>
        para (doubleQuoted "(hi mom)" <> "!")
    , "nested multiline enclosure" =:
        ".Dq Po a \\&; b \\&; c Pc ." =?>
        para (doubleQuoted "(a ; b ; c)" <> ".")
    , "with inlines" =:
        ".Dq hello Sy world ." =?>
        para (doubleQuoted ("hello" <> space <> strong "world" <> "."))
    , "with text production" =:
        ".Dq I love the St -iso8601 standard!" =?>
        para (doubleQuoted "I love the ISO 8601 standard!")
    , "with Ns" =:
        ".Op , Ns Ar value ..." =?>
        para ("[," <> (codeWith (cls "variable") "value ...") <> "]")
    , "ending with open delimiter" =:
        ".Dq hi (" =?>
        para (doubleQuoted "hi (")
    ]
  , testGroup "multiline enclosures"
    [ "nested multiline" =:
        T.unlines [".Bo", ".Po", "hi", ".Pc", ".Bc"] =?>
        para ("[(hi)]")
    , "nested on one line" =:
        ".Bo Po hi Pc Bc" =?>
        para ("[(hi)]")
    , "with wacky delimiters" =:
        ".Bo ( | hi ! Bc ?" =?>
        para ("([| hi!]?")
    ]
  , testGroup "simple inlines"
    [ "Sy" =:
        ".Sy hello world" =?>
        para (strong "hello world")
    , "Em" =:
        ".Em hello world" =?>
        para (emph "hello world")
    , "Ev" =:
        ".Ev HELLO_WORLD ," =?>
        para (codeWith (cls "Ev") "HELLO_WORLD" <> ",")
    , "Ar" =:
        ".Ar ) z" =?>
        para (codeWith (cls "variable") "file ..." <> ") " <> codeWith (cls "variable") "z")
    , "In" =:
        ".In ( math.h ) b c" =?>
        para ("(" <> codeWith (cls "In") "<math.h>" <> ") b c")
    , "Mt" =:
        ".Mt [email protected] , [email protected]" =?>
        para ((link "mailto:[email protected]" "" "[email protected]") <>
               "," <> space <> (link "mailto:[email protected]" "" "[email protected]"))
    , "No" =:
        ".No ( hello , world ! )" =?>
        para "(hello, world!)"
    , "empty Pa with closing punctuation" =:
        ".Pa ) z" =?>
        para (spanWith (cls "Pa") "~" <> ")" <> space <> spanWith (mempty, ["Pa"], mempty) "z")
    , "delimiters" =:
        ".Sy ( hello world )" =?>
        para (mconcat ["(", strong "hello world", ")"])
    , "multiple" =:
        ".Sy hello Em world" =?>
        para (strong "hello" <> space <> emph "world")
    ]
  , testGroup "Fl"
    [ "simple" =:
        ".Fl w" =?>
        para (codeWith (cls "Fl") "-w")
    , "multiple" =:
        ".Fl W all" =?>
        para (codeWith (cls "Fl") "-W" <> space <> codeWith (cls "Fl") "-all")
    , "empty with following macro" =:
        ".Fl Cm x" =?>
        para (codeWith (cls "Fl") "-" <> codeWith (cls "Cm") "x")
    , "enclosed with following macro" =:
        ".Pq Fl Cm x" =?>
        para ("(" <> codeWith (cls "Fl") "-" <> codeWith (cls "Cm") "x" <> ")")
    -- XXX this is a mandoc delta, the period is meant to land outside
    -- the enclosure. parseInline has learned how to defer eols but not
    -- closing delimiters
    , "enclosed with following delimiters" =:
        ".Pq Fl x ." =?>
        para ("(" <> codeWith (cls "Fl") "-x" <> ".)")
    , "following Ns" =:
        ".Fl W Ns Cm all" =?>
        para (codeWith (cls "Fl") "-W" <> codeWith (cls "Cm") "all")
    , "GNU" =:
        ".Fl -help" =?>
        para (codeWith (cls "Fl") "--help")
    , "GNU escaped" =:
        ".Fl \\-help" =?>
        para (codeWith (cls "Fl") "--help")
    , "GNU Fl Fl" =:
        ".Fl Fl help" =?>
        para (codeWith (cls "Fl") "--help")
    , "punctuation" =:
        ".Op Fl a | b" =?>
        para ("[" <> codeWith (cls "Fl") "-a" <> " | " <> codeWith (cls "Fl") "-b" <> "]")
    , "middle close paren" =:
        ".Fl a ) z" =?>
        para (codeWith (cls "Fl") "-a" <> ") " <> codeWith (cls "Fl") "-z")
    , "empty with close paren" =:
        ".Fl ) z" =?>
        para (codeWith (cls "Fl") "-" <> ") " <> codeWith (cls "Fl") "-z")
    , "empty with pipe" =:
        ".Fl | z" =?>
        para (codeWith (cls "Fl") "-" <> " | " <> codeWith (cls "Fl") "-z")
    , "empty with parens" =:
        ".Fl ( )" =?>
        para ("(" <> codeWith (cls "Fl") "-" <> ")")
    ]
  , testGroup "links"
    [ "basic" =:
        ".Lk href name" =?>
        para (link "href" "" "name")
    , "complicated" =:
        ".Lk , ( href name )" =?>
        para ("," <> space <> "(" <> link "href" "" "name" <> ")")
    , "unnamed" =:
        ".Lk href" =?>
        para (link "href" "" "href")
    ]
  , testGroup "Ns macro"
    [ "at the beginning of a macro line (mandoc delta)" =:
        T.unlines [".Op before", ".Ns Op after"] =?>
        para "[before] [after]"  -- mandoc: warning
    , "after a block closing macro" =:
        T.unlines [".Oo before", ".Oc Ns Op after"] =?>
        para "[before][after]"
    , "in the middle of a macro line" =:
        ".Oo before Oc Ns Op after" =?>
        para "[before][after]"
    , "before closing punctuation" =:
        ".Oo before Oc Ns : Op after" =?>
        para "[before]: [after]"  -- mandoc: warning
    , "after closing punctuation" =:
        ".Oo before Oc : Ns Op after" =?>
        para "[before]:[after]"
    , "at the end of a macro line" =:
        T.unlines [".Oo before Oc Ns", ".Op after"] =?>
        para "[before][after]"
    , "at the end of a partial-implicit line" =:
        T.unlines [".Op before Ns", ".Op after"] =?>
        para "[before][after]"
    , "normal words" =:
        ".No no Ns ns No no" =?>
        para ("nons" <> space <> "no")
    , "opening punctuation" =:
        ".No no Ns \"(\" ns No no" =?>
        para ("no(ns" <> space <> "no")
    , "closing punctuation" =:
        ".No no \"Ns\" ns \")\" No no" =?>
        para ("nons)" <> space <> "no")
    ]
  , testGroup "spacing mode"
    [ "all text" =:
        T.unlines ["a", ".Sm off", "b c", "d", ".Sm on", "e"] =?>
        para ("a b c d e")
    , "text around macro" =:
        T.unlines ["a", ".Sm off", ".Sy b c", ".Sm on", "d"] =?>
        para ("a" <> space <> strong "bc" <> space <> "d")
    , "mulitple macros" =:
        T.unlines ["a", ".Sm off", ".Sy b Em c", ".Sm on", "d"] =?>
        para ("a" <> space <> strong "b" <> emph "c" <> space <> "d")
    , "mulitple control lines" =:
        T.unlines ["a", ".Sm off", ".Sy b", ".Em c", ".Sm on", "d"] =?>
        para ("a" <> space <> strong "b" <> emph "c" <> space <> "d")
    , "mixed control and text lines" =:
        T.unlines ["a", ".Sm off", ".Sy b", "c", ".Em d", ".Sm on", "d"] =?>
        para ("a" <> space <> strong "b" <> "c" <> emph "d" <> space <> "d")
    , "delimiters" =:
        T.unlines [".Sm off", ".Em a ", ".Em [ b | c ]", ".Sm on"] =?>
        para (emph "a" <> "[" <> emph "b" <> "|" <> emph "c" <> "]")
    ]
  , testGroup "Ap macro"
    [ "in the middle of a macro line" =:
        ".Xr mandoc 1 Ap s" =?>
        para (spanWith (cls "Xr") "mandoc(1)" <> "'s")
    -- mandoc difference: the edge case of "Ap (" tested in this mandoc regress
    -- isn't present in any actual OpenBSD base system manuals, where Ap is
    -- only ever followed by a letter. Furthermore, "Ap" is generally uncommon
    -- compared to "Ns '" (e.g. ".Xr mandoc 1 Ns 's"). I'm accepting a
    -- difference from mandoc here because correctly suppressing space after
    -- the "(" here would require more refactoring than I feel like doing at
    -- time of writing.
    -- per mandoc, should be: para (strong "bold" <> "'(" <> strong "bold")
    , "with punctuation and called macro" =:
        ".Sy bold Ap ( \"Sy\" bold" =?>
        para (strong "bold" <> "'( " <> strong "bold")
    ]
  , testGroup "Pf macro"
    [ "closing punctuation" =:
        ".Pf . right ." =?>
        para ".right."
    , "double punctuation" =:
        ".Pf . . double" =?>
        para ".. double"
    , "opening punctuation" =:
        ".Pf ( left ." =?>
        para "(left."
    , "unparsed argument" =:
        ".Pf Ar Sy gument " =?>
        para ("Ar" <> strong "gument")
    ]
  , testGroup "text production"
    [ "early NetBSD versions" =:
        T.unlines [".Nx 0.9a", "and", ".Nx 9.4b"] =?>
        para "NetBSD 0.9A and NetBSD 9.4b"
    , "with Ns" =:
        ".Ox Ns -specific" =?>
        para "OpenBSD-specific"
    , "with punctuation" =:
        ".Fx ," =?>
        para "FreeBSD,"
    , "with argument and punctuation" =:
        ".Fx 12.0 ." =?>
        para "FreeBSD 12.0."
    , "BSD alone" =:
        ".Bx ." =?>
        para "BSD."
    , "BSD with macro" =:
        ".Bx No rocks" =?>
        para "BSD rocks"
    , "BSD with version" =:
        ".Bx 4.4 ," =?>
        para "4.4BSD,"
    , "BSD with variant" =:
        ".Bx 4.3 tahoe !" =?>
        para "4.3BSD-Tahoe!"
    ]
  , testGroup "inline punctuation"
    [ testGroup "leading punctuation"
      [ "open paren"           =: ".Em ( b"     =?> para ("(" <> emph "b")
      , "open square bracket"  =: ".Em \"[\" b" =?> para ("[" <> emph "b")
      , "pipe"                 =: ".Em | b"     =?> para ("|" <> space <> emph "b")
      , "period"               =: ".Em . b"     =?> para ("." <> space <> emph "b")
      , "comma"                =: ".Em , b"     =?> para ("," <> space <> emph "b")
      , "semicolon"            =: ".Em ; b"     =?> para (";" <> space <> emph "b")
      , "colon"                =: ".Em : b"     =?> para (":" <> space <> emph "b")
      , "question mark"        =: ".Em ? b"     =?> para ("?" <> space <> emph "b")
      , "exclamation"          =: ".Em ! b"     =?> para ("!" <> space <> emph "b")
      , "close paren"          =: ".Em ) b"     =?> para (")" <> space <> emph "b")
      , "close square bracket" =: ".Em \"]\" b" =?> para ("]" <> space <> emph "b")
      ]
    , testGroup "trailing punctuation"
      [ "open paren"           =: ".Em a ("     =?> para (emph "a" <> space <> "(")
      , "open square bracket"  =: ".Em a ["     =?> para (emph "a" <> space <> "[")
      , "pipe"                 =: ".Em a |"     =?> para (emph "a" <> space <> "|")
      , "period"               =: ".Em a ."     =?> para (emph "a" <> ".")
      , "comma"                =: ".Em a ,"     =?> para (emph "a" <> ",")
      , "semicolon"            =: ".Em a ;"     =?> para (emph "a" <> ";")
      , "colon"                =: ".Em a :"     =?> para (emph "a" <> ":")
      , "question mark"        =: ".Em a ?"     =?> para (emph "a" <> "?")
      , "exclamation"          =: ".Em a !"     =?> para (emph "a" <> "!")
      , "close parens"         =: ".Em a \")\"" =?> para (emph "a" <> ")")
      , "close square bracket" =: ".Em a ]"     =?> para (emph "a" <> "]")
      ]
    , testGroup "middle punctuation"
      [ "open paren"           =: ".Em a ( b"     =?> para (mconcat [emph "a", space, "(", emph "b"])
      , "open square bracket"  =: ".Em a [ b"     =?> para (mconcat [emph "a", space, "[", emph "b"])
      , "pipe"                 =: ".Em a \"|\" b" =?> para (mconcat [emph "a", space, "|", space, emph "b"])
      , "period"               =: ".Em a . b"     =?> para (mconcat [emph "a", ".", space, emph "b"])
      , "comma"                =: ".Em a , b"     =?> para (mconcat [emph "a", ",", space, emph "b"])
      , "semicolon"            =: ".Em a ; b"     =?> para (mconcat [emph "a", ";", space, emph "b"])
      , "colon"                =: ".Em a \":\" b" =?> para (mconcat [emph "a", ":", space, emph "b"])
      , "question mark"        =: ".Em a ? b"     =?> para (mconcat [emph "a", "?", space, emph "b"])
      , "exclamation"          =: ".Em a ! b"     =?> para (mconcat [emph "a", "!", space, emph "b"])
      , "close paren"          =: ".Em a ) b"     =?> para (mconcat [emph "a", ")", space, emph "b"])
      , "close square bracket" =: ".Em a ] b"     =?> para (mconcat [emph "a", "]", space, emph "b"])
      ]
    ]
  ]