Skip to content

Commit

Permalink
Include enforce_keys on struct
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Jun 22, 2024
1 parent 5b83c3e commit 816fd77
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/mimic/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ defmodule Mimic.Module do

defp generate_mimic_struct(module) do
if module.__info__(:struct) != nil do
required_fields = for %{field: field, required: true} <- module.__info__(:struct), do: field

quote do
@enforce_keys unquote(required_fields)
defstruct unquote(for %{field: field} <- module.__info__(:struct), do: field)
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/mimic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -950,14 +950,14 @@ defmodule Mimic.Test do
setup :set_mimic_private

test "copies struct fields" do
struct_fields =
Structs.__info__(:struct)
|> Enum.map(&Map.get(&1, :field))

Structs
|> stub(:foo, fn -> :stubbed end)

assert Structs.__info__(:struct) |> Enum.map(&Map.get(&1, :field)) == struct_fields
assert Structs.__info__(:struct) == [
%{field: :foo, required: true},
%{field: :bar, required: true},
%{field: :default, required: false}
]
end

test "protocol still works" do
Expand Down

0 comments on commit 816fd77

Please sign in to comment.