Skip to content

Commit 7ef6dfc

Browse files
committed
Fixes to ExUnit.Diff, closes #9926
1 parent e1158a5 commit 7ef6dfc

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ defmodule Kernel.SpecialForms do
1414
forms used to define tuple and binary data structures respectively.
1515
1616
This module also documents macros that return information about Elixir's
17-
compilation environment, such as (`__ENV__/0`, `__MODULE__/0`, `__DIR__/0` and `__CALLER__/0`).
17+
compilation environment, such as (`__ENV__/0`, `__MODULE__/0`, `__DIR__/0`,
18+
`__STACKTRACE__/0`, and `__CALLER__/0`).
1819
1920
Additionally, it documents two special forms, `__block__/1` and
2021
`__aliases__/1`, which are not intended to be called directly by the

lib/ex_unit/lib/ex_unit/diff.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ defmodule ExUnit.Diff do
5757
end
5858

5959
defp diff_quoted({name, _, context} = left, right, env)
60-
when is_atom(name) and is_atom(context) do
60+
when is_atom(name) and is_atom(context) and
61+
name not in [:__MODULE__, :__DIR__, :__STACKTRACE__, :__ENV__, :__CALLER__] do
6162
diff_var(left, right, env)
6263
end
6364

@@ -94,7 +95,8 @@ defmodule ExUnit.Diff do
9495
end
9596
end
9697

97-
defp diff_quoted({:<>, _, _} = left, right, env) when is_binary(right) do
98+
defp diff_quoted({:<>, _, [literal, _]} = left, right, env)
99+
when is_binary(literal) and is_binary(right) do
98100
diff_string_concat(left, right, env)
99101
end
100102

@@ -761,11 +763,10 @@ defmodule ExUnit.Diff do
761763
String.bag_distance(left, right) > 0.4
762764
end
763765

764-
defp parse_string({:<>, _, [literal, rest]}) do
766+
defp parse_string({:<>, _, [literal, rest]}) when is_binary(literal) do
765767
{parsed, quoted, indexes, parsed_length} = parse_string(rest)
766768
literal_length = String.length(literal)
767769
length = literal_length + parsed_length
768-
769770
{literal <> parsed, quoted, [literal_length | indexes], length}
770771
end
771772

lib/ex_unit/test/ex_unit/diff_test.exs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ defmodule ExUnit.DiffTest do
141141
refute_diff(^b = :a, "-^b-", "+:a+", pins)
142142
end
143143

144+
test "pseudo vars" do
145+
assert_diff(__MODULE__ = ExUnit.DiffTest, [])
146+
refute_diff(__MODULE__ = SomethingElse, "-__MODULE__-", "+SomethingElse+")
147+
end
148+
144149
test "integers" do
145150
assert_diff(123 = 123, [])
146151
assert_diff(-123 = -123, [])
@@ -749,7 +754,7 @@ defmodule ExUnit.DiffTest do
749754
)
750755
end
751756

752-
test "concat operator" do
757+
test "concat binaries" do
753758
assert_diff("fox hops" <> " over the dog" = "fox hops over the dog", [])
754759
assert_diff("fox hops " <> "over " <> "the dog" = "fox hops over the dog", [])
755760

@@ -783,6 +788,10 @@ defmodule ExUnit.DiffTest do
783788
~s/"fox hops over the dog"/
784789
)
785790

791+
refute_diff("fox" <> " hops" = :a, ~s/-"fox" <> " hops"-/, "+:a+")
792+
end
793+
794+
test "concat binaries with pin" do
786795
pins = %{{:x, nil} => " over the dog"}
787796

788797
assert_diff("fox hops" <> x = "fox hops over the dog", x: " over the dog")
@@ -801,8 +810,16 @@ defmodule ExUnit.DiffTest do
801810
~s/"fox hops over +t+he dog"/,
802811
pins
803812
)
813+
end
804814

805-
refute_diff("fox" <> " hops" = :a, ~s/-"fox" <> " hops"-/, "+:a+")
815+
test "concat binaries with specifiers" do
816+
input = "foobar"
817+
818+
refute_diff(
819+
<<trap::binary-size(3)>> <> "baz" = input,
820+
"-<<trap::binary-size(3)>> <> \"baz\"-",
821+
"+\"foobar\"+"
822+
)
806823
end
807824

808825
test "underscore" do

0 commit comments

Comments
 (0)