Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.8KB

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