summary refs log tree commit diff stats
path: root/tests/stdlib/tdochelpers.nim
blob: 300ce76dc69f8dfae6c0dbd1c7148589a0bf0ee5 (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
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
discard """
  output: '''

[Suite] Integration with Nim
'''
"""

# tests for dochelpers.nim module

import ../../lib/packages/docutils/[rstast, rst, dochelpers]
import unittest
import std/assertions

proc testMsgHandler(filename: string, line, col: int, msgkind: MsgKind,
                    arg: string) =
  doAssert msgkind == mwBrokenLink

proc fromRst(text: string): LangSymbol =
  let r = rstParse(text, "-input-", LineRstInit, ColRstInit,
                   {roNimFile},
                   msgHandler=testMsgHandler)
  assert r.node.kind == rnRstRef
  result = toLangSymbol(r.node)

proc fromMd(text: string): LangSymbol =
  let r = rstParse(text, "-input-", LineRstInit, ColRstInit,
                   {roPreferMarkdown, roSupportMarkdown, roNimFile},
                   msgHandler=testMsgHandler)
  assert r.node.kind == rnPandocRef
  assert r.node.len == 2
  # this son is the target:
  assert r.node.sons[1].kind == rnInner
  result = toLangSymbol(r.node.sons[1])

suite "Integration with Nim":
  test "simple symbol parsing (shortest form)":
    let expected = LangSymbol(symKind: "", name: "g")
    check "g_".fromRst == expected
    check "[g]".fromMd == expected
    # test also alternative syntax variants of Pandoc Markdown:
    check "[g][]".fromMd == expected
    check "[this symbol][g]".fromMd == expected

  test "simple symbol parsing (group of words)":
    #let input1 = "`Y`_".rstParseTest
    let expected1 = LangSymbol(symKind: "", name: "Y")
    check "`Y`_".fromRst == expected1
    check "[Y]".fromMd == expected1

    # this means not a statement 'type', it's a backticked identifier `type`:
    let expected2 = LangSymbol(symKind: "", name: "type")
    check "`type`_".fromRst == expected2
    check "[type]".fromMd == expected2

    let expected3 = LangSymbol(symKind: "", name: "[]")
    check "`[]`_".fromRst == expected3
    # Markdown syntax for this case is NOT [[]]
    check "[`[]`]".fromMd == expected3

    let expected4 = LangSymbol(symKind: "", name: "Xyz")
    check "`X Y Z`_".fromRst == expected4
    check "[X Y Z]".fromMd == expected4

  test "simple proc parsing":
    let expected = LangSymbol(symKind: "proc", name: "f")
    check "`proc f`_".fromRst == expected
    check "[proc f]".fromMd == expected

  test "another backticked name":
    let expected = LangSymbol(symKind: "template", name: "type")
    check """`template \`type\``_""".fromRst == expected
    # no backslash in Markdown:
    check """[template `type`]""".fromMd == expected

  test "simple proc parsing with parameters":
    let expected = LangSymbol(symKind: "proc", name: "f",
                              parametersProvided: true)
    check "`proc f*()`_".fromRst == expected
    check "`proc f()`_".fromRst == expected
    check "[proc f*()]".fromMd == expected
    check "[proc f()]".fromMd == expected

  test "symbol parsing with 1 parameter":
    let expected = LangSymbol(symKind: "", name: "f",
                              parameters: @[("G[int]", "")],
                              parametersProvided: true)
    check "`f(G[int])`_".fromRst == expected
    check "[f(G[int])]".fromMd == expected

  test "more proc parsing":
    let input1 = "`proc f[T](x:G[T]):M[T]`_".fromRst
    let input2 = "`proc f[ T ] ( x: G [T] ): M[T]`_".fromRst
    let input3 = "`proc f*[T](x: G[T]): M[T]`_".fromRst
    let expected = LangSymbol(symKind: "proc",
                              name: "f",
                              generics: "[T]",
                              parameters: @[("x", "G[T]")],
                              parametersProvided: true,
                              outType: "M[T]")
    check(input1 == expected)
    check(input2 == expected)
    check(input3 == expected)

  test "advanced proc parsing with Nim identifier normalization":
    let inputRst = """`proc binarySearch*[T, K](a: openarray[T]; key: K;
                       cmp: proc (x: T; y: K): int)`_"""
    let inputMd = """[proc binarySearch*[T, K](a: openarray[T]; key: K;
                       cmp: proc (x: T; y: K): int)]"""
    let expected = LangSymbol(symKind: "proc",
                              name: "binarysearch",
                              generics: "[T,K]",
                              parameters: @[
                                ("a", "openarray[T]"),
                                ("key", "K"),
                                ("cmp", "proc(x:T;y:K):int")],
                              parametersProvided: true,
                              outType: "")
    check(inputRst.fromRst == expected)
    check(inputMd.fromMd == expected)

  test "the same without proc":
    let input = """`binarySearch*[T, K](a: openarray[T]; key: K;
                    cmp: proc (x: T; y: K): int {.closure.})`_"""
    let expected = LangSymbol(symKind: "",
                              name: "binarysearch",
                              generics: "[T,K]",
                              parameters: @[
                                ("a", "openarray[T]"),
                                ("key", "K"),
                                ("cmp", "proc(x:T;y:K):int")],
                              parametersProvided: true,
                              outType: "")
    check(input.fromRst == expected)
    let inputMd = """[binarySearch*[T, K](a: openarray[T]; key: K;
                      cmp: proc (x: T; y: K): int {.closure.})]"""
    check(inputMd.fromMd == expected)

  test "operator $ with and without backticks":
    let input1 = """`func \`$\`*[T](a: \`open Array\`[T]): string`_"""
    let input1md = "[func `$`*[T](a: `open Array`[T]): string]"
    let input2 = """`func $*[T](a: \`open Array\`[T]): string`_"""
    let input2md = "[func $*[T](a: `open Array`[T]): string]"
    let expected = LangSymbol(symKind: "func",
                              name: "$",
                              generics: "[T]",
                              parameters: @[("a", "openarray[T]")],
                              parametersProvided: true,
                              outType: "string")
    check input1.fromRst == expected
    check input2.fromRst == expected
    check input1md.fromMd == expected
    check input2md.fromMd == expected

  test "operator [] with and without backticks":
    let input1 = """`func \`[]\`[T](a: \`open Array\`[T], idx: int): T`_"""
    let input1md = "[func `[]`[T](a: `open Array`[T], idx: int): T]"
    let input2 = """`func [][T](a: \`open Array\`[T], idx: int): T`_"""
    let input2md = "[func [][T](a: `open Array`[T], idx: int): T]"
    let expected = LangSymbol(symKind: "func",
                              name: "[]",
                              generics: "[T]",
                              parameters: @[("a", "openarray[T]"),
                                            ("idx", "int")],
                              parametersProvided: true,
                              outType: "T")
    check input1.fromRst == expected
    check input2.fromRst == expected
    check input1md.fromMd == expected
    check input2md.fromMd == expected

  test "postfix symbol specifier #1":
    let input = "`walkDir iterator`_"
    let inputMd = "[walkDir iterator]"
    let expected = LangSymbol(symKind: "iterator",
                              name: "walkdir")
    check input.fromRst == expected
    check inputMd.fromMd == expected

  test "postfix symbol specifier #2":
    let input1 = """`\`[]\`[T](a: \`open Array\`[T], idx: int): T func`_"""
    let input1md = "[`[]`[T](a: `open Array`[T], idx: int): T func]"
    let input2 = """`[][T](a: \`open Array\`[T], idx: int): T func`_"""
    # note again that ` is needed between 1st and second [
    let input2md = "[`[]`[T](a: `open Array`[T], idx: int): T func]"
    let expected = LangSymbol(symKind: "func",
                              name: "[]",
                              generics: "[T]",
                              parameters: @[("a", "openarray[T]"),
                                            ("idx", "int")],
                              parametersProvided: true,
                              outType: "T")
    check input1.fromRst == expected
    check input2.fromRst == expected
    check input1md.fromMd == expected
    check input2md.fromMd == expected

  test "type of type":
    let inputRst = "`CopyFlag enum`_"
    let inputMd = "[CopyFlag enum]"
    let expected = LangSymbol(symKind: "type",
                              symTypeKind: "enum",
                              name: "Copyflag")
    check inputRst.fromRst == expected
    check inputMd.fromMd == expected

  test "prefixed module":
    let inputRst = "`module std / paths`_"
    let inputMd = "[module std / paths]"
    let expected = LangSymbol(symKind: "module",
                              name: "std/paths")
    check inputRst.fromRst == expected
    check inputMd.fromMd == expected

  test "postfixed module":
    let inputRst = "`std / paths module`_"
    let inputMd = "[std / paths module]"
    let expected = LangSymbol(symKind: "module",
                              name: "std/paths")
    check inputRst.fromRst == expected
    check inputMd.fromMd == expected