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

Fix up the scoping for the @mock macro. #113

Merged
merged 1 commit into from
Apr 28, 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
17 changes: 7 additions & 10 deletions src/mock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ macro mock(expr)
expr.head == :call || error("expression is not a function call")

target = expr.args[1]
args = filter(!Mocking.iskwarg, expr.args[2:end])
args = filter(!iskwarg, expr.args[2:end])
kwargs = extract_kwargs(expr)

args_var = gensym("args")
alternate_var = gensym("alt")

# Due to how world-age works (see Julia issue #265 and PR #17057) when
# `Mocking.activated` is overwritten then all dependent functions will be recompiled.
# When `Mocking.activated() == false` then Julia will optimize the
# code below to have zero-overhead by only executing the original expression.
result = quote
if Mocking.activated()
local $args_var = tuple($(args...))
local $alternate_var = Mocking.get_alternate($target, $args_var...)
if $alternate_var !== nothing
Base.invokelatest($alternate_var, $args_var...; $(kwargs...))
if $activated()
args_var = tuple($(args...))
alternate_var = $get_alternate($target, args_var...)
if alternate_var !== nothing
Base.invokelatest(alternate_var, args_var...; $(kwargs...))
else
$target($args_var...; $(kwargs...))
$target(args_var...; $(kwargs...))
end
else
$target($(args...); $(kwargs...))
Expand Down
2 changes: 1 addition & 1 deletion src/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro patch(expr::Expr)

# We need to evaluate the alternate function in the context of the `@patch` macro in
# order to support closures.
return esc(:(Mocking.Patch($target, $alternate)))
return esc(:($Patch($target, $alternate)))
end

struct PatchEnv
Expand Down
Loading