fix for subgroup tuple
[akkoma] / test / docs / generator_test.exs
1 defmodule Pleroma.Docs.GeneratorTest do
2 use ExUnit.Case, async: true
3 alias Pleroma.Docs.Generator
4
5 @descriptions [
6 %{
7 group: :pleroma,
8 key: Pleroma.Upload,
9 type: :group,
10 description: "",
11 children: [
12 %{
13 key: :uploader,
14 type: :module,
15 description: "",
16 suggestions:
17 Generator.list_modules_in_dir(
18 "lib/pleroma/upload/filter",
19 "Elixir.Pleroma.Upload.Filter."
20 )
21 },
22 %{
23 key: :filters,
24 type: {:list, :module},
25 description: "",
26 suggestions:
27 Generator.list_modules_in_dir(
28 "lib/pleroma/web/activity_pub/mrf",
29 "Elixir.Pleroma.Web.ActivityPub.MRF."
30 )
31 },
32 %{
33 key: Pleroma.Upload,
34 type: :string,
35 description: "",
36 suggestions: [""]
37 },
38 %{
39 key: :some_key,
40 type: :keyword,
41 description: "",
42 suggestions: [],
43 children: [
44 %{
45 key: :another_key,
46 type: :integer,
47 description: "",
48 suggestions: [5]
49 },
50 %{
51 key: :another_key_with_label,
52 label: "Another label",
53 type: :integer,
54 description: "",
55 suggestions: [7]
56 }
57 ]
58 },
59 %{
60 key: :key1,
61 type: :atom,
62 description: "",
63 suggestions: [
64 :atom,
65 Pleroma.Upload,
66 {:tuple, "string", 8080},
67 [:atom, Pleroma.Upload, {:atom, Pleroma.Upload}]
68 ]
69 },
70 %{
71 key: Pleroma.Upload,
72 label: "Special Label",
73 type: :string,
74 description: "",
75 suggestions: [""]
76 },
77 %{
78 group: {:subgroup, Swoosh.Adapters.SMTP},
79 key: :auth,
80 type: :atom,
81 description: "`Swoosh.Adapters.SMTP` adapter specific setting",
82 suggestions: [:always, :never, :if_available]
83 },
84 %{
85 key: "application/xml",
86 type: {:list, :string},
87 suggestions: ["xml"]
88 }
89 ]
90 },
91 %{
92 group: :tesla,
93 key: :adapter,
94 type: :group,
95 description: ""
96 },
97 %{
98 group: :cors_plug,
99 type: :group,
100 children: [%{key: :key1, type: :string, suggestions: [""]}]
101 },
102 %{group: "Some string group", key: "Some string key", type: :group}
103 ]
104
105 describe "convert_to_strings/1" do
106 test "group, key, label" do
107 [desc1, desc2 | _] = Generator.convert_to_strings(@descriptions)
108
109 assert desc1[:group] == ":pleroma"
110 assert desc1[:key] == "Pleroma.Upload"
111 assert desc1[:label] == "Pleroma.Upload"
112
113 assert desc2[:group] == ":tesla"
114 assert desc2[:key] == ":adapter"
115 assert desc2[:label] == "Adapter"
116 end
117
118 test "group without key" do
119 descriptions = Generator.convert_to_strings(@descriptions)
120 desc = Enum.at(descriptions, 2)
121
122 assert desc[:group] == ":cors_plug"
123 refute desc[:key]
124 assert desc[:label] == "Cors plug"
125 end
126
127 test "children key, label, type" do
128 [%{children: [child1, child2, child3, child4 | _]} | _] =
129 Generator.convert_to_strings(@descriptions)
130
131 assert child1[:key] == ":uploader"
132 assert child1[:label] == "Uploader"
133 assert child1[:type] == :module
134
135 assert child2[:key] == ":filters"
136 assert child2[:label] == "Filters"
137 assert child2[:type] == {:list, :module}
138
139 assert child3[:key] == "Pleroma.Upload"
140 assert child3[:label] == "Pleroma.Upload"
141 assert child3[:type] == :string
142
143 assert child4[:key] == ":some_key"
144 assert child4[:label] == "Some key"
145 assert child4[:type] == :keyword
146 end
147
148 test "child with predefined label" do
149 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
150 child = Enum.at(children, 5)
151 assert child[:key] == "Pleroma.Upload"
152 assert child[:label] == "Special Label"
153 end
154
155 test "subchild" do
156 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
157 child = Enum.at(children, 3)
158 %{children: [subchild | _]} = child
159
160 assert subchild[:key] == ":another_key"
161 assert subchild[:label] == "Another key"
162 assert subchild[:type] == :integer
163 end
164
165 test "subchild with predefined label" do
166 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
167 child = Enum.at(children, 3)
168 %{children: subchildren} = child
169 subchild = Enum.at(subchildren, 1)
170
171 assert subchild[:key] == ":another_key_with_label"
172 assert subchild[:label] == "Another label"
173 end
174
175 test "module suggestions" do
176 [%{children: [%{suggestions: suggestions} | _]} | _] =
177 Generator.convert_to_strings(@descriptions)
178
179 Enum.each(suggestions, fn suggestion ->
180 assert String.starts_with?(suggestion, "Pleroma.")
181 end)
182 end
183
184 test "atoms in suggestions with leading `:`" do
185 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
186 %{suggestions: suggestions} = Enum.at(children, 4)
187 assert Enum.at(suggestions, 0) == ":atom"
188 assert Enum.at(suggestions, 1) == "Pleroma.Upload"
189 assert Enum.at(suggestions, 2) == {":tuple", "string", 8080}
190 assert Enum.at(suggestions, 3) == [":atom", "Pleroma.Upload", {":atom", "Pleroma.Upload"}]
191
192 %{suggestions: suggestions} = Enum.at(children, 6)
193 assert Enum.at(suggestions, 0) == ":always"
194 assert Enum.at(suggestions, 1) == ":never"
195 assert Enum.at(suggestions, 2) == ":if_available"
196 end
197
198 test "group, key as string in main desc" do
199 descriptions = Generator.convert_to_strings(@descriptions)
200 desc = Enum.at(descriptions, 3)
201 assert desc[:group] == "Some string group"
202 assert desc[:key] == "Some string key"
203 end
204
205 test "key as string subchild" do
206 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
207 child = Enum.at(children, 7)
208 assert child[:key] == "application/xml"
209 end
210
211 test "subgroup with module name" do
212 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
213
214 %{group: subgroup} = Enum.at(children, 6)
215 assert subgroup == {":subgroup", "Swoosh.Adapters.SMTP"}
216 end
217 end
218 end