Sha256: aed3ef560da785ed9afaf1eebde715dc8e58a31b77ceaa22e21cc4aa7aeab08d

Contents?: true

Size: 1.93 KB

Versions: 52

Compression:

Stored size: 1.93 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)
require "stringio"

describe "StringIO#truncate when passed [length]" do
  before(:each) do
    @io = StringIO.new('123456789')
  end

  # TODO: Report to Ruby-Core: The RDoc says it always returns 0
  it "returns the passed length" do
    @io.truncate(4).should eql(4)
    @io.truncate(10).should eql(10)
  end

  it "truncated the underlying string down to the passed length" do
    @io.truncate(4)
    @io.string.should == "1234"
  end

  it "does not create a copy of the underlying string" do
    io = StringIO.new(str = "123456789")
    io.truncate(4)
    io.string.should equal(str)
  end

  it "does not change the position" do
    @io.pos = 7
    @io.truncate(4)
    @io.pos.should eql(7)
  end

  it "can grow a string to a larger size, padding it with \\000" do
    @io.truncate(12)
    @io.string.should == "123456789\000\000\000"
  end

  it "raises an Errno::EINVAL when the passed length is negative" do
    lambda { @io.truncate(-1) }.should raise_error(Errno::EINVAL)
    lambda { @io.truncate(-10) }.should raise_error(Errno::EINVAL)
  end

  it "tries to convert the passed length to an Integer using #to_int" do
    obj = mock("to_int")
    obj.should_receive(:to_int).and_return(4)

    @io.truncate(obj)
    @io.string.should == "1234"
  end

  it "returns the passed length Object, NOT the result of #to_int" do
    obj = mock("to_int")
    obj.should_receive(:to_int).and_return(4)
    @io.truncate(obj).should equal(obj)
  end

  it "raises a TypeError when the passed length can't be converted to an Integer" do
    lambda { @io.truncate(Object.new) }.should raise_error(TypeError)
  end
end

describe "StringIO#truncate when self is not writable" do
  it "raises an IOError" do
    io = StringIO.new("test", "r")
    lambda { io.truncate(2) }.should raise_error(IOError)

    io = StringIO.new("test")
    io.close_write
    lambda { io.truncate(2) }.should raise_error(IOError)
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/library/stringio/truncate_spec.rb