Skip to content

Commit

Permalink
Minor refactor to activate (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
omus authored Jul 12, 2024
1 parent 5562f2f commit f0adf5d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
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

0 comments on commit f0adf5d

Please sign in to comment.