Skip to content

Commit 9911266

Browse files
authored
Add elixir 1.19.x compatibility (#107)
* Add elixir 1.19 to CI * Fix type violation warning
1 parent b637737 commit 9911266

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141
elixir: 1.17.x
4242
- otp: 27.x
4343
elixir: 1.18.x
44+
- otp: 27.x
45+
elixir: 1.19.x
4446
env:
4547
MIX_ENV: test
4648
steps:

lib/mimic/module.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,12 @@ defmodule Mimic.Module do
129129
if @elixir_version >= 1.18 do
130130
defp generate_mimic_struct(module) do
131131
if function_exported?(module, :__info__, 1) && module.__info__(:struct) != nil do
132-
struct_info = module.__info__(:struct)
133-
134132
struct_template = Map.from_struct(module.__struct__())
135133

136134
struct_params =
137-
for %{field: field} <- struct_info,
138-
do: {field, Macro.escape(struct_template[field])}
135+
Enum.map(module.__info__(:struct), fn %{field: field} ->
136+
{field, Macro.escape(struct_template[field])}
137+
end)
139138

140139
quote do
141140
defstruct unquote(struct_params)

test/mimic_test.exs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,26 +1103,42 @@ defmodule Mimic.Test do
11031103
}
11041104
end
11051105

1106-
if @elixir_version >= 1.18 do
1107-
test "copies struct fields" do
1108-
StructNoEnforceKeys
1109-
|> stub(:bar, fn -> @stubbed end)
1110-
1111-
assert StructNoEnforceKeys.__info__(:struct) == [
1112-
%{field: :foo, default: nil},
1113-
%{field: :bar, default: nil}
1114-
]
1115-
end
1116-
else
1117-
test "copies struct fields" do
1118-
StructNoEnforceKeys
1119-
|> stub(:bar, fn -> @stubbed end)
1120-
1121-
assert StructNoEnforceKeys.__info__(:struct) == [
1122-
%{field: :foo, required: false},
1123-
%{field: :bar, required: false}
1124-
]
1125-
end
1106+
cond do
1107+
@elixir_version >= 1.19 ->
1108+
test "copies struct fields" do
1109+
StructNoEnforceKeys
1110+
|> stub(:bar, fn -> @stubbed end)
1111+
1112+
# Why pattern matching:
1113+
# Direct comparison causes the compiler to emit a warning.
1114+
# The `default` key may or may not exist in the struct info.
1115+
assert [
1116+
%{field: :foo, default: nil, required: false},
1117+
%{field: :bar, default: nil, required: false}
1118+
] = StructNoEnforceKeys.__info__(:struct)
1119+
end
1120+
1121+
@elixir_version >= 1.18 ->
1122+
test "copies struct fields" do
1123+
StructNoEnforceKeys
1124+
|> stub(:bar, fn -> @stubbed end)
1125+
1126+
assert StructNoEnforceKeys.__info__(:struct) == [
1127+
%{field: :foo, default: nil},
1128+
%{field: :bar, default: nil}
1129+
]
1130+
end
1131+
1132+
true ->
1133+
test "copies struct fields" do
1134+
StructNoEnforceKeys
1135+
|> stub(:bar, fn -> @stubbed end)
1136+
1137+
assert StructNoEnforceKeys.__info__(:struct) == [
1138+
%{field: :foo, required: false},
1139+
%{field: :bar, required: false}
1140+
]
1141+
end
11261142
end
11271143

11281144
test "protocol still works" do

0 commit comments

Comments
 (0)