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

Minor refactor to activate #118

Merged
merged 1 commit into from
Jul 12, 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
10 changes: 4 additions & 6 deletions src/Mocking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ Enable `@mock` call sites to allow for calling patches instead of the original f
"""
function activate()
# Avoid redefining `activated` when it's already set appropriately
!activated() && @eval activated() = true
!Base.invokelatest(activated) && @eval activated() = true
return nothing
end

function activate(f)
old = activated()
started_deactivated = !activated()
try
activate()
Base.invokelatest(f)
finally
if (Base.invokelatest(activated) != old)
@eval activated() = $old
end
started_deactivated && deactivate()
end
end

Expand All @@ -44,7 +42,7 @@ Disable `@mock` call sites to only call the original function.
"""
function deactivate()
# Avoid redefining `activated` when it's already set appropriately
activated() && @eval activated() = false
Base.invokelatest(activated) && @eval activated() = false
return nothing
end

Expand Down
4 changes: 2 additions & 2 deletions test/activate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Starting with Mocking enabled.
Mocking.activate()
@assert Mocking.activated()
@test Mocking.activated()
Mocking.activate() do
apply(patch) do
@test (@mock add1(2)) == 44
Expand All @@ -16,7 +16,7 @@
# Make sure to leave it enabled for the rest of the tests.
try
Mocking.deactivate()
@assert !Mocking.activated()
@test !Mocking.activated()
Mocking.activate() do
apply(patch) do
@test (@mock add1(2)) == 44
Expand Down
Loading