Skip to content

Commit a8c9206

Browse files
authored
Format inserted/deleted maps in list assertions (#13954)
1 parent d7f8578 commit a8c9206

File tree

2 files changed

+196
-10
lines changed

2 files changed

+196
-10
lines changed

lib/ex_unit/lib/ex_unit/diff.ex

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,9 +1142,22 @@ defmodule ExUnit.Diff do
11421142
defp maybe_escape(other, %{context: :match}), do: other
11431143
defp maybe_escape(other, _env), do: escape(other)
11441144

1145-
# We escape container types to make a distinction between AST
1146-
# and values that should be inspected. All other values have no
1147-
# special AST representation, so we can keep them as is.
1145+
# We escape container types to make a distinction between AST and values that
1146+
# should be inspected. Maps and structs without custom inspect implementation
1147+
# should not be inspected, convert it to ast.
1148+
# All other values have no special AST representation, so we can keep them as is.
1149+
defp escape(other) when is_map(other) do
1150+
struct = maybe_struct(other)
1151+
1152+
if struct && Inspect.impl_for(other) not in [Inspect.Any, Inspect.Map] do
1153+
other
1154+
else
1155+
other
1156+
|> Map.to_list()
1157+
|> build_map_or_struct(struct)
1158+
end
1159+
end
1160+
11481161
defp escape(other) when is_list(other) or is_tuple(other), do: {other}
11491162
defp escape(other), do: other
11501163

lib/ex_unit/test/ex_unit/diff_test.exs

Lines changed: 180 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ defmodule ExUnit.DiffTest do
1010
defstruct [:name, :age]
1111
end
1212

13+
defmodule Customer do
14+
defstruct [:address, :age, :first_name, :language, :last_name, :notifications]
15+
end
16+
1317
defmodule Person do
1418
defstruct [:age]
1519
end
@@ -126,10 +130,11 @@ defmodule ExUnit.DiffTest do
126130
assert env_binding == expected_binding
127131
end
128132

133+
@terminal_width 80
129134
defp to_diff(side, sign) do
130135
side
131136
|> Diff.to_algebra(&diff_wrapper(&1, sign))
132-
|> Algebra.format(:infinity)
137+
|> Algebra.format(@terminal_width)
133138
|> IO.iodata_to_binary()
134139
end
135140

@@ -682,13 +687,49 @@ defmodule ExUnit.DiffTest do
682687
refute_diff(
683688
%User{age: %Date{}} = struct,
684689
~s/%ExUnit.DiffTest.User{age: %-Date-{}}/,
685-
~s/%ExUnit.DiffTest.User{age: %+DateTime+{calendar: Calendar.ISO, day: 30, hour: 13, microsecond: {253158, 6}, minute: 49, month: 7, second: 59, std_offset: 0, time_zone: "Etc\/UTC", utc_offset: 0, year: 2020, zone_abbr: "UTC"}, name: nil}/
690+
"""
691+
%ExUnit.DiffTest.User{
692+
age: %+DateTime+{
693+
calendar: Calendar.ISO,
694+
day: 30,
695+
hour: 13,
696+
microsecond: {253158, 6},
697+
minute: 49,
698+
month: 7,
699+
second: 59,
700+
std_offset: 0,
701+
time_zone: "Etc\/UTC",
702+
utc_offset: 0,
703+
year: 2020,
704+
zone_abbr: "UTC"
705+
},
706+
name: nil
707+
}\
708+
"""
686709
)
687710

688711
refute_diff(
689712
%{age: %Date{}} = struct,
690713
~s/%{age: %-Date-{}}/,
691-
~s/%ExUnit.DiffTest.User{age: %+DateTime+{calendar: Calendar.ISO, day: 30, hour: 13, microsecond: {253158, 6}, minute: 49, month: 7, second: 59, std_offset: 0, time_zone: "Etc\/UTC", utc_offset: 0, year: 2020, zone_abbr: "UTC"}, name: nil}/
714+
"""
715+
%ExUnit.DiffTest.User{
716+
age: %+DateTime+{
717+
calendar: Calendar.ISO,
718+
day: 30,
719+
hour: 13,
720+
microsecond: {253158, 6},
721+
minute: 49,
722+
month: 7,
723+
second: 59,
724+
std_offset: 0,
725+
time_zone: \"Etc/UTC\",
726+
utc_offset: 0,
727+
year: 2020,
728+
zone_abbr: \"UTC\"
729+
},
730+
name: nil
731+
}\
732+
"""
692733
)
693734
end
694735

@@ -836,6 +877,110 @@ defmodule ExUnit.DiffTest do
836877
)
837878
end
838879

880+
test "maps in lists" do
881+
map = %{
882+
address: %{
883+
street: "123 Main St",
884+
city: "Springfield",
885+
state: "IL",
886+
zip: "62701"
887+
},
888+
age: 30,
889+
first_name: "John",
890+
language: "en-US",
891+
last_name: "Doe",
892+
notifications: true
893+
}
894+
895+
refute_diff(
896+
[map] == [],
897+
"""
898+
[
899+
-%{
900+
address: %{state: "IL", zip: "62701", street: "123 Main St", city: "Springfield"},
901+
age: 30,
902+
first_name: "John",
903+
language: "en-US",
904+
last_name: "Doe",
905+
notifications: true
906+
}-
907+
]\
908+
""",
909+
"[]"
910+
)
911+
912+
refute_diff(
913+
[] == [map],
914+
"[]",
915+
"""
916+
[
917+
+%{
918+
address: %{state: "IL", zip: "62701", street: "123 Main St", city: "Springfield"},
919+
age: 30,
920+
first_name: "John",
921+
language: "en-US",
922+
last_name: "Doe",
923+
notifications: true
924+
}+
925+
]\
926+
"""
927+
)
928+
929+
assert_diff([map] == [map], [])
930+
end
931+
932+
test "structs in lists" do
933+
customer = %Customer{
934+
address: %{
935+
street: "123 Main St",
936+
city: "Springfield",
937+
state: "IL",
938+
zip: "62701"
939+
},
940+
age: 30,
941+
first_name: "John",
942+
language: "en-US",
943+
last_name: "Doe",
944+
notifications: true
945+
}
946+
947+
refute_diff(
948+
[customer] == [],
949+
"""
950+
[
951+
-%ExUnit.DiffTest.Customer{
952+
address: %{state: "IL", zip: "62701", street: "123 Main St", city: "Springfield"},
953+
age: 30,
954+
first_name: "John",
955+
language: "en-US",
956+
last_name: "Doe",
957+
notifications: true
958+
}-
959+
]\
960+
""",
961+
"[]"
962+
)
963+
964+
refute_diff(
965+
[] == [customer],
966+
"[]",
967+
"""
968+
[
969+
+%ExUnit.DiffTest.Customer{
970+
address: %{state: "IL", zip: "62701", street: "123 Main St", city: "Springfield"},
971+
age: 30,
972+
first_name: "John",
973+
language: "en-US",
974+
last_name: "Doe",
975+
notifications: true
976+
}+
977+
]\
978+
"""
979+
)
980+
981+
assert_diff([customer] == [customer], [])
982+
end
983+
839984
test "maps and structs with escaped values" do
840985
refute_diff(
841986
%User{age: {1, 2, 3}} = %User{age: {1, 2, 4}},
@@ -1082,8 +1227,18 @@ defmodule ExUnit.DiffTest do
10821227

10831228
refute_diff(
10841229
{ref1, ref2} == {ref2, ref1},
1085-
"{-#{inspect_ref1}-, -#{inspect_ref2}-}",
1086-
"{+#{inspect_ref2}+, +#{inspect_ref1}+}"
1230+
"""
1231+
{
1232+
-#{inspect_ref1}-,
1233+
-#{inspect_ref2}-
1234+
}\
1235+
""",
1236+
"""
1237+
{
1238+
+#{inspect_ref2}+,
1239+
+#{inspect_ref1}+
1240+
}\
1241+
"""
10871242
)
10881243

10891244
refute_diff(
@@ -1100,7 +1255,16 @@ defmodule ExUnit.DiffTest do
11001255

11011256
refute_diff(ref1 == :a, "-#{inspect_ref1}-", "+:a+")
11021257
refute_diff({ref1, ref2} == :a, "-{#{inspect_ref1}, #{inspect_ref2}}", "+:a+")
1103-
refute_diff(%{ref1 => ref2} == :a, "-%{#{inspect_ref1} => #{inspect_ref2}}", "+:a+")
1258+
1259+
refute_diff(
1260+
%{ref1 => ref2} == :a,
1261+
"""
1262+
-%{
1263+
#{inspect_ref1} => #{inspect_ref2}
1264+
}\
1265+
""",
1266+
"+:a+"
1267+
)
11041268

11051269
refute_diff(
11061270
%Opaque{data: ref1} == :a,
@@ -1141,7 +1305,16 @@ defmodule ExUnit.DiffTest do
11411305
refute_diff(identity == :a, "-#{inspect}-", "+:a+")
11421306
refute_diff({identity, identity} == :a, "-{#{inspect}, #{inspect}}", "+:a+")
11431307
refute_diff({identity, :a} == {:a, identity}, "{-#{inspect}-, -:a-}", "{+:a+, +#{inspect}+}")
1144-
refute_diff(%{identity => identity} == :a, "-%{#{inspect} => #{inspect}}", "+:a+")
1308+
1309+
refute_diff(
1310+
%{identity => identity} == :a,
1311+
"""
1312+
-%{
1313+
#{inspect} => #{inspect}
1314+
}-\
1315+
""",
1316+
"+:a+"
1317+
)
11451318

11461319
refute_diff(
11471320
(&String.to_charlist/1) == (&String.unknown/1),

0 commit comments

Comments
 (0)