Skip to content

Commit

Permalink
Specs for IO.copy_stream with arbitrary objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Apr 17, 2013
1 parent 3da147a commit 2bbacf1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions spec/ruby/core/io/copy_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,36 @@
it_behaves_like :io_copy_stream_to_io, nil, IOSpecs::CopyStream
end
end

describe "with non-IO Objects" do
before do
@io = new_io @from_name, "rb"
end

after do
@io.close unless @io.closed?
end

it "calls #readpartial on the source Object if defined" do
from = IOSpecs::CopyStreamReadPartial.new @io

IO.copy_stream(from, @to_name)
@to_name.should have_data(@content)
end

it "calls #read on the source Object" do
from = IOSpecs::CopyStreamRead.new @io

IO.copy_stream(from, @to_name)
@to_name.should have_data(@content)
end

it "calls #write on the destination Object" do
to = mock("io_copy_stream_to_object")
to.should_receive(:write).with(@content).and_return(@content.size)

IO.copy_stream(@from_name, to)
end
end
end
end
20 changes: 20 additions & 0 deletions spec/ruby/core/io/fixtures/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,24 @@ def self.from
@from
end
end

class CopyStreamRead
def initialize(io)
@io = io
end

def read(size, buf=nil)
@io.read size, buf
end
end

class CopyStreamReadPartial
def initialize(io)
@io = io
end

def readpartial(size, buf=nil)
@io.readpartial size, buf
end
end
end

0 comments on commit 2bbacf1

Please sign in to comment.