Skip to content

Commit

Permalink
Merge branch 'fix/allow-arguments-with-binary-pattern-matching'
Browse files Browse the repository at this point in the history
  • Loading branch information
amalbuquerque committed Sep 22, 2019
2 parents a623adf + 516c384 commit c4ea8ba
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* intercepted functions without arguments that don't have `()` isn't working (e.g. `def foo, do: 123`)

# Changelog for v0.5.0
# Changelog for v0.5.1

## Changes

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies in `mix.exs`:
```elixir
def deps do
[
{:interceptor, "~> 0.5.0"}
{:interceptor, "~> 0.5.1"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion lib/function_arguments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ defmodule Interceptor.FunctionArguments do
# in this case, `arg_ast` doesn't contain an assignment, so we are "manually"
# placing it inside an assignment statement
defp get_arg_name_and_its_ast({arg_name, _metadata, _context} = arg_ast)
when arg_name in [:<<>>, :{}, :%{}, :%] do
when arg_name in [:<<>>, :{}, :%{}, :%, :<>] do
random_name = Utils.random_atom()

# arg variables always have their context as nil
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Interceptor.MixProject do
app: :interceptor,
package: package(),
source_url: "https://github.com/amalbuquerque/interceptor",
version: "0.5.0",
version: "0.5.1",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
16 changes: 16 additions & 0 deletions test/annotated/annotated_interceptor_edge_cases_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ defmodule AnnotatedInterceptorEdgeCasesTest do

@process_name :annotated_edge_cases_test_process

describe "functions with argument using `<>`" do
test "it passes the argument with the prefix before `<>`" do
{:ok, _pid} = spawn_agent()

result = AnnotatedInterceptedEdgeCases1.intercept_with_prefix("some_prefix:blabla")

callback_calls = get_agent_messages()

[{_started_at, _ended_at, callback_result, intercepted_mfa}] = callback_calls

assert length(callback_calls) == 1
assert result == callback_result
assert intercepted_mfa == {AnnotatedInterceptedEdgeCases1, :intercept_with_prefix, ["some_prefix:blabla"]}
end
end

describe "module with functions with ignored arguments" do
test "it passes the ignored arguments as `:arg_cant_be_intercepted`" do
{:ok, _pid} = spawn_agent()
Expand Down
16 changes: 16 additions & 0 deletions test/do_block/interceptor_edge_cases_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ defmodule InterceptorEdgeCasesTest do

@process_name :edge_cases_test_process

describe "functions with argument using `<>`" do
test "it passes the argument with the prefix before `<>`" do
{:ok, _pid} = spawn_agent()

result = InterceptedEdgeCases1.intercept_with_prefix("some_prefix:blabla")

callback_calls = get_agent_messages()

[{_started_at, _ended_at, callback_result, intercepted_mfa}] = callback_calls

assert length(callback_calls) == 1
assert result == callback_result
assert intercepted_mfa == {InterceptedEdgeCases1, :intercept_with_prefix, ["some_prefix:blabla"]}
end
end

describe "module with functions with ignored arguments" do
test "it passes the ignored arguments as `:arg_cant_be_intercepted`" do
{:ok, _pid} = spawn_agent()
Expand Down
18 changes: 18 additions & 0 deletions test/function_arguments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ defmodule FunctionArgumentsTest do
assert_ast_match(random_var_ast, expected_random_var_ast)
end

test "it handles binary destructure with `<>`" do
function_header = get_function_header(~s/def abc("a prefix" <> hallo), do: hallo/)
result = FunctionArguments.get_args_names_and_new_args_list(function_header)

{args_names, args_ast} = result

assert length(args_names) == length(args_ast)
assert length(args_ast) == 1

[{:=, _metadata, [arg_ast, random_var_ast]}] = args_ast

expected_arg_ast = quote do: "a prefix" <> hallo
expected_random_var_ast = Macro.var(hd(args_names), nil)

assert_ast_match(arg_ast, expected_arg_ast)
assert_ast_match(random_var_ast, expected_random_var_ast)
end

test "it handles map destructure" do
function_header = get_function_header("def abc(%{a: x, b: y, c: z}), do: [x,y,z]")
result = FunctionArguments.get_args_names_and_new_args_list(function_header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ defmodule AnnotatedInterceptedEdgeCases1 do

@intercept true
def to_intercept(a, b, _to_ignore), do: "#{a} #{b}"

@intercept true
def intercept_with_prefix("some_prefix:" <> abc), do: abc
end
2 changes: 2 additions & 0 deletions test/intercepted_modules/do_block/intercepted_edge_cases.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ defmodule InterceptedEdgeCases1 do

I.intercept do
def to_intercept(a, b, _to_ignore), do: "#{a} #{b}"

def intercept_with_prefix("some_prefix:" <> suffix), do: suffix
end
end
2 changes: 2 additions & 0 deletions test/intercepted_modules/intercept_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule InterceptConfig do

# edge cases
{InterceptedEdgeCases1, :to_intercept, 3} => [on_success: {EdgeCases.Callbacks, :success_cb, 3}, on_error: {EdgeCases.Callbacks, :error_cb, 3}],
{InterceptedEdgeCases1, :intercept_with_prefix, 1} => [on_success: {EdgeCases.Callbacks, :success_cb, 3}, on_error: {EdgeCases.Callbacks, :error_cb, 3}],

# these configs will be overridden by the module own configuration
{InterceptedOnAfterOwnConfiguration1, :to_intercept, 0} => [after: {After.Callback, :right_after, 2}],
Expand Down Expand Up @@ -90,6 +91,7 @@ defmodule InterceptConfig do

# edge cases
{AnnotatedInterceptedEdgeCases1, :to_intercept, 3} => [on_success: {AnnotatedEdgeCases.Callbacks, :success_cb, 3}, on_error: {AnnotatedEdgeCases.Callbacks, :error_cb, 3}],
{AnnotatedInterceptedEdgeCases1, :intercept_with_prefix, 1} => [on_success: {AnnotatedEdgeCases.Callbacks, :success_cb, 3}, on_error: {AnnotatedEdgeCases.Callbacks, :error_cb, 3}],

# these configs will be overridden by the module own configuration
{AnnotatedInterceptedOnAfterOwnConfiguration1, :to_intercept, 0} => [after: {After.Callback, :right_after, 2}],
Expand Down

0 comments on commit c4ea8ba

Please sign in to comment.