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.

203 lines
5.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. defmodule AutoLinker.ParserTest do
  2. use ExUnit.Case, async: true
  3. doctest AutoLinker.Parser
  4. import AutoLinker.Parser
  5. describe "is_url" do
  6. test "valid scheme true" do
  7. valid_scheme_urls()
  8. |> Enum.each(fn url ->
  9. assert is_url?(url, true)
  10. end)
  11. end
  12. test "invalid scheme true" do
  13. invalid_scheme_urls()
  14. |> Enum.each(fn url ->
  15. refute is_url?(url, true)
  16. end)
  17. end
  18. test "valid scheme false" do
  19. valid_non_scheme_urls()
  20. |> Enum.each(fn url ->
  21. assert is_url?(url, false)
  22. end)
  23. end
  24. test "invalid scheme false" do
  25. invalid_non_scheme_urls()
  26. |> Enum.each(fn url ->
  27. refute is_url?(url, false)
  28. end)
  29. end
  30. end
  31. describe "match_phone" do
  32. test "valid" do
  33. valid_phone_nunbers()
  34. |> Enum.each(fn number ->
  35. assert number |> match_phone() |> valid_number?(number)
  36. end)
  37. end
  38. test "invalid" do
  39. invalid_phone_numbers()
  40. |> Enum.each(fn number ->
  41. assert number |> match_phone() |> is_nil
  42. end)
  43. end
  44. end
  45. describe "parse" do
  46. test "handle line breakes" do
  47. text = "google.com\r\nssss"
  48. expected =
  49. "<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
  50. assert parse(text) == expected
  51. end
  52. test "does not link attributes" do
  53. text = "Check out <a href='google.com'>google</a>"
  54. assert parse(text) == text
  55. text = "Check out <img src='google.com' alt='google.com'/>"
  56. assert parse(text) == text
  57. text = "Check out <span><img src='google.com' alt='google.com'/></span>"
  58. assert parse(text) == text
  59. end
  60. test "does not link inside `<pre>` and `<code>`" do
  61. text = "<pre>google.com</pre>"
  62. assert parse(text) == text
  63. text = "<code>google.com</code>"
  64. assert parse(text) == text
  65. text = "<pre><code>google.com</code></pre>"
  66. assert parse(text) == text
  67. end
  68. test "links url inside html" do
  69. text = "<div>google.com</div>"
  70. expected = "<div><a href=\"http://google.com\">google.com</a></div>"
  71. assert parse(text, class: false, rel: false, new_window: false, phone: false) == expected
  72. text = "Check out <div class='section'>google.com</div>"
  73. expected =
  74. "Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>"
  75. assert parse(text, class: false, rel: false, new_window: false) == expected
  76. end
  77. test "links url inside nested html" do
  78. text = "<p><strong>google.com</strong></p>"
  79. expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>"
  80. assert parse(text, class: false, rel: false, new_window: false) == expected
  81. end
  82. test "excludes html with specified class" do
  83. text = "```Check out <div class='section'>google.com</div>```"
  84. assert parse(text, exclude_patterns: ["```"]) == text
  85. end
  86. test "do not link urls" do
  87. text = "google.com"
  88. assert parse(text, url: false, phone: true) == text
  89. end
  90. test "do not link `:test.test`" do
  91. text = ":test.test"
  92. assert parse(text, %{
  93. scheme: true,
  94. extra: true,
  95. class: false,
  96. strip_prefix: false,
  97. new_window: false,
  98. rel: false
  99. }) == text
  100. end
  101. end
  102. def valid_number?([list], number) do
  103. assert List.last(list) == number
  104. end
  105. def valid_number?(_, _), do: false
  106. def valid_scheme_urls,
  107. do: [
  108. "https://www.example.com",
  109. "http://www2.example.com",
  110. "http://home.example-site.com",
  111. "http://blog.example.com",
  112. "http://www.example.com/product",
  113. "http://www.example.com/products?id=1&page=2",
  114. "http://www.example.com#up",
  115. "http://255.255.255.255",
  116. "http://www.site.com:8008"
  117. ]
  118. def invalid_scheme_urls,
  119. do: [
  120. "http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2"
  121. ]
  122. def valid_non_scheme_urls,
  123. do: [
  124. "www.example.com",
  125. "www2.example.com",
  126. "www.example.com:2000",
  127. "www.example.com?abc=1",
  128. "example.example-site.com",
  129. "example.com",
  130. "example.ca",
  131. "example.tv",
  132. "example.com:999?one=one",
  133. "255.255.255.255",
  134. "255.255.255.255:3000?one=1&two=2"
  135. ]
  136. def invalid_non_scheme_urls,
  137. do: [
  138. "invalid.com/perl.cgi?key= | web-site.com/cgi-bin/perl.cgi?key1=value1&key2",
  139. "invalid.",
  140. "hi..there",
  141. "555.555.5555"
  142. ]
  143. def valid_phone_nunbers,
  144. do: [
  145. "x55",
  146. "x555",
  147. "x5555",
  148. "x12345",
  149. "+1 555 555-5555",
  150. "555 555-5555",
  151. "555.555.5555",
  152. "613-555-5555",
  153. "1 (555) 555-5555",
  154. "(555) 555-5555",
  155. "1.555.555.5555",
  156. "800 555-5555",
  157. "1.800.555.5555",
  158. "1 (800) 555-5555",
  159. "888 555-5555",
  160. "887 555-5555",
  161. "1-877-555-5555",
  162. "1 800 710-5515"
  163. ]
  164. def invalid_phone_numbers,
  165. do: [
  166. "5555",
  167. "x5",
  168. "(555) 555-55"
  169. ]
  170. end