Skip to content

Commit

Permalink
Add activate method taking function (#102)
Browse files Browse the repository at this point in the history
* Add `activate` method taking function

* Bump version

* Combine docstrings per code review
  • Loading branch information
nickrobinson251 authored Mar 3, 2023
1 parent f07545f commit 47abfea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ keywords = ["testing", "mocking"]
license = "MIT"
desc = "Allows Julia function calls to be temporarily overloaded for purpose of testing"
author = ["Curtis Vogt"]
version = "0.7.5"
version = "0.7.6"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
14 changes: 13 additions & 1 deletion src/Mocking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include("deprecated.jl")
activated() = false

"""
Mocking.activate()
Mocking.activate([func])
Enable `@mock` call sites to allow for calling patches instead of the original function.
"""
Expand All @@ -25,6 +25,18 @@ function activate()
return nothing
end

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

"""
Mocking.deactivate()
Expand Down
29 changes: 29 additions & 0 deletions test/activate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@testset "activate(func)" begin
add1(x) = x + 1
patch = @patch add1(x) = x + 42

# Starting with Mocking enabled.
Mocking.activate()
@assert Mocking.activated()
Mocking.activate() do
apply(patch) do
@test (@mock add1(2)) == 44
end
end
@test Mocking.activated()

# Starting with Mocking disabled.
# Make sure to leave it enabled for the rest of the tests.
try
Mocking.deactivate()
@assert !Mocking.activated()
Mocking.activate() do
apply(patch) do
@test (@mock add1(2)) == 44
end
end
@test !Mocking.activated()
finally
Mocking.activate()
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ using Mocking: anon_morespecific, anonymous_signature, dispatch, type_morespecif
include("nested_apply.jl")
include("async.jl")
include("issues.jl")
include("activate.jl")
end

2 comments on commit 47abfea

@rofinn
Copy link
Collaborator

@rofinn rofinn commented on 47abfea Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/78894

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.6 -m "<description of version>" 47abfea8e482dd6ea9b2ab8ae7250bc43a7a58b3
git push origin v0.7.6

Please sign in to comment.