Skip to content

Commit

Permalink
add guarded function support, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
smoes committed Jul 30, 2024
1 parent 59509b7 commit b846450
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Efx follows the following principles:
To use `Efx` in your project, add this to your dependencies in `mix.ex`:

```elixir
{:efx, "~> 0.2.1"}
{:efx, "~> 0.2.2"}
```

If you want to have proper formatting of the `Efx.defeffect/2` macro, you can add
Expand Down
10 changes: 9 additions & 1 deletion lib/efx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ defmodule Efx do
end

defmacro defeffect(fun, do_block) do
{name, ctx, args} = fun
{name, ctx, args} = extract_fun(fun)
args = ensure_list(args)
module = __CALLER__.module

Expand Down Expand Up @@ -239,6 +239,14 @@ defmodule Efx do
end
end

defp extract_fun({:when, _ctx, [fun, _when_condition]}) do
fun
end

defp extract_fun(fun) do
fun
end

@spec spec_name({any(), any(), list()}) :: name :: atom()
defp spec_name({_, _, a}) do
a = ensure_list(a)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Efx.MixProject do
use Mix.Project

@version "0.2.1"
@version "0.2.2"
@github_page "https://github.com/bravobike/efx"

def project do
Expand Down
5 changes: 5 additions & 0 deletions test/efx_case_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ defmodule EfxCaseTest do
assert EfxExample.multi_fun(:a) == :a
end

test "on guarded funs" do
bind(EfxExample, :guarded_fun, fn arg -> arg end)
assert EfxExample.guarded_fun(:a) == :a
end

test "works with expected number of calls" do
bind(EfxExample, :get, fn -> [] end, calls: 1)
assert EfxExample.get() == []
Expand Down
18 changes: 18 additions & 0 deletions test/support/efx_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ defmodule EfxCase.EfxExample do
string
end

# we use this to see if functions with guards
# work properly at compile time
@spec guarded_fun(any()) :: any()
defeffect guarded_fun(a) when a in [:a, :b] do

Check warning on line 35 in test/support/efx_example.ex

View workflow job for this annotation

GitHub Actions / Test on OTP 25.0.4 / Elixir 1.14.0

got "@impl EfxCase.EfxExample" for function guarded_fun/1 but this behaviour does not specify such callback. There are no known callbacks, please specify the proper @behaviour and make sure it defines callbacks
{a, :a_or_b}
end

@spec guarded_fun(any()) :: any()
defeffect guarded_fun(a) do
a
end

# we use this to see if one liners work without getting
# deformed by formatter

@spec one_liner(any()) :: any()
defeffect one_liner(a), do: a

Check warning on line 48 in test/support/efx_example.ex

View workflow job for this annotation

GitHub Actions / Test on OTP 25.0.4 / Elixir 1.14.0

got "@impl EfxCase.EfxExample" for function one_liner/1 but this behaviour does not specify such callback. There are no known callbacks, please specify the proper @behaviour and make sure it defines callbacks

@spec without_parens :: atom
defeffect without_parens do
:ok
Expand Down

0 comments on commit b846450

Please sign in to comment.