Skip to content

Commit 9443a16

Browse files
authored
Merge pull request #31 from invenia/cv/dates-stdlib
Support Dates as part of the stdlib
2 parents 0355940 + e9d04ea commit 9443a16

File tree

9 files changed

+33
-20
lines changed

9 files changed

+33
-20
lines changed

src/bindings.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ function ingest_default!(b::Bindings, expr::Expr)
106106
ingest_default!(b, arg)
107107
end
108108

109+
# Core.Int and Base.Random.rand
110+
elseif expr.head == :.
111+
reference = expr
112+
!(reference in b.internal) && push!(b.external, reference)
113+
109114
else
110115
error("expression is not valid as a parameter default: $expr")
111116
end

test/REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Compat 0.33
1+
Compat 0.37

test/bindings/ingest_assertion.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Mocking: Bindings, ingest_assertion!
2-
import Base.Dates: Hour
32

43
@testset "assertion" begin
54
b = Bindings([:T], [])

test/bindings/ingest_default.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ import Mocking: Bindings, ingest_default!
1515
ingest_default!(b, :(f(rand(Bool))))
1616
@test b.internal == Set()
1717
@test b.external == Set([:f, :rand, :Bool])
18+
19+
b = Bindings()
20+
ingest_default!(b, :(f(rand(Base.Bool))))
21+
@test b.internal == Set()
22+
@test b.external == Set([:f, :rand, :(Base.Bool)])
1823
end

test/expr.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import Base.Dates: Hour
1+
import Compat: Dates
2+
import Dates: Hour
23

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

57
@testset "joinbinding" begin
68
@test Mocking.joinbinding(:Foo) == :(Foo)
@@ -18,9 +20,8 @@ end
1820
@test Mocking.binding_expr(Int) == INT_EXPR # typealias. TODO: Change to Core.Int? Shouldn't actually matter
1921
@test Mocking.binding_expr(Int64) == :(Core.Int64) # concrete type
2022
@test Mocking.binding_expr(Integer) == :(Core.Integer) # abstract type
21-
@test Mocking.binding_expr(Hour) == :(Base.Dates.Hour) # unexported type
22-
@test Mocking.binding_expr(Dates.Hour) == :(Base.Dates.Hour) # submodule
23-
@test Mocking.binding_expr(Base.Dates.Hour) == :(Base.Dates.Hour) # full type binding
23+
@test Mocking.binding_expr(Hour) == HOUR_EXPR # unexported type
24+
@test Mocking.binding_expr(Dates.Hour) == HOUR_EXPR # submodule
2425
@test Mocking.binding_expr(rand) == :(Base.Random.rand) # function
2526
@test Mocking.binding_expr(AbstractArray{Int64}) == :(Core.AbstractArray) # Core.AbstractArray{Int64}?
2627
# @test Mocking.binding_expr(AbstractArray{T}) == :(Core.AbstractArray{T})

test/import.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import Compat: Dates
12
import Compat: read
23

34
# Patches should allow using imported bindings in the body of the patch
45
@testset "imported binding in body" begin
56
@test_throws UndefVarError Minute
6-
@test isdefined(Base.Dates, :Minute)
7-
import Base.Dates: Minute, Hour
7+
@test isdefined(Dates, :Minute)
8+
import Dates: Minute, Hour
89

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

test/optional.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import Base.Dates: Hour
1+
import Compat: Dates
2+
import Dates: Hour
23

34
# Creating a patch with an optional parameter
45
@testset "patch with optional parameter" begin
5-
hourvalue(h::Hour=Hour(0)) = Base.Dates.value(h)
6+
hourvalue(h::Hour=Hour(0)) = Dates.value(h)
67

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

1415
# Creating a patch with an keyword parameter
1516
@testset "patch with keyword parameter" begin
16-
hourvalue(; hour::Hour=Hour(0)) = Base.Dates.value(hour)
17+
hourvalue(; hour::Hour=Hour(0)) = Dates.value(hour)
1718

18-
patch = @patch hourvalue(; hour::Hour=Hour(21)) = 2 * Base.Dates.value(hour)
19+
patch = @patch hourvalue(; hour::Hour=Hour(21)) = 2 * Dates.value(hour)
1920
apply(patch) do
2021
@test (@mock hourvalue()) == 42
2122
# @test (@mock hourvalue(hour=Hour(4))) == 8 # TODO

test/patch.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Base: Dates
2-
import Base.Dates: Hour
1+
import Compat: Dates
2+
import Dates: Hour
33

44
@testset "patch" begin
55
@testset "basic" begin
@@ -30,13 +30,13 @@ import Base.Dates: Hour
3030

3131
@testset "assertion qualification" begin
3232
patches = [
33-
@patch f(h::Base.Dates.Hour=Base.Dates.Hour(rand())) = nothing
34-
@patch f(h::Dates.Hour=Dates.Hour(rand())) = nothing
35-
@patch f(h::Hour=Hour(rand())) = nothing
33+
@patch f(h::Base.Core.Int64=rand(Base.Core.Int64)) = nothing
34+
@patch f(h::Core.Int64=rand(Core.Int64)) = nothing
35+
@patch f(h::Int64=rand(Int64)) = nothing
3636
]
3737
for p in patches
38-
@test p.signature == :(f(h::Base.Dates.Hour=Base.Dates.Hour(Base.Random.rand())))
39-
@test p.modules == Set([:(Base.Dates), :(Base.Random)])
38+
@test p.signature == :(f(h::Core.Int64=Base.Random.rand(Core.Int64)))
39+
@test p.modules == Set([:Core, :(Base.Random)])
4040
end
4141
end
4242
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Mocking
22
Mocking.enable(force=true)
33

44
VERSION < v"0.7-" && import Compat: Test
5+
import Compat: Dates
56
using Test
67
import Mocking: apply
78

0 commit comments

Comments
 (0)