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.

284 lines
9.9 KiB

  1. defmodule Microformats2ItemsTest do
  2. use ExUnit.Case
  3. doctest Microformats2.Items
  4. test "successfully parses a whole document" do
  5. str = """
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <title>Blub</title>
  10. </head>
  11. <body>
  12. <h1>Blah</h1>
  13. <article class="h-card">
  14. <span class="p-name">Luke <span>lulu</span></span>
  15. <a href="blub" class="u-url">blah</a>
  16. </article>
  17. </body>
  18. </html>
  19. """
  20. ret = Microformats2.parse(str, "http://localhost")
  21. assert not Enum.empty?(ret[:items])
  22. end
  23. test "minimal h-card" do
  24. assert %{rels: _, rel_urls: _, items: [%{type: ["h-card"], properties: %{name: ["Frances Berriman"]}}]} =
  25. Microformats2.parse("<span class=\"h-card\">Frances Berriman</span>", "http://localhost")
  26. assert %{
  27. rels: _,
  28. rel_urls: _,
  29. items: [%{type: ["h-card"], properties: %{name: ["Ben Ward"], url: ["http://benward.me"]}}]
  30. } = Microformats2.parse("<a class=\"h-card\" href=\"http://benward.me\">Ben Ward</a>", "http://localhost")
  31. assert %{
  32. rels: _,
  33. rel_urls: _,
  34. items: [
  35. %{
  36. type: ["h-card"],
  37. properties: %{
  38. name: ["Rohit Khare"],
  39. url: ["http://rohit.khare.org/"],
  40. photo: [
  41. "https://s3.amazonaws.com/twitter_production/profile_images/53307499/180px-Rohit-sq_bigger.jpg"
  42. ]
  43. }
  44. }
  45. ]
  46. } =
  47. Microformats2.parse(
  48. """
  49. <a class="h-card" href="http://rohit.khare.org/">
  50. <img alt="Rohit Khare"
  51. src="https://s3.amazonaws.com/twitter_production/profile_images/53307499/180px-Rohit-sq_bigger.jpg">
  52. </a>
  53. """,
  54. "http://localhost"
  55. )
  56. end
  57. test "successfully parses a h-card with author name" do
  58. {:ok, str} = File.read("./test/documents/h_card_with_author.html")
  59. assert %{
  60. rels: _,
  61. rel_urls: _,
  62. items: [
  63. %{
  64. type: ["h-card"],
  65. properties: %{
  66. photo: ["https://webfwd.org/content/about-experts/300.mitchellbaker/mentor_mbaker.jpg"],
  67. name: ["Mitchell Baker"],
  68. url: ["http://blog.lizardwrangler.com/", "https://twitter.com/MitchellBaker"],
  69. org: ["Mozilla Foundation"],
  70. note: [
  71. "Mitchell is responsible for setting the direction and scope of the Mozilla Foundation and its activities."
  72. ],
  73. category: ["Strategy", "Leadership"]
  74. }
  75. }
  76. ]
  77. } = Microformats2.parse(str, "http://localhost")
  78. end
  79. test "successfully parses a h-event combined with h-card" do
  80. {:ok, str} = File.read("./test/documents/h_event_combined_h_card.html")
  81. assert %{
  82. rels: _,
  83. rel_urls: _,
  84. items: [
  85. %{
  86. type: ["h-event"],
  87. properties: %{
  88. name: ["IndieWebCamp 2012"],
  89. url: ["http://indiewebcamp.com/2012"],
  90. start: ["2012-06-30"],
  91. end: ["2012-07-01"],
  92. location: [
  93. %{
  94. value: "Geoloqi",
  95. type: ["h-card"],
  96. properties: %{
  97. name: ["Geoloqi"],
  98. org: ["Geoloqi"],
  99. url: ["http://geoloqi.com/"],
  100. street_address: ["920 SW 3rd Ave. Suite 400"],
  101. locality: ["Portland"],
  102. region: ["Oregon"]
  103. }
  104. }
  105. ]
  106. }
  107. }
  108. ]
  109. } = Microformats2.parse(str, "http://localhost")
  110. end
  111. test "successfully parses a h-card with org" do
  112. {:ok, str} = File.read("./test/documents/h_card_org.html")
  113. assert %{
  114. rels: _,
  115. rel_urls: _,
  116. items: [
  117. %{
  118. type: ["h-card"],
  119. properties: %{
  120. name: ["Mitchell Baker"],
  121. url: ["http://blog.lizardwrangler.com/"],
  122. org: ["Mozilla Foundation"]
  123. }
  124. }
  125. ]
  126. } = Microformats2.parse(str, "http://localhost")
  127. end
  128. test "successfully parses a h-card with h-card and org" do
  129. {:ok, str} = File.read("./test/documents/h_card_with_h_card_org.html")
  130. assert %{
  131. rels: _,
  132. rel_urls: _,
  133. items: [
  134. %{
  135. type: ["h-card"],
  136. properties: %{
  137. name: ["Mitchell Baker"],
  138. url: ["http://blog.lizardwrangler.com/"],
  139. org: [
  140. %{
  141. value: "Mozilla Foundation",
  142. type: ["h-card"],
  143. properties: %{name: ["Mozilla Foundation"], url: ["http://mozilla.org/"]}
  144. }
  145. ]
  146. }
  147. }
  148. ]
  149. } = Microformats2.parse(str, "http://localhost")
  150. end
  151. test "successfully parses a nested h-card h-org h-card" do
  152. {:ok, str} = File.read("./test/documents/nested_h_card_h_org_h_card.html")
  153. assert %{
  154. rels: _,
  155. rel_urls: _,
  156. items: [
  157. %{
  158. type: ["h-card"],
  159. properties: %{
  160. name: ["Mitchell Baker"],
  161. url: ["http://blog.lizardwrangler.com/"],
  162. org: [
  163. %{
  164. value: "Mozilla Foundation",
  165. type: ["h-card", "h-org"],
  166. properties: %{name: ["Mozilla Foundation"], url: ["http://mozilla.org/"]}
  167. }
  168. ]
  169. }
  170. }
  171. ]
  172. } = Microformats2.parse(str, "http://localhost")
  173. end
  174. test "successfully parses a nested h-card w/o attached property" do
  175. {:ok, str} = File.read("./test/documents/h_card_org_h_card.html")
  176. assert %{
  177. rels: _,
  178. rel_urls: _,
  179. items: [
  180. %{
  181. type: ["h-card"],
  182. properties: %{name: ["Mitchell Baker"], url: ["http://blog.lizardwrangler.com/"]},
  183. children: [
  184. %{type: ["h-card"], properties: %{name: ["Mozilla Foundation"], url: ["http://mozilla.org/"]}}
  185. ]
  186. }
  187. ]
  188. } = Microformats2.parse(str, "http://localhost")
  189. end
  190. test "resolves explicit url to absolute URL" do
  191. assert %{
  192. rels: _,
  193. rel_urls: _,
  194. items: [%{type: ["h-card"], properties: %{name: ["Ben Ward"], url: ["http://benward.me/foo"]}}]
  195. } =
  196. Microformats2.parse(
  197. "<div class=\"h-card\"><a class=\"u-url\" href=\"/foo\">Ben Ward</a></div>",
  198. "http://benward.me"
  199. )
  200. assert %{
  201. rels: _,
  202. rel_urls: _,
  203. items: [%{type: ["h-card"], properties: %{name: ["Ben Ward"], url: ["http://benward.me/foo"]}}]
  204. } = Microformats2.parse("<div class=\"h-card\"><a href=\"/foo\">Ben Ward</a></div>", "http://benward.me")
  205. end
  206. test "jeena entry" do
  207. {:ok, str} = File.read("./test/documents/real_world_note.html")
  208. assert %{
  209. rels: _,
  210. rel_urls: _,
  211. items: [
  212. %{
  213. properties: %{
  214. author: [
  215. %{
  216. properties: %{
  217. name: ["Jeena"],
  218. photo: ["http://localhost/avatar.jpg"],
  219. url: ["http://localhost/"]
  220. },
  221. type: ["h-card"],
  222. value: "Jeena"
  223. }
  224. ],
  225. comment: [
  226. %{
  227. properties: %{
  228. author: [
  229. %{
  230. properties: %{
  231. name: ["Christian Kruse"],
  232. photo: [
  233. "http://localhost/cache?size=40x40>&url=https%3A%2F%2Fwwwtech.de%2Fimages%2Fchristian-kruse-242470c34a3671da4cab3e3b0d941729.jpg%3Fvsn%3Dd"
  234. ],
  235. url: ["https://wwwtech.de/notes/132"]
  236. },
  237. type: ["h-card"],
  238. value: "Christian Kruse"
  239. }
  240. ],
  241. content: [%{html: "Of course he is!", text: "Of course he is!"}],
  242. name: ["Christian Kruse,\n\t\t 4 days ago\n\t\t Of course he is!"],
  243. published: ["2016-02-19T10:50:17Z"],
  244. url: ["https://wwwtech.de/notes/132"]
  245. },
  246. type: ["h-cite"],
  247. value: "Christian Kruse,\n\t\t 4 days ago\n\t\t Of course he is!"
  248. }
  249. ],
  250. content: [%{html: "<p>He's right, you know?</p>", text: "He's right, you know?"}],
  251. in_reply_to: ["https://wwwtech.de/pictures/51"],
  252. name: ["Note #587"],
  253. published: ["2016-02-18T19:33:25Z"],
  254. updated: ["2016-02-18T19:33:25Z"],
  255. url: ["http://localhost/comments/587"]
  256. },
  257. type: ["h-as-note", "h-entry"]
  258. }
  259. ]
  260. } = Microformats2.parse(str, "http://localhost")
  261. end
  262. end