File: truncate_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (62 lines) | stat: -rw-r--r-- 1,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require_relative '../../spec_helper'
require "stringio"

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

  it "returns an Integer" do
    @io.truncate(4).should be_kind_of(Integer)
  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
    -> { @io.truncate(-1) }.should raise_error(Errno::EINVAL)
    -> { @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 "raises a TypeError when the passed length can't be converted to an Integer" do
    -> { @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")
    -> { io.truncate(2) }.should raise_error(IOError)

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