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

Reintroduce activate(f) and deactivate() #135

Closed
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
41 changes: 41 additions & 0 deletions src/Mocking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,47 @@ function activate()
return nothing
end

"""
Mocking.activate(f)


Activate `@mock` call sites for the duration of the function `f`.

!!! warning
This function redefines Mocking.jl internals and so can produce warnings about
method redefinitions. You may see the warning:
`WARNING: Method definition _activated($Int) at ... overwritten at ...`
This is expected behavior and can be safely ignored.
To avoid these warnings, instead use `Mocking.activate()` to activate `@mock` call
sites for the duration of the test suite. Alternatively, start Julia with
`--warn-overwrite=no` to suppress these warnings.
"""
function activate(f)
started_deactivated = !activated()
try
activate()
Base.invokelatest(f)
finally
started_deactivated && deactivate()
end
end

"""
Mocking.deactivate() -> Nothing

Disable `@mock` call sites to only call the original function.

!!! note
It is not usually necessary to call this function directly.
Instead it is recommended to simply call `Mocking.activate()` in `test/runtests.jl` to
activate `@mock` call sites for the duration of the test suite.
"""
function deactivate()
# Avoid redefining `_activated` when it's already set appropriately
Base.invokelatest(activated) && @eval _activated(::Int) = false
return nothing
end
Comment on lines +78 to +92
Copy link
Member

Choose a reason for hiding this comment

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

Do you need deactivate? Unless you also require this for your code base it's probably best to avoid re-introducing this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, just for implementing (and testing) activate(f), so perhaps we should delete the doc-string and name this _deactivate()?

Copy link
Member

Choose a reason for hiding this comment

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

That would be good with me.


const NULLIFIED = Ref{Bool}(false)

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

# Starting with Mocking enabled.
@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()
@test !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 @@ -39,4 +39,5 @@ Mocking.activate()
include("async-scope.jl")
include("issues.jl")
include("async-world-ages.jl")
include("activate.jl")
end
Loading