-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from invenia/tg/nested_apply
Add support for nested calls to apply
- Loading branch information
Showing
6 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module Mocking | ||
|
||
using Compat: mergewith | ||
using ExprTools: splitdef, combinedef | ||
|
||
export @patch, @mock, Patch, apply | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@testset "merge PatchEnv instances" begin | ||
multiply(x::Number) = 2x | ||
multiply(x::Int) = 2x - 1 | ||
add(x::Number) = x + 2 | ||
add(x::Int) = x + 1 | ||
|
||
patches = Patch[ | ||
@patch multiply(x::Integer) = 3x | ||
@patch multiply(x::Int) = 4x | ||
@patch add(x::Int) = x + 4 | ||
] | ||
|
||
@testset "simple" begin | ||
pe1 = Mocking.PatchEnv(patches[1]) | ||
pe2 = Mocking.PatchEnv(patches[2:3]) | ||
pe = Mocking.PatchEnv(patches) | ||
|
||
@test pe == merge(pe1, pe2) | ||
end | ||
|
||
@testset "debug flag" begin | ||
pe1 = Mocking.PatchEnv(patches[1], true) | ||
pe2 = Mocking.PatchEnv(patches[2:3]) | ||
pe = Mocking.PatchEnv(patches, true) | ||
|
||
@test pe == merge(pe1, pe2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Nesting calls to apply should take the appropriate union of patches. | ||
@testset "nested apply calls" begin | ||
multiply(x::Number) = 2x | ||
multiply(x::Int) = 2x - 1 | ||
add(x::Number) = x + 2 | ||
add(x::Int) = x + 1 | ||
|
||
@testset "simple" begin | ||
patches = Patch[ | ||
@patch multiply(x::Integer) = 3x | ||
@patch multiply(x::Int) = 4x | ||
@patch add(x::Int) = x + 4 | ||
] | ||
|
||
apply(patches) do | ||
@test (@mock multiply(2)) == 8 | ||
@test (@mock multiply(0x2)) == 0x6 | ||
@test (@mock multiply(2//1)) == 4//1 | ||
@test (@mock add(2//1)) == 4//1 | ||
@test (@mock add(2)) == 6 | ||
end | ||
|
||
apply(patches[1]) do | ||
@test (@mock multiply(2)) == 6 | ||
@test (@mock multiply(0x2)) == 0x6 | ||
@test (@mock multiply(2//1)) == 4//1 | ||
@test (@mock add(2//1)) == 4//1 | ||
@test (@mock add(2)) == 3 | ||
|
||
apply(patches[2]) do | ||
@test (@mock multiply(2)) == 8 | ||
@test (@mock multiply(0x2)) == 0x6 | ||
@test (@mock multiply(2//1)) == 4//1 | ||
@test (@mock add(2//1)) == 4//1 | ||
@test (@mock add(2)) == 3 | ||
|
||
apply(patches[3]) do | ||
@test (@mock multiply(2)) == 8 | ||
@test (@mock multiply(0x2)) == 0x6 | ||
@test (@mock multiply(2//1)) == 4//1 | ||
@test (@mock add(2//1)) == 4//1 | ||
@test (@mock add(2)) == 6 | ||
end | ||
|
||
@test (@mock multiply(2)) == 8 | ||
@test (@mock multiply(0x2)) == 0x6 | ||
@test (@mock multiply(2//1)) == 4//1 | ||
@test (@mock add(2//1)) == 4//1 | ||
@test (@mock add(2)) == 3 | ||
end | ||
|
||
@test (@mock multiply(2)) == 6 | ||
@test (@mock multiply(0x2)) == 0x6 | ||
@test (@mock multiply(2//1)) == 4//1 | ||
@test (@mock add(2//1)) == 4//1 | ||
@test (@mock add(2)) == 3 | ||
end | ||
end | ||
|
||
@testset "repeated patch" begin | ||
patches = Patch[ | ||
@patch multiply(x::Integer) = 3x | ||
@patch multiply(x::Integer) = 4x | ||
] | ||
|
||
apply(patches) do | ||
@test (@mock multiply(2)) == 8 | ||
end | ||
|
||
apply(patches[1]) do | ||
@test (@mock multiply(2)) == 6 | ||
apply(patches[2]) do | ||
@test (@mock multiply(2)) == 8 | ||
end | ||
@test (@mock multiply(2)) == 6 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9fd8321
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
9fd8321
There was a problem hiding this comment.
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/46037
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: