logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

generator_test.exs (6746B)


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