Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add elixir 1.18.0 compatibility #88

Merged
merged 6 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
coverage: true
- otp: 27.x
elixir: 1.17.x
- otp: 27.x
elixir: 1.18.x
env:
MIX_ENV: test
steps:
Expand Down
62 changes: 41 additions & 21 deletions lib/mimic/module.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Mimic.Module do
alias Mimic.{Cover, Server}

@elixir_version System.version() |> Float.parse() |> elem(0)
@moduledoc false

@spec original(module) :: module
Expand Down Expand Up @@ -121,29 +123,47 @@ defmodule Mimic.Module do
module
end

defp generate_mimic_struct(module) do
if function_exported?(module, :__info__, 1) && module.__info__(:struct) != nil do
struct_info =
module.__info__(:struct)
|> Enum.split_with(& &1.required)
|> Tuple.to_list()
|> List.flatten()

required_fields = for %{field: field, required: true} <- struct_info, do: field
struct_template = Map.from_struct(module.__struct__())

struct_params =
for %{field: field, required: required} <- struct_info do
if required do
field
else
{field, Macro.escape(struct_template[field])}
end
if @elixir_version >= 1.18 do
defp generate_mimic_struct(module) do
if function_exported?(module, :__info__, 1) && module.__info__(:struct) != nil do
struct_info = module.__info__(:struct)

struct_template = Map.from_struct(module.__struct__())

struct_params =
for %{field: field} <- struct_info,
do: {field, Macro.escape(struct_template[field])}

quote do
defstruct unquote(struct_params)
end
end
end
else
defp generate_mimic_struct(module) do
if function_exported?(module, :__info__, 1) && module.__info__(:struct) != nil do
struct_info =
module.__info__(:struct)
|> Enum.split_with(& &1.required)
|> Tuple.to_list()
|> List.flatten()

required_fields = for %{field: field, required: true} <- struct_info, do: field
struct_template = Map.from_struct(module.__struct__())

struct_params =
for %{field: field, required: required} <- struct_info do
if required do
field
else
{field, Macro.escape(struct_template[field])}
end
end

quote do
@enforce_keys unquote(required_fields)
defstruct unquote(struct_params)
quote do
@enforce_keys unquote(required_fields)
defstruct unquote(struct_params)
end
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions test/dsl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ defmodule Mimic.DSLTest do
use ExUnit.Case, async: true
use Mimic.DSL

@stub 10

test "basic example" do
stub(Calculator.add(_x, _y), do: :stub)
stub(Calculator.add(_x, _y), do: @stub)
expect Calculator.add(x, y), do: x + y
expect Calculator.mult(x, y), do: x * y

assert Calculator.add(2, 3) == 5
assert Calculator.mult(2, 3) == 6

assert Calculator.add(2, 3) == :stub
assert Calculator.add(2, 3) == @stub
end

test "guards on stub" do
Expand Down
Loading
Loading