Skip to content

Commit

Permalink
Update readstring(x) to read(x, String)
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Oct 31, 2017
1 parent 8d4c683 commit 472f091
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions test/import.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Compat: readstring
import Compat: read

# Patches should allow using imported bindings in the body of the patch
@test_throws UndefVarError Minute
Expand All @@ -20,15 +20,15 @@ end
@test_throws UndefVarError AbstractCmd
@test isdefined(Base, :AbstractCmd)

patch = @patch readstring(cmd::Base.AbstractCmd) = "bar"
patch = @patch read(cmd::Base.AbstractCmd, ::Type{String}) = "bar"
apply(patch) do
@test (@mock readstring(`foo`)) == "bar"
@test (@mock read(`foo`, String)) == "bar"
end

# Patches should allow using imported bindings syntax in the signature
import Base: AbstractCmd

patch = @patch readstring(cmd::AbstractCmd) = "bar"
patch = @patch read(cmd::AbstractCmd, ::Type{String}) = "bar"
apply(patch) do
@test (@mock readstring(`foo`)) == "bar"
@test (@mock read(`foo`, String)) == "bar"
end
4 changes: 2 additions & 2 deletions test/real-nested.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Compat: readstring
import Compat: read

let
readfile(filename) = (@mock isfile(filename)) ? readstring(@mock open(filename)) : ""
readfile(filename) = (@mock isfile(filename)) ? read((@mock open(filename)), String) : ""

# Testing with both generic and anonymous functions
patches = Patch[
Expand Down
12 changes: 6 additions & 6 deletions test/real-open.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Compat: readstring
import Compat: read

let
# Ensure that open cannot find the file "foo"
Expand All @@ -9,19 +9,19 @@ let
# will be preferred over the original `open(::AbstractString)` for `open("foo")`
patch = @patch open(name) = IOBuffer("bar")
apply(patch) do
@test readstring(@mock open("foo")) == "bar"
@test read((@mock open("foo")), String) == "bar"

# The `open(::Any)` patch could result in unintended (or intended) consequences
@test readstring(@mock open(`echo helloworld`)) == "bar"
@test read((@mock open(`echo helloworld`)), String) == "bar"
end

# Better to be specific with your patches
patch = @patch open(name::AbstractString) = IOBuffer("bar")
apply(patch) do
@test readstring(@mock open("foo")) == "bar"
@test read((@mock open("foo")), String) == "bar"

# The more specific `open(::AbstractString)` patches only a single method
io, pobj = (@mock open(`echo helloworld`))
@test readstring(io) == "helloworld\n"
io, pobj = @mock open(`echo helloworld`)
@test read(io, String) == "helloworld\n"
end
end

0 comments on commit 472f091

Please sign in to comment.