Skip to content

Commit ef7fd40

Browse files
committed
implement for structs
1 parent ae322db commit ef7fd40

File tree

2 files changed

+86
-19
lines changed

2 files changed

+86
-19
lines changed

lib/ex_unit/lib/ex_unit/diff.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,10 +1143,21 @@ defmodule ExUnit.Diff do
11431143
defp maybe_escape(other, _env), do: escape(other)
11441144

11451145
# We escape container types to make a distinction between AST and values that
1146-
# should be inspected. Maps should not be inspected, convert it to ast.
1146+
# should be inspected. Maps and structs without custom inspect implementation
1147+
# should not be inspected, convert it to ast.
11471148
# All other values have no special AST representation, so we can keep them as is.
1148-
defp escape(other) when is_struct(other), do: other
1149-
defp escape(other) when is_map(other), do: {:%{}, [], Map.to_list(other)}
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+
11501161
defp escape(other) when is_list(other) or is_tuple(other), do: {other}
11511162
defp escape(other), do: other
11521163

lib/ex_unit/test/ex_unit/diff_test.exs

Lines changed: 72 additions & 16 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
@@ -875,30 +879,30 @@ defmodule ExUnit.DiffTest do
875879

876880
test "maps in lists" do
877881
map = %{
878-
first_name: "John",
879-
last_name: "Doe",
880-
age: 30,
881-
notifications: true,
882-
language: "en-US",
883882
address: %{
884883
street: "123 Main St",
885884
city: "Springfield",
886885
state: "IL",
887886
zip: "62701"
888-
}
887+
},
888+
age: 30,
889+
first_name: "John",
890+
language: "en-US",
891+
last_name: "Doe",
892+
notifications: true
889893
}
890894

891895
refute_diff(
892896
[map] == [],
893897
"""
894898
[
895899
-%{
896-
address: %{state: \"IL\", zip: \"62701\", street: \"123 Main St\", city: \"Springfield\"},
900+
address: %{state: "IL", zip: "62701", street: "123 Main St", city: "Springfield"},
897901
age: 30,
898-
first_name: \"John\",
899-
last_name: \"Doe\",
900-
notifications: true,
901-
language: \"en-US\"
902+
first_name: "John",
903+
language: "en-US",
904+
last_name: "Doe",
905+
notifications: true
902906
}-
903907
]\
904908
""",
@@ -911,12 +915,12 @@ defmodule ExUnit.DiffTest do
911915
"""
912916
[
913917
+%{
914-
address: %{state: \"IL\", zip: \"62701\", street: \"123 Main St\", city: \"Springfield\"},
918+
address: %{state: "IL", zip: "62701", street: "123 Main St", city: "Springfield"},
915919
age: 30,
916-
first_name: \"John\",
917-
last_name: \"Doe\",
918-
notifications: true,
919-
language: \"en-US\"
920+
first_name: "John",
921+
language: "en-US",
922+
last_name: "Doe",
923+
notifications: true
920924
}+
921925
]\
922926
"""
@@ -925,6 +929,58 @@ defmodule ExUnit.DiffTest do
925929
assert_diff([map] == [map], [])
926930
end
927931

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+
928984
test "maps and structs with escaped values" do
929985
refute_diff(
930986
%User{age: {1, 2, 3}} = %User{age: {1, 2, 4}},

0 commit comments

Comments
 (0)