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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
require 'spec_helper'
require 'mspec/guards'
require 'mspec/helpers'
RSpec.describe IOStub do
before :each do
@out = IOStub.new
@sep = $\
end
after :each do
$\ = @sep
end
it "provides a write method" do
@out.write "this"
expect(@out).to eq("this")
end
it "concatenates the arguments sent to write" do
@out.write "flim ", "flam"
expect(@out).to eq("flim flam")
end
it "provides a print method that appends the default separator" do
$\ = " [newline] "
@out.print "hello"
@out.print "world"
expect(@out).to eq("hello [newline] world [newline] ")
end
it "provides a puts method that appends the default separator" do
@out.puts "hello", 1, 2, 3
expect(@out).to eq("hello\n1\n2\n3\n")
end
it "provides a puts method that appends separator if argument not given" do
@out.puts
expect(@out).to eq("\n")
end
it "provides a printf method" do
@out.printf "%-10s, %03d, %2.1f", "test", 42, 4.2
expect(@out).to eq("test , 042, 4.2")
end
it "provides a flush method that does nothing and returns self" do
expect(@out.flush).to eq(@out)
end
end
RSpec.describe Object, "#new_fd" do
before :each do
@name = tmp("io_specs")
@io = nil
end
after :each do
@io.close if @io and not @io.closed?
rm_r @name
end
it "returns an Integer that can be used to create an IO instance" do
fd = new_fd @name
expect(fd).to be_kind_of(Integer)
@io = IO.new fd, 'w:utf-8'
@io.sync = true
@io.print "io data"
expect(IO.read(@name)).to eq("io data")
end
it "accepts an options Hash" do
allow(FeatureGuard).to receive(:enabled?).and_return(true)
fd = new_fd @name, { :mode => 'w:utf-8' }
expect(fd).to be_kind_of(Integer)
@io = IO.new fd, 'w:utf-8'
@io.sync = true
@io.print "io data"
expect(IO.read(@name)).to eq("io data")
end
it "raises an ArgumentError if the options Hash does not include :mode" do
allow(FeatureGuard).to receive(:enabled?).and_return(true)
expect { new_fd @name, { :encoding => "utf-8" } }.to raise_error(ArgumentError)
end
end
RSpec.describe Object, "#new_io" do
before :each do
@name = tmp("io_specs.txt")
end
after :each do
@io.close if @io and !@io.closed?
rm_r @name
end
it "returns a File instance" do
@io = new_io @name
expect(@io).to be_an_instance_of(File)
end
it "opens the IO for reading if passed 'r'" do
touch(@name) { |f| f.print "io data" }
@io = new_io @name, "r"
expect(@io.read).to eq("io data")
expect { @io.puts "more data" }.to raise_error(IOError)
end
it "opens the IO for writing if passed 'w'" do
@io = new_io @name, "w"
@io.sync = true
@io.print "io data"
expect(IO.read(@name)).to eq("io data")
end
it "opens the IO for reading if passed { :mode => 'r' }" do
touch(@name) { |f| f.print "io data" }
@io = new_io @name, { :mode => "r" }
expect(@io.read).to eq("io data")
expect { @io.puts "more data" }.to raise_error(IOError)
end
it "opens the IO for writing if passed { :mode => 'w' }" do
@io = new_io @name, { :mode => "w" }
@io.sync = true
@io.print "io data"
expect(IO.read(@name)).to eq("io data")
end
end
|