From c77b2c0eadafd62ed798af5b3dfb31eddf062594 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 23 Feb 2019 13:31:09 +0000 Subject: [PATCH 1/5] Fix link text --- lib/auto_linker/builder.ex | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/auto_linker/builder.ex b/lib/auto_linker/builder.ex index cb2d18e..c2d3799 100644 --- a/lib/auto_linker/builder.ex +++ b/lib/auto_linker/builder.ex @@ -6,15 +6,15 @@ defmodule AutoLinker.Builder do @doc """ Create a link. """ - def create_link(url, opts) do - url = add_scheme(url) + def create_link(text, opts) do + url = add_scheme(text) [] |> build_attrs(url, opts, :rel) |> build_attrs(url, opts, :target) |> build_attrs(url, opts, :class) |> build_attrs(url, opts, :href) - |> format_url(url, opts) + |> format_url(text, opts) end def create_markdown_links(text, opts) do @@ -59,7 +59,7 @@ defmodule AutoLinker.Builder do |> truncate(Map.get(opts, :truncate, false)) attrs = format_attrs(attrs) - "" <> url <> "" + "#{url}" end defp format_attrs(attrs) do @@ -93,9 +93,7 @@ defmodule AutoLinker.Builder do defp strip_prefix(url, _), do: url - def create_phone_link([], buffer, _) do - buffer - end + def create_phone_link([], buffer, _), do: buffer def create_phone_link([h | t], buffer, opts) do create_phone_link(t, format_phone_link(h, buffer, opts), opts) From a4184277d00085d5cfc52fc79f2c4c7adf6ef6fe Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Wed, 27 Feb 2019 06:57:39 +0100 Subject: [PATCH 2/5] AutoLinker.BuilderTest: Put tests on all the public functions --- test/builder_test.exs | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/test/builder_test.exs b/test/builder_test.exs index 4272359..8722acd 100644 --- a/test/builder_test.exs +++ b/test/builder_test.exs @@ -4,6 +4,43 @@ defmodule AutoLinker.BuilderTest do import AutoLinker.Builder + test "create_link/2" do + expected = + "text" + + assert create_link("text", %{href: "url"}) == expected + + expected = "text" + assert create_link("text", %{href: "url", rel: nil}) == expected + + expected = "text" + assert create_link("text", %{href: "url", rel: "me"}) == expected + end + + test "create_markdown_links/2" do + expected = + "text" + + assert create_markdown_links("[text](url)", %{}) == expected + end + + test "format_hashtag/3" do + expected = "#girls" + assert format_hashtag(%{href: "/t/girls"}, "girls", nil) == expected + end + + test "format_email/3" do + expected = "mailto:user@example.org" + + assert format_email(%{href: "mailto:user@example.org"}, "mailto:user@example.org", nil) == + expected + end + + test "format_mention/3" do + expected = "@user@host" + format_mention(%{href: "url"}, "user@host", nil) == expected + end + describe "create_phone_link" do test "finishes" do assert create_phone_link([], "", []) == "" @@ -28,4 +65,17 @@ defmodule AutoLinker.BuilderTest do assert create_phone_link([["555.555.5555", ""], ["(555) 888-8888"]], phrase, []) == expected end end + + test "create_mention_link/3" do + expected = + "@navi" + + assert create_mention_link("@navi", "hello @navi", %{mention_prefix: "/u/"}) == expected + end + + test "create_email_link/3" do + expected = "user@example.org" + assert create_email_link("user@example.org", %{}) == expected + assert create_email_link("user@example.org", %{href: "mailto:user@example.org"}) == expected + end end From 204ad610e3a14414d3b4543f6b4693f961213981 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Wed, 27 Feb 2019 18:22:52 +0100 Subject: [PATCH 3/5] AutoLinker.BuilderTest: Match (strange) behaviour --- test/builder_test.exs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/builder_test.exs b/test/builder_test.exs index 8722acd..90bd455 100644 --- a/test/builder_test.exs +++ b/test/builder_test.exs @@ -6,14 +6,16 @@ defmodule AutoLinker.BuilderTest do test "create_link/2" do expected = - "text" + "text" assert create_link("text", %{href: "url"}) == expected - expected = "text" + expected = "text" assert create_link("text", %{href: "url", rel: nil}) == expected - expected = "text" + expected = + "text" + assert create_link("text", %{href: "url", rel: "me"}) == expected end From bdd98ee731c49fe13d5e143bc2517a542aabc308 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Wed, 27 Feb 2019 18:33:06 +0100 Subject: [PATCH 4/5] AutoLinker.BuilderTest: Do not pass overridden href to create_link/2 --- test/builder_test.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/builder_test.exs b/test/builder_test.exs index 90bd455..779c5b2 100644 --- a/test/builder_test.exs +++ b/test/builder_test.exs @@ -8,15 +8,15 @@ defmodule AutoLinker.BuilderTest do expected = "text" - assert create_link("text", %{href: "url"}) == expected + assert create_link("text", %{}) == expected expected = "text" - assert create_link("text", %{href: "url", rel: nil}) == expected + assert create_link("text", %{rel: nil}) == expected expected = "text" - assert create_link("text", %{href: "url", rel: "me"}) == expected + assert create_link("text", %{rel: "me"}) == expected end test "create_markdown_links/2" do From 4dae1eadd5c31ecdb7874b145cf380808a971127 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Fri, 1 Mar 2019 22:49:56 +0100 Subject: [PATCH 5/5] AutoLinker.BuilderTest: Add forgotten assert on format_mention/3 --- test/builder_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/builder_test.exs b/test/builder_test.exs index 779c5b2..0742bd6 100644 --- a/test/builder_test.exs +++ b/test/builder_test.exs @@ -40,7 +40,7 @@ defmodule AutoLinker.BuilderTest do test "format_mention/3" do expected = "@user@host" - format_mention(%{href: "url"}, "user@host", nil) == expected + assert format_mention(%{href: "url"}, "user@host", nil) == expected end describe "create_phone_link" do