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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
describe :io_readlines, shared: true do
it "raises TypeError if the first parameter is nil" do
lambda { IO.send(@method, nil, &@object) }.should raise_error(TypeError)
end
it "raises an Errno::ENOENT if the file does not exist" do
name = tmp("nonexistent.txt")
lambda { IO.send(@method, name, &@object) }.should raise_error(Errno::ENOENT)
end
it "yields a single string with entire content when the separator is nil" do
result = IO.send(@method, @name, nil, &@object)
(result ? result : ScratchPad.recorded).should == [IO.read(@name)]
end
it "yields a sequence of paragraphs when the separator is an empty string" do
result = IO.send(@method, @name, "", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_empty_separator
end
ruby_version_is "2.4" do
it "yields a sequence of lines without trailing newline characters when chomp is passed" do
result = IO.send(@method, @name, chomp: true, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_without_newline_characters
end
end
end
describe :io_readlines_options_19, shared: true do
before :each do
@filename = tmp("io readlines options")
end
after :each do
rm_r @filename
end
describe "when passed name" do
it "calls #to_path to convert the name" do
name = mock("io name to_path")
name.should_receive(:to_path).and_return(@name)
IO.send(@method, name, &@object)
end
it "defaults to $/ as the separator" do
result = IO.send(@method, @name, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines
end
end
describe "when passed name, object" do
it "calls #to_str to convert the object to a separator" do
sep = mock("io readlines separator")
sep.should_receive(:to_str).at_least(1).and_return(" ")
result = IO.send(@method, @name, sep, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator
end
describe "when the object is a Fixnum" do
before :each do
@sep = $/
end
after :each do
$/ = @sep
end
it "defaults to $/ as the separator" do
$/ = " "
result = IO.send(@method, @name, 10, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "uses the object as a limit if it is a Fixnum" do
result = IO.send(@method, @name, 10, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_limit
end
end
describe "when the object is a String" do
it "uses the value as the separator" do
result = IO.send(@method, @name, " ", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator
end
it "accepts non-ASCII data as separator" do
result = IO.send(@method, @name, "\303\250".force_encoding("utf-8"), &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_arbitrary_separator
end
end
describe "when the object is a Hash" do
it "uses the value as the options hash" do
result = IO.send(@method, @name, mode: "r", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines
end
end
end
describe "when passed name, object, object" do
describe "when the first object is a Fixnum" do
it "uses the second object as an options Hash" do
lambda do
IO.send(@method, @filename, 10, mode: "w", &@object)
end.should raise_error(IOError)
end
it "calls #to_hash to convert the second object to a Hash" do
options = mock("io readlines options Hash")
options.should_receive(:to_hash).and_return({ mode: "w" })
lambda do
IO.send(@method, @filename, 10, options, &@object)
end.should raise_error(IOError)
end
end
describe "when the first object is a String" do
it "uses the second object as a limit if it is a Fixnum" do
result = IO.send(@method, @name, " ", 10, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "calls #to_int to convert the second object" do
limit = mock("io readlines limit")
limit.should_receive(:to_int).at_least(1).and_return(10)
result = IO.send(@method, @name, " ", limit, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "uses the second object as an options Hash" do
lambda do
IO.send(@method, @filename, " ", mode: "w", &@object)
end.should raise_error(IOError)
end
it "calls #to_hash to convert the second object to a Hash" do
options = mock("io readlines options Hash")
options.should_receive(:to_hash).and_return({ mode: "w" })
lambda do
IO.send(@method, @filename, " ", options, &@object)
end.should raise_error(IOError)
end
end
describe "when the first object is not a String or Fixnum" do
it "calls #to_str to convert the object to a String" do
sep = mock("io readlines separator")
sep.should_receive(:to_str).at_least(1).and_return(" ")
result = IO.send(@method, @name, sep, 10, mode: "r", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "uses the second object as a limit if it is a Fixnum" do
result = IO.send(@method, @name, " ", 10, mode: "r", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "calls #to_int to convert the second object" do
limit = mock("io readlines limit")
limit.should_receive(:to_int).at_least(1).and_return(10)
result = IO.send(@method, @name, " ", limit, &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "uses the second object as an options Hash" do
lambda do
IO.send(@method, @filename, " ", mode: "w", &@object)
end.should raise_error(IOError)
end
it "calls #to_hash to convert the second object to a Hash" do
options = mock("io readlines options Hash")
options.should_receive(:to_hash).and_return({ mode: "w" })
lambda do
IO.send(@method, @filename, " ", options, &@object)
end.should raise_error(IOError)
end
end
end
describe "when passed name, separator, limit, options" do
it "calls #to_path to convert the name object" do
name = mock("io name to_path")
name.should_receive(:to_path).and_return(@name)
result = IO.send(@method, name, " ", 10, mode: "r", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "calls #to_str to convert the separator object" do
sep = mock("io readlines separator")
sep.should_receive(:to_str).at_least(1).and_return(" ")
result = IO.send(@method, @name, sep, 10, mode: "r", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "calls #to_int to convert the limit argument" do
limit = mock("io readlines limit")
limit.should_receive(:to_int).at_least(1).and_return(10)
result = IO.send(@method, @name, " ", limit, mode: "r", &@object)
(result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit
end
it "calls #to_hash to convert the options object" do
options = mock("io readlines options Hash")
options.should_receive(:to_hash).and_return({ mode: "w" })
lambda do
IO.send(@method, @filename, " ", 10, options, &@object)
end.should raise_error(IOError)
end
end
end
|