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.

178 lines
5.3 KiB

  1. defmodule Microformats2RelsTest do
  2. use ExUnit.Case
  3. doctest Microformats2.Rels
  4. test "parse successfully parses rels" do
  5. assert(
  6. %{items: _, rel_urls: %{"http://blub" => %{rels: ["me"], text: "blub"}}, rels: %{"me" => ["http://blub"]}} =
  7. Microformats2.parse("<a rel=\"me\" href=\"http://blub\">blub</a>", "http://localhost")
  8. )
  9. end
  10. test "parse successfully parses multiple rels" do
  11. assert(
  12. %{
  13. items: _,
  14. rel_urls: %{"http://blub" => %{rels: ["me"], text: "blub"}, "http://blah" => %{rels: ["me"], text: "blub"}},
  15. rels: %{"me" => ["http://blub", "http://blah"]}
  16. } =
  17. Microformats2.parse(
  18. """
  19. <a rel=\"me\" href=\"http://blub\">blub</a>
  20. <a rel=\"me\" href=\"http://blah\">blub</a>
  21. """,
  22. "http://localhost"
  23. )
  24. )
  25. end
  26. test "parse only saves one URL" do
  27. assert(
  28. %{items: _, rel_urls: %{"http://blub" => %{rels: ["me"], text: "blub"}}, rels: %{"me" => ["http://blub"]}} =
  29. Microformats2.parse(
  30. """
  31. <a rel=\"me\" href=\"http://blub\">blub</a>
  32. <a rel=\"me\" href=\"http://blub\">blub</a>
  33. """,
  34. "http://localhost"
  35. )
  36. )
  37. end
  38. test "parse saves all rels" do
  39. assert(
  40. %{
  41. items: _,
  42. rel_urls: %{"http://blub" => %{rels: ["me", "moo"], text: "blub"}},
  43. rels: %{"me" => ["http://blub"], "moo" => ["http://blub"]}
  44. } =
  45. Microformats2.parse(
  46. """
  47. <a rel=\"me\" href=\"http://blub\">blub</a>
  48. <a rel=\"moo\" href=\"http://blub\">blub</a>
  49. """,
  50. "http://localhost"
  51. )
  52. )
  53. end
  54. test "parse successfully parses rels with attributes" do
  55. assert(
  56. %{
  57. items: _,
  58. rel_urls: %{"http://blub" => %{rels: ["me"], media: "video", text: "blub"}},
  59. rels: %{"me" => ["http://blub"]}
  60. } = Microformats2.parse("<a rel=\"me\" media=\"video\" href=\"http://blub\">blub</a>", "http://localhost")
  61. )
  62. assert(
  63. %{
  64. items: _,
  65. rel_urls: %{"http://blub" => %{rels: ["me"], hreflang: "de", text: "blub"}},
  66. rels: %{"me" => ["http://blub"]}
  67. } = Microformats2.parse("<a rel=\"me\" hreflang=\"de\" href=\"http://blub\">blub</a>", "http://localhost")
  68. )
  69. assert(
  70. %{
  71. items: _,
  72. rel_urls: %{"http://blub" => %{rels: ["me"], title: "blub", text: "blub"}},
  73. rels: %{"me" => ["http://blub"]}
  74. } = Microformats2.parse("<a rel=\"me\" title=\"blub\" href=\"http://blub\">blub</a>", "http://localhost")
  75. )
  76. assert(
  77. %{
  78. items: _,
  79. rel_urls: %{"http://blub" => %{rels: ["me"], type: "text/html", text: "blub"}},
  80. rels: %{"me" => ["http://blub"]}
  81. } = Microformats2.parse("<a rel=\"me\" type=\"text/html\" href=\"http://blub\">blub</a>", "http://localhost")
  82. )
  83. assert(
  84. %{
  85. items: _,
  86. rel_urls: %{
  87. "http://blub" => %{
  88. rels: ["me"],
  89. media: "video",
  90. title: "blub",
  91. hreflang: "de",
  92. type: "text/html",
  93. text: "blub"
  94. }
  95. },
  96. rels: %{"me" => ["http://blub"]}
  97. } =
  98. Microformats2.parse(
  99. "<a rel=\"me\" hreflang=\"de\" media=\"video\" title=\"blub\" type=\"text/html\" href=\"http://blub\">blub</a>",
  100. "http://localhost"
  101. )
  102. )
  103. end
  104. test "duplicate value doesn't overwrite the first one" do
  105. assert(
  106. %{
  107. items: _,
  108. rel_urls: %{"http://blub" => %{rels: ["me"], text: "blub", hreflang: "de"}},
  109. rels: %{"me" => ["http://blub"]}
  110. } =
  111. Microformats2.parse(
  112. """
  113. <a rel="me" hreflang="de" href="http://blub">blub</a>
  114. <a rel="me" hreflang="en" href="http://blub">blah</a>
  115. """,
  116. "http://localhost"
  117. )
  118. )
  119. end
  120. test "parse ignores template elements" do
  121. assert(
  122. %{items: _, rel_urls: %{"http://blub" => %{rels: ["me"], text: "blub"}}, rels: %{"me" => ["http://blub"]}} =
  123. Microformats2.parse(
  124. """
  125. <a rel="me" href="http://blub">blub</a>
  126. <template><a rel="moo" href="http://blub">blub</a></template>
  127. """,
  128. "http://localhost"
  129. )
  130. )
  131. end
  132. test "parse generates an absolute URL" do
  133. assert(
  134. %{
  135. items: _,
  136. rel_urls: %{"http://localhost/blub" => %{rels: ["me"], text: "blub"}},
  137. rels: %{"me" => ["http://localhost/blub"]}
  138. } = Microformats2.parse("<a rel=\"me\" href=\"/blub\">blub</a>", "http://localhost")
  139. )
  140. assert(
  141. %{
  142. items: _,
  143. rel_urls: %{"http://localhost/blub" => %{rels: ["me"], text: "blub"}},
  144. rels: %{"me" => ["http://localhost/blub"]}
  145. } = Microformats2.parse("<a rel=\"me\" href=\"blub\">blub</a>", "http://localhost")
  146. )
  147. assert(
  148. %{
  149. items: _,
  150. rel_urls: %{"http://localhost/blah/blub" => %{rels: ["me"], text: "blub"}},
  151. rels: %{"me" => ["http://localhost/blah/blub"]}
  152. } = Microformats2.parse("<a rel=\"me\" href=\"blub\">blub</a>", "http://localhost/blah/foo")
  153. )
  154. assert(
  155. %{
  156. items: _,
  157. rel_urls: %{"http://localhost/blub" => %{rels: ["me"], text: "blub"}},
  158. rels: %{"me" => ["http://localhost/blub"]}
  159. } = Microformats2.parse("<a rel=\"me\" href=\"/blub\">blub</a>", "http://localhost/blah/foo")
  160. )
  161. end
  162. end