File: search_data_test.exs

package info (click to toggle)
elixir-ex-doc 0.35.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,784 kB
  • sloc: javascript: 2,848; makefile: 15; xml: 12; sh: 5
file content (260 lines) | stat: -rw-r--r-- 6,712 bytes parent folder | download
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
defmodule ExDoc.Formatter.HTML.SearchDataTest do
  use ExUnit.Case, async: true
  import TestHelper

  @moduletag :tmp_dir

  test "Elixir module", c do
    modules =
      elixirc(c, ~S'''
      defmodule SearchFoo do
        @moduledoc """
        Hello.

        ## Section 1

        Section Content 1.

        #### Section 1a

        Section Content 1a.
        """
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    data = search_data(modules, config)
    assert data["content_type"] == "text/markdown"
    assert data["proglang"] == "elixir"
    assert data["producer"]["name"] == "ex_doc"
    [item1, item2] = data["items"]

    assert item1["ref"] == "SearchFoo.html"
    assert item1["type"] == "module"
    assert item1["title"] == "SearchFoo"
    assert item1["doc"] == "Hello."

    assert item2["ref"] == "SearchFoo.html#module-section-1"
    assert item2["type"] == "module"
    assert item2["title"] == "Section 1 - SearchFoo"

    assert item2["doc"] == """
           Section Content 1.

           #### Section 1a

           Section Content 1a.\
           """
  end

  test "Mix task", c do
    modules =
      elixirc(c, ~S'''
      defmodule Mix.Tasks.SearchItemTest do
        @moduledoc """
        Test task.

        ## Section 1

            $ mix search_item_test
        """
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    [item1, item2] = search_data(modules, config)["items"]

    assert item1["ref"] == "Mix.Tasks.SearchItemTest.html"
    assert item1["type"] == "task"
    assert item1["title"] == "mix search_item_test"
    assert item1["doc"] == "Test task."

    assert item2["ref"] == "Mix.Tasks.SearchItemTest.html#module-section-1"
    assert item2["type"] == "task"
    assert item2["title"] == "Section 1 - mix search_item_test"
    assert item2["doc"] == "$ mix search_item_test"
  end

  test "module with no docs", c do
    modules =
      elixirc(c, ~S'''
      defmodule SearchFoo do
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    [item] = search_data(modules, config)["items"]
    assert item["ref"] == "SearchFoo.html"
    assert item["type"] == "module"
    assert item["title"] == "SearchFoo"
    assert item["doc"] == ""
  end

  test "escaping", c do
    modules =
      elixirc(c, ~S'''
      defmodule SearchFoo do
        @moduledoc ~S"""
        #{}
        """
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    [item] = search_data(modules, config)["items"]

    assert item["doc"] == ~S"#{}"
  end

  @tag :otp_eep48
  test "Erlang module", c do
    [module] =
      erlc(c, :search_foo, """
      %% @doc
      %% Hello <em>world</em>.
      -module(search_foo).
      """)

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc", proglang: :erlang}
    data = search_data([module], config)
    assert data["content_type"] == "text/markdown"
    [item] = data["items"]
    assert item["ref"] == "search_foo.html"
    assert item["type"] == "module"
    assert item["title"] == "search_foo"
    assert item["doc"] == "Hello world ."
  end

  test "function", c do
    modules =
      elixirc(c, ~S'''
      defmodule SearchFoo do
        @doc """
        Hello *world*.
        """
        def foo, do: :ok
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    [%{"type" => "module"}, item] = search_data(modules, config)["items"]
    assert item["ref"] == "SearchFoo.html#foo/0"
    assert item["type"] == "function"
    assert item["title"] == "SearchFoo.foo/0"
    assert item["doc"] == "Hello *world*."
  end

  test "callback", c do
    modules =
      elixirc(c, ~S'''
      defmodule SearchFoo do
        @doc """
        Handles _foo_.

        ## Section

        Section content.
        """
        @callback handle_foo() :: :ok
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    [%{"type" => "behaviour"}, item1, item2] = search_data(modules, config)["items"]
    assert item1["ref"] == "SearchFoo.html#c:handle_foo/0"
    assert item1["type"] == "callback"
    assert item1["title"] == "SearchFoo.handle_foo/0"
    assert item1["doc"] == "Handles _foo_."

    assert item2["ref"] == "SearchFoo.html#c:handle_foo/0-section"
    assert item2["type"] == "callback"
    assert item2["title"] == "Section - SearchFoo.handle_foo/0"
    assert item2["doc"] == "Section content."
  end

  test "type", c do
    modules =
      elixirc(c, ~S'''
      defmodule SearchFoo do
        @typedoc """
        The _foo_ type.
        """
        @type foo() :: atom()
      end
      ''')

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc"}
    [%{"type" => "module"}, item] = search_data(modules, config)["items"]
    assert item["ref"] == "SearchFoo.html#t:foo/0"
    assert item["type"] == "type"
    assert item["title"] == "SearchFoo.foo/0"
    assert item["doc"] == "The _foo_ type."
  end

  test "extras", c do
    readme_path = "#{c.tmp_dir}/README.md"

    File.write!(readme_path, """
    # Foo

    _Foo_ content.

    ## Section 1 Header

    Section _1_ content.
    """)

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc", extras: [readme_path]}
    [item1, item2] = search_data([], config)["items"]

    assert item1["ref"] == "readme.html"
    assert item1["type"] == "extras"
    assert item1["title"] == "Foo"
    assert item1["doc"] == "# Foo\n\n_Foo_ content."

    assert item2["ref"] == "readme.html#section-1-header"
    assert item2["type"] == "extras"
    assert item2["title"] == "Section 1 Header - Foo"
    assert item2["doc"] == "Section _1_ content."
  end

  test "drops vega-lite blocks", c do
    readme_path = "#{c.tmp_dir}/README.md"

    File.write!(readme_path, """
    # Foo

    _Foo_ content.

    ## Section 1 Header

    ```vega-lite
      graph
    ```

    Section _1_ content.
    """)

    config = %ExDoc.Config{output: "#{c.tmp_dir}/doc", extras: [readme_path]}
    [item1, item2] = search_data([], config)["items"]

    assert item1["ref"] == "readme.html"
    assert item1["type"] == "extras"
    assert item1["title"] == "Foo"
    assert item1["doc"] == "# Foo\n\n_Foo_ content."

    assert item2["ref"] == "readme.html#section-1-header"
    assert item2["type"] == "extras"
    assert item2["title"] == "Section 1 Header - Foo"
    assert item2["doc"] == "Section _1_ content."
  end

  defp search_data(modules, config) do
    {modules, []} = ExDoc.Retriever.docs_from_modules(modules, config)

    ExDoc.Formatter.HTML.run(modules, [], config)
    [path] = Path.wildcard(Path.join([config.output, "dist", "search_data-*.js"]))
    "searchData=" <> json = File.read!(path)
    Jason.decode!(json)
  end
end