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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
require File.expand_path('../spec_helper', __FILE__)
load_extension('io')
describe "C-API IO function" do
before :each do
@o = CApiIOSpecs.new
@name = tmp("c_api_rb_io_specs")
touch @name
@io = new_io @name, fmode("w:utf-8")
@io.sync = true
end
after :each do
@io.close unless @io.closed?
rm_r @name
end
describe "rb_io_addstr" do
it "calls #to_s to convert the object to a String" do
obj = mock("rb_io_addstr string")
obj.should_receive(:to_s).and_return("rb_io_addstr data")
@o.rb_io_addstr(@io, obj)
@name.should have_data("rb_io_addstr data")
end
it "writes the String to the IO" do
@o.rb_io_addstr(@io, "rb_io_addstr data")
@name.should have_data("rb_io_addstr data")
end
it "returns the io" do
@o.rb_io_addstr(@io, "rb_io_addstr data").should eql(@io)
end
end
describe "rb_io_printf" do
it "calls #to_str to convert the format object to a String" do
obj = mock("rb_io_printf format")
obj.should_receive(:to_str).and_return("%s")
@o.rb_io_printf(@io, [obj, "rb_io_printf"])
@name.should have_data("rb_io_printf")
end
it "calls #to_s to convert the object to a String" do
obj = mock("rb_io_printf string")
obj.should_receive(:to_s).and_return("rb_io_printf")
@o.rb_io_printf(@io, ["%s", obj])
@name.should have_data("rb_io_printf")
end
it "writes the Strings to the IO" do
@o.rb_io_printf(@io, ["%s_%s_%s", "rb", "io", "printf"])
@name.should have_data("rb_io_printf")
end
end
describe "rb_io_print" do
it "calls #to_s to convert the object to a String" do
obj = mock("rb_io_print string")
obj.should_receive(:to_s).and_return("rb_io_print")
@o.rb_io_print(@io, [obj])
@name.should have_data("rb_io_print")
end
it "writes the Strings to the IO with no separator" do
@o.rb_io_print(@io, ["rb_", "io_", "print"])
@name.should have_data("rb_io_print")
end
end
describe "rb_io_puts" do
it "calls #to_s to convert the object to a String" do
obj = mock("rb_io_puts string")
obj.should_receive(:to_s).and_return("rb_io_puts")
@o.rb_io_puts(@io, [obj])
@name.should have_data("rb_io_puts")
end
it "writes the Strings to the IO separated by newlines" do
@o.rb_io_puts(@io, ["rb", "io", "write"])
@name.should have_data("rb\nio\nwrite")
end
end
describe "rb_io_write" do
it "calls #to_s to convert the object to a String" do
obj = mock("rb_io_write string")
obj.should_receive(:to_s).and_return("rb_io_write")
@o.rb_io_write(@io, obj)
@name.should have_data("rb_io_write")
end
it "writes the String to the IO" do
@o.rb_io_write(@io, "rb_io_write")
@name.should have_data("rb_io_write")
end
end
end
describe "C-API IO function" do
before :each do
@o = CApiIOSpecs.new
@name = tmp("c_api_io_specs")
touch @name
@io = new_io @name, fmode("r:utf-8")
end
after :each do
@io.close unless @io.closed?
rm_r @name
end
describe "rb_io_close" do
it "closes an IO object" do
@io.closed?.should be_false
@o.rb_io_close(@io)
@io.closed?.should be_true
end
end
describe "rb_io_check_closed" do
it "does not raise an exception if the IO is not closed" do
# The MRI function is void, so we use should_not raise_error
lambda { @o.rb_io_check_closed(@io) }.should_not raise_error
end
it "raises an error if the IO is closed" do
@io.close
lambda { @o.rb_io_check_writable(@io) }.should raise_error(IOError)
end
end
describe "GetOpenFile" do
it "allows access to the system fileno" do
@o.GetOpenFile_fd($stdin).should == 0
@o.GetOpenFile_fd($stdout).should == 1
@o.GetOpenFile_fd($stderr).should == 2
@o.GetOpenFile_fd(@io).should == @io.fileno
end
end
describe "rb_io_binmode" do
it "returns self" do
@o.rb_io_binmode(@io).should == @io
end
ruby_version_is "1.9" do
it "sets binmode" do
@o.rb_io_binmode(@io)
@io.binmode?.should be_true
end
end
end
end
describe "C-API IO function" do
before :each do
@o = CApiIOSpecs.new
@r_io, @w_io = IO.pipe
@name = tmp("c_api_io_specs")
touch @name
@rw_io = new_io @name, fmode("w+")
end
after :each do
@r_io.close unless @r_io.closed?
@w_io.close unless @w_io.closed?
@rw_io.close unless @rw_io.closed?
rm_r @name
end
describe "rb_io_check_readable" do
it "does not raise an exception if the IO is opened for reading" do
# The MRI function is void, so we use should_not raise_error
lambda { @o.rb_io_check_readable(@r_io) }.should_not raise_error
end
it "does not raise an exception if the IO is opened for read and write" do
lambda { @o.rb_io_check_readable(@rw_io) }.should_not raise_error
end
it "raises an IOError if the IO is not opened for reading" do
lambda { @o.rb_io_check_readable(@w_io) }.should raise_error(IOError)
end
end
describe "rb_io_check_writable" do
it "does not raise an exeption if the IO is opened for writing" do
# The MRI function is void, so we use should_not raise_error
lambda { @o.rb_io_check_writable(@w_io) }.should_not raise_error
end
it "does not raise an exception if the IO is opened for read and write" do
lambda { @o.rb_io_check_writable(@rw_io) }.should_not raise_error
end
it "raises an IOError if the IO is not opened for reading" do
lambda { @o.rb_io_check_writable(@r_io) }.should raise_error(IOError)
end
end
describe "rb_io_wait_writeable" do
it "returns false if there is no error condition" do
@o.rb_io_wait_writable(@w_io).should be_false
end
it "raises an IOError if the IO is closed" do
@w_io.close
lambda { @o.rb_io_wait_writable(@w_io) }.should raise_error(IOError)
end
end
describe "rb_thread_fd_writable" do
it "waits til an fd is ready for writing" do
@o.rb_thread_fd_writable(@w_io).should be_nil
end
end
describe "rb_io_wait_readable" do
it "returns false if there is no error condition" do
@o.rb_io_wait_readable(@r_io, false).should be_false
end
it "raises and IOError if passed a closed stream" do
@r_io.close
lambda { @o.rb_io_wait_readable(@r_io, false) }.should raise_error(IOError)
end
it "blocks until the io is readable and returns true" do
@o.instance_variable_set :@write_data, false
thr = Thread.new do
sleep 0.1 until @o.instance_variable_get(:@write_data)
@w_io.write "rb_io_wait_readable"
end
Thread.pass until thr.alive?
@o.rb_io_wait_readable(@r_io, true).should be_true
@o.instance_variable_get(:@read_data).should == "rb_io_wait_re"
thr.join
end
end
describe "rb_thread_wait_fd" do
it "waits til an fd is ready for reading" do
start = false
thr = Thread.new do
start = true
sleep 0.5
@w_io.write "rb_io_wait_readable"
end
Thread.pass until start
@o.rb_thread_wait_fd(@r_io).should be_nil
thr.join
end
end
end
|