Skip to content
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
5 changes: 5 additions & 0 deletions src/bindings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ function ingest_default!(b::Bindings, expr::Expr)
ingest_default!(b, arg)
end

# Core.Int and Base.Random.rand
elseif expr.head == :.
reference = expr
!(reference in b.internal) && push!(b.external, reference)

else
error("expression is not valid as a parameter default: $expr")
end
Expand Down
2 changes: 1 addition & 1 deletion test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Compat 0.33
Compat 0.37
1 change: 0 additions & 1 deletion test/bindings/ingest_assertion.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Mocking: Bindings, ingest_assertion!
import Base.Dates: Hour

@testset "assertion" begin
b = Bindings([:T], [])
Expand Down
5 changes: 5 additions & 0 deletions test/bindings/ingest_default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ import Mocking: Bindings, ingest_default!
ingest_default!(b, :(f(rand(Bool))))
@test b.internal == Set()
@test b.external == Set([:f, :rand, :Bool])

b = Bindings()
ingest_default!(b, :(f(rand(Base.Bool))))
@test b.internal == Set()
@test b.external == Set([:f, :rand, :(Base.Bool)])
end
9 changes: 5 additions & 4 deletions test/expr.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Base.Dates: Hour
import Compat: Dates
import Dates: Hour

const INT_EXPR = Int === Int32 ? :(Core.Int32) : :(Core.Int64)
const HOUR_EXPR = VERSION < v"0.7.0-DEV.2575" ? :(Base.Dates.Hour) : :(Dates.Hour)

@testset "joinbinding" begin
@test Mocking.joinbinding(:Foo) == :(Foo)
Expand All @@ -18,9 +20,8 @@ end
@test Mocking.binding_expr(Int) == INT_EXPR # typealias. TODO: Change to Core.Int? Shouldn't actually matter
@test Mocking.binding_expr(Int64) == :(Core.Int64) # concrete type
@test Mocking.binding_expr(Integer) == :(Core.Integer) # abstract type
@test Mocking.binding_expr(Hour) == :(Base.Dates.Hour) # unexported type
@test Mocking.binding_expr(Dates.Hour) == :(Base.Dates.Hour) # submodule
@test Mocking.binding_expr(Base.Dates.Hour) == :(Base.Dates.Hour) # full type binding
@test Mocking.binding_expr(Hour) == HOUR_EXPR # unexported type
@test Mocking.binding_expr(Dates.Hour) == HOUR_EXPR # submodule
@test Mocking.binding_expr(rand) == :(Base.Random.rand) # function
@test Mocking.binding_expr(AbstractArray{Int64}) == :(Core.AbstractArray) # Core.AbstractArray{Int64}?
# @test Mocking.binding_expr(AbstractArray{T}) == :(Core.AbstractArray{T})
Expand Down
5 changes: 3 additions & 2 deletions test/import.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Compat: Dates
import Compat: read

# Patches should allow using imported bindings in the body of the patch
@testset "imported binding in body" begin
@test_throws UndefVarError Minute
@test isdefined(Base.Dates, :Minute)
import Base.Dates: Minute, Hour
@test isdefined(Dates, :Minute)
import Dates: Minute, Hour

myminute(x::Integer) = Minute(x)

Expand Down
11 changes: 6 additions & 5 deletions test/optional.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Base.Dates: Hour
import Compat: Dates
import Dates: Hour

# Creating a patch with an optional parameter
@testset "patch with optional parameter" begin
hourvalue(h::Hour=Hour(0)) = Base.Dates.value(h)
hourvalue(h::Hour=Hour(0)) = Dates.value(h)

patch = @patch hourvalue(h::Hour=Hour(21)) = 2 * Base.Dates.value(h)
patch = @patch hourvalue(h::Hour=Hour(21)) = 2 * Dates.value(h)
apply(patch) do
@test (@mock hourvalue()) == 42
@test (@mock hourvalue(Hour(4))) == 8
Expand All @@ -13,9 +14,9 @@ end

# Creating a patch with an keyword parameter
@testset "patch with keyword parameter" begin
hourvalue(; hour::Hour=Hour(0)) = Base.Dates.value(hour)
hourvalue(; hour::Hour=Hour(0)) = Dates.value(hour)

patch = @patch hourvalue(; hour::Hour=Hour(21)) = 2 * Base.Dates.value(hour)
patch = @patch hourvalue(; hour::Hour=Hour(21)) = 2 * Dates.value(hour)
apply(patch) do
@test (@mock hourvalue()) == 42
# @test (@mock hourvalue(hour=Hour(4))) == 8 # TODO
Expand Down
14 changes: 7 additions & 7 deletions test/patch.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Base: Dates
import Base.Dates: Hour
import Compat: Dates
import Dates: Hour

@testset "patch" begin
@testset "basic" begin
Expand Down Expand Up @@ -30,13 +30,13 @@ import Base.Dates: Hour

@testset "assertion qualification" begin
patches = [
@patch f(h::Base.Dates.Hour=Base.Dates.Hour(rand())) = nothing
@patch f(h::Dates.Hour=Dates.Hour(rand())) = nothing
@patch f(h::Hour=Hour(rand())) = nothing
@patch f(h::Base.Core.Int64=rand(Base.Core.Int64)) = nothing
@patch f(h::Core.Int64=rand(Core.Int64)) = nothing
@patch f(h::Int64=rand(Int64)) = nothing
]
for p in patches
@test p.signature == :(f(h::Base.Dates.Hour=Base.Dates.Hour(Base.Random.rand())))
@test p.modules == Set([:(Base.Dates), :(Base.Random)])
@test p.signature == :(f(h::Core.Int64=Base.Random.rand(Core.Int64)))
@test p.modules == Set([:Core, :(Base.Random)])
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Mocking
Mocking.enable(force=true)

VERSION < v"0.7-" && import Compat: Test
import Compat: Dates
using Test
import Mocking: apply

Expand Down