Add __decorated_functions__ function to modules using the decorator#54
Open
leggebroten wants to merge 4 commits intoarjan:masterfrom
Open
Add __decorated_functions__ function to modules using the decorator#54leggebroten wants to merge 4 commits intoarjan:masterfrom
leggebroten wants to merge 4 commits intoarjan:masterfrom
Conversation
…t of all decorated functions
### Simple reflection for all decorated functions
A "hidden" function `__decorated_functions__/0` is added to any module that decorates functions and returns a list of
all decorated functions within that module.
This can be useful for testing purposes since you don't have to assert the decorated behaviors of the decorated
functions. So long as your decorator function is well tested, you can just assert the expected decorators are present
for the given function.
The function returns a map with the function name and arguments as the key and a list of tuples with the decorator.
This permits multiple decorators to be asserted with a single function call so that adding new decorators will cause
the assertion to fail.
For example, given the following module:
```elixir
defmodule NewModule do
use Decorator.Define, [new_decorator: 1, another_decorator: 2]
@decorate new_decorator("one")
def func(%StructOne{} = msg) do
msg
end
@decorate new_decorator("two")
@decorate another_decorator("a", "b")
@decorate new_decorator("b")
def func(%StructTwo{c: 2} = _msg) do
:ok
end
end
```
You can assert the decorated functions like so:
```elixir
test "Module with decorated functions are returned by `__decorated_functions__()" do
assert %{
{:func, ["%StructOne{} = msg"]} => [
{DecoratorTest.Fixture.NewDecorator, :new_decorator, ["one"]}
],
{:func, ["%StructTwo{c: 2} = _msg"]} => [
{DecoratorTest.Fixture.NewDecorator, :new_decorator, ["two"]},
{DecoratorTest.Fixture.AnotherDecorator, :another_decorator, ["a", "b"]},
{DecoratorTest.Fixture.NewDecorator, :new_decorator, ["b"]}
]
} == NewModule.__decorated_functions__()
end
```
Obviously, any changes to the parameters of the decorated function will cause the simple assertion to fail, but that's
intentional, as the decorator may be dependent on the parameters of the decorated function.
Added a "reflection" function `__decorated_functions__` to return lis…
Added __decorated_functions__ function to the created macro so that unit tests could assert the proper decoration had been done and not have to assert the decorated functionality
florius0
added a commit
to florius0/decorator
that referenced
this pull request
Sep 6, 2024
PR arjan#54 from head repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds "hidden"
__decorated_functions__function to modules that use the@decoratefunction.The new function allows unit tests to assert the decoration was properly called, thereby avoiding the need to assert that the behavior invoked by the
@decoratecall was done, and not have to assert the decorated functionality