File: putc_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 (103 lines) | stat: -rw-r--r-- 2,374 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "StringIO#putc when passed [String]" do
  before :each do
    @io = StringIO.new('example')
  end

  it "overwrites the character at the current position" do
    @io.putc("t")
    @io.string.should == "txample"

    @io.pos = 3
    @io.putc("t")
    @io.string.should == "txatple"
  end

  it "only writes the first character from the passed String" do
    @io.putc("test")
    @io.string.should == "txample"
  end

  it "returns the passed String" do
    str = "test"
    @io.putc(str).should equal(str)
  end

  it "correctly updates the current position" do
    @io.putc("t")
    @io.pos.should == 1

    @io.putc("test")
    @io.pos.should == 2

    @io.putc("t")
    @io.pos.should == 3
  end

  it "handles concurrent writes correctly" do
    @io = StringIO.new
    n = 8
    go = false
    threads = n.times.map { |i|
      Thread.new {
        Thread.pass until go
        @io.putc i.to_s
      }
    }
    go = true
    threads.each(&:join)
    @io.string.size.should == n
  end
end

describe "StringIO#putc when passed [Object]" do
  before :each do
    @io = StringIO.new('example')
  end

  it "it writes the passed Integer % 256 to self" do
    @io.putc(333) # 333 % 256 == ?M
    @io.string.should == "Mxample"

    @io.putc(-450) # -450 % 256 == ?>
    @io.string.should == "M>ample"
  end

  it "pads self with \\000 when the current position is after the end" do
    @io.pos = 10
    @io.putc(?A)
    @io.string.should == "example\000\000\000A"
  end

  it "tries to convert the passed argument to an Integer using #to_int" do
    obj = mock('to_int')
    obj.should_receive(:to_int).and_return(116)
    @io.putc(obj)
    @io.string.should == "txample"
  end

  it "raises a TypeError when the passed argument can't be coerced to Integer" do
    -> { @io.putc(Object.new) }.should raise_error(TypeError)
  end
end

describe "StringIO#putc when in append mode" do
  it "appends to the end of self" do
    io = StringIO.new("test", "a")
    io.putc(?t)
    io.string.should == "testt"
  end
end

describe "StringIO#putc when self is not writable" do
  it "raises an IOError" do
    io = StringIO.new("test", "r")
    -> { io.putc(?a) }.should raise_error(IOError)

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