Skip to content

Commit 6ba40a7

Browse files
committed
Simplify tests
1 parent d8e5fcb commit 6ba40a7

File tree

5 files changed

+22
-44
lines changed

5 files changed

+22
-44
lines changed

lib/ex_doc/formatter/html.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ defmodule ExDoc.Formatter.HTML do
387387

388388
source_path = input |> Path.relative_to(File.cwd!()) |> String.replace_leading("./", "")
389389

390-
source_url = Utils.source_url_pattern(source_url_pattern, source_path, "1")
390+
source_url = Utils.source_url_pattern(source_url_pattern, source_path, 1)
391391

392392
%{
393393
id: id,

lib/ex_doc/retriever.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ defmodule ExDoc.Retriever do
336336
defp source_link(%{path: _, url: nil}, _line), do: nil
337337

338338
defp source_link(source, line) do
339-
Utils.source_url_pattern(source.url, source.path, to_string(line))
339+
Utils.source_url_pattern(source.url, source.path, line)
340340
end
341341

342342
defp source_path(module, _config) do

lib/ex_doc/utils.ex

+15-8
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,22 @@ defmodule ExDoc.Utils do
9191
Integer.to_string(integer)
9292
end
9393

94-
def source_url_pattern(source_url_pattern, path, line) do
95-
if is_function(source_url_pattern) do
96-
source_url_pattern.(path, line)
97-
else
98-
if url = source_url_pattern do
99-
url
94+
@doc """
95+
Generates a url based on the given pattern.
96+
"""
97+
def source_url_pattern(source_url_pattern, path, line)
98+
when is_binary(path) and is_integer(line) do
99+
cond do
100+
is_function(source_url_pattern) ->
101+
source_url_pattern.(path, line)
102+
103+
source_url_pattern ->
104+
source_url_pattern
100105
|> String.replace("%{path}", path)
101-
|> String.replace("%{line}", line)
102-
end
106+
|> String.replace("%{line}", Integer.to_string(line))
107+
108+
true ->
109+
nil
103110
end
104111
end
105112
end

test/ex_doc/retriever/source_pattern_test.exs

-34
This file was deleted.

test/ex_doc/utils_test.exs

+5
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,9 @@ defmodule ExDoc.UtilsTest do
8282
}\
8383
"""
8484
end
85+
86+
test "source_url_pattern" do
87+
assert ExDoc.Utils.source_url_pattern("/%{path}-%{line}", "lib/foo", 13) == "/lib/foo-13"
88+
assert ExDoc.Utils.source_url_pattern(&"/#{&1}:#{&2}", "lib/foo", 13) == "/lib/foo:13"
89+
end
8590
end

0 commit comments

Comments
 (0)