| 1 |
## |
| 2 |
# IO Test |
| 3 |
|
| 4 |
MRubyIOTestUtil.io_test_setup |
| 5 |
$cr, $crlf, $cmd = MRubyIOTestUtil.win? ? [1, "\r\n", "cmd /c "] : [0, "\n", ""] |
| 6 |
|
| 7 |
def assert_io_open(meth) |
| 8 |
assert "assert_io_open" do |
| 9 |
fd = IO.sysopen($mrbtest_io_rfname) |
| 10 |
assert_equal Integer, fd.class |
| 11 |
io1 = IO.__send__(meth, fd) |
| 12 |
begin |
| 13 |
assert_equal IO, io1.class |
| 14 |
assert_equal $mrbtest_io_msg, io1.read |
| 15 |
ensure |
| 16 |
io1.close |
| 17 |
end |
| 18 |
|
| 19 |
if meth == :open |
| 20 |
io2 = IO.__send__(meth, IO.sysopen($mrbtest_io_rfname))do |io| |
| 21 |
if meth == :open |
| 22 |
assert_equal $mrbtest_io_msg, io.read |
| 23 |
else |
| 24 |
flunk "IO.#{meth} does not take block" |
| 25 |
end |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
assert_raise(RuntimeError) { IO.__send__(meth, 1023) } # For Windows |
| 30 |
assert_raise(RuntimeError) { IO.__send__(meth, 1 << 26) } |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
assert('IO.class', '15.2.20') do |
| 35 |
assert_equal(Class, IO.class) |
| 36 |
end |
| 37 |
|
| 38 |
assert('IO.superclass', '15.2.20.2') do |
| 39 |
assert_equal(Object, IO.superclass) |
| 40 |
end |
| 41 |
|
| 42 |
assert('IO.ancestors', '15.2.20.3') do |
| 43 |
assert_include(IO.ancestors, Enumerable) |
| 44 |
end |
| 45 |
|
| 46 |
assert('IO.open', '15.2.20.4.1') do |
| 47 |
assert_io_open(:open) |
| 48 |
end |
| 49 |
|
| 50 |
assert('IO#close', '15.2.20.5.1') do |
| 51 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 52 |
assert_nil io.close |
| 53 |
end |
| 54 |
|
| 55 |
assert('IO#closed?', '15.2.20.5.2') do |
| 56 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 57 |
assert_false io.closed? |
| 58 |
io.close |
| 59 |
assert_true io.closed? |
| 60 |
end |
| 61 |
|
| 62 |
#assert('IO#each', '15.2.20.5.3') do |
| 63 |
#assert('IO#each_byte', '15.2.20.5.4') do |
| 64 |
#assert('IO#each_line', '15.2.20.5.5') do |
| 65 |
|
| 66 |
assert('IO#eof?', '15.2.20.5.6') do |
| 67 |
io = IO.new(IO.sysopen($mrbtest_io_wfname, 'w'), 'w') |
| 68 |
assert_raise(IOError) do |
| 69 |
io.eof? |
| 70 |
end |
| 71 |
io.close |
| 72 |
|
| 73 |
# empty file |
| 74 |
io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w'), 'w') |
| 75 |
io.close |
| 76 |
io = IO.open(IO.sysopen($mrbtest_io_wfname, 'r'), 'r') |
| 77 |
assert_true io.eof? |
| 78 |
io.close |
| 79 |
|
| 80 |
# nonempty file |
| 81 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 82 |
assert_false io.eof? |
| 83 |
io.readchar |
| 84 |
assert_false io.eof? |
| 85 |
io.read |
| 86 |
assert_true io.eof? |
| 87 |
io.close |
| 88 |
end |
| 89 |
|
| 90 |
assert('IO#flush', '15.2.20.5.7') do |
| 91 |
# Note: mruby-io does not have any buffer to be flushed now. |
| 92 |
io = IO.new(IO.sysopen($mrbtest_io_wfname)) |
| 93 |
assert_equal io, io.flush |
| 94 |
io.close |
| 95 |
assert_raise(IOError) do |
| 96 |
io.flush |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
assert('IO#getc', '15.2.20.5.8') do |
| 101 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 102 |
$mrbtest_io_msg.split("").each { |ch| |
| 103 |
assert_equal ch, io.getc |
| 104 |
} |
| 105 |
assert_equal nil, io.getc |
| 106 |
io.close |
| 107 |
end |
| 108 |
|
| 109 |
assert('IO#getbyte') do |
| 110 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 111 |
$mrbtest_io_msg.split("").each do |ch| |
| 112 |
assert_equal ch.getbyte(0), io.getbyte |
| 113 |
end |
| 114 |
assert_equal nil, io.getbyte |
| 115 |
io.close |
| 116 |
end |
| 117 |
|
| 118 |
#assert('IO#gets', '15.2.20.5.9') do |
| 119 |
#assert('IO#initialize_copy', '15.2.20.5.10') do |
| 120 |
#assert('IO#print', '15.2.20.5.11') do |
| 121 |
#assert('IO#putc', '15.2.20.5.12') do |
| 122 |
#assert('IO#puts', '15.2.20.5.13') do |
| 123 |
|
| 124 |
assert('IO#read', '15.2.20.5.14') do |
| 125 |
IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| |
| 126 |
assert_raise(ArgumentError) { io.read(-5) } |
| 127 |
assert_raise(TypeError) { io.read("str") } |
| 128 |
|
| 129 |
len = $mrbtest_io_msg.length |
| 130 |
assert_equal '', io.read(0) |
| 131 |
assert_equal 'mruby', io.read(5) |
| 132 |
assert_equal $mrbtest_io_msg[5,len], io.read(len) |
| 133 |
|
| 134 |
assert_equal "", io.read |
| 135 |
assert_nil io.read(1) |
| 136 |
end |
| 137 |
|
| 138 |
IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| |
| 139 |
assert_equal $mrbtest_io_msg, io.read |
| 140 |
end |
| 141 |
end |
| 142 |
|
| 143 |
assert "IO#read(n) with n > IO::BUF_SIZE" do |
| 144 |
buf_size = 4096 # copied from io.c |
| 145 |
skip "pipe is not supported on this platform" if MRubyIOTestUtil.win? |
| 146 |
IO.pipe do |r,w| |
| 147 |
n = buf_size+1 |
| 148 |
w.write 'a'*n |
| 149 |
assert_equal 'a'*n, r.read(n) |
| 150 |
end |
| 151 |
end |
| 152 |
|
| 153 |
assert('IO#readchar', '15.2.20.5.15') do |
| 154 |
# almost same as IO#getc |
| 155 |
IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| |
| 156 |
$mrbtest_io_msg.split("").each { |ch| |
| 157 |
assert_equal ch, io.readchar |
| 158 |
} |
| 159 |
assert_raise(EOFError) do |
| 160 |
io.readchar |
| 161 |
end |
| 162 |
end |
| 163 |
end |
| 164 |
|
| 165 |
#assert('IO#readline', '15.2.20.5.16') do |
| 166 |
#assert('IO#readlines', '15.2.20.5.17') do |
| 167 |
|
| 168 |
assert('IO#sync', '15.2.20.5.18') do |
| 169 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 170 |
s = io.sync |
| 171 |
assert_true(s == true || s == false) |
| 172 |
io.close |
| 173 |
assert_raise(IOError) do |
| 174 |
io.sync |
| 175 |
end |
| 176 |
end |
| 177 |
|
| 178 |
assert('IO#sync=', '15.2.20.5.19') do |
| 179 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 180 |
io.sync = true |
| 181 |
assert_true io.sync |
| 182 |
io.sync = false |
| 183 |
assert_false io.sync |
| 184 |
io.close |
| 185 |
assert_raise(IOError) do |
| 186 |
io.sync = true |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
assert('IO#write', '15.2.20.5.20') do |
| 191 |
io = IO.open(IO.sysopen($mrbtest_io_wfname, "w"), "w") |
| 192 |
assert_equal 1, io.write("a") |
| 193 |
io.close |
| 194 |
|
| 195 |
io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+") |
| 196 |
assert_equal 7, io.write("abcdefg") |
| 197 |
io.rewind |
| 198 |
assert_equal "ab", io.read(2) |
| 199 |
assert_equal 3, io.write("123") |
| 200 |
io.rewind |
| 201 |
assert_equal "ab123fg", io.read |
| 202 |
io.close |
| 203 |
end |
| 204 |
|
| 205 |
assert('IO#<<') do |
| 206 |
io = IO.open(IO.sysopen($mrbtest_io_wfname, "w"), "w") |
| 207 |
io << "a" << "b" |
| 208 |
assert_equal 2, io.pos |
| 209 |
io.close |
| 210 |
end |
| 211 |
|
| 212 |
assert('IO#dup for readable') do |
| 213 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 214 |
dup = io.dup |
| 215 |
assert_true io != dup |
| 216 |
assert_true io.fileno != dup.fileno |
| 217 |
begin |
| 218 |
assert_true dup.close_on_exec? |
| 219 |
rescue NotImplementedError |
| 220 |
end |
| 221 |
assert_equal 'm', dup.sysread(1) |
| 222 |
assert_equal 'r', io.sysread(1) |
| 223 |
assert_equal 'u', dup.sysread(1) |
| 224 |
assert_equal 'b', io.sysread(1) |
| 225 |
assert_equal 'y', dup.sysread(1) |
| 226 |
dup.close |
| 227 |
assert_false io.closed? |
| 228 |
io.close |
| 229 |
end |
| 230 |
|
| 231 |
assert('IO#dup for writable') do |
| 232 |
io = IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+') |
| 233 |
dup = io.dup |
| 234 |
io.syswrite "mruby" |
| 235 |
assert_equal 5, dup.sysseek(0, IO::SEEK_CUR) |
| 236 |
io.sysseek 0, IO::SEEK_SET |
| 237 |
assert_equal 0, dup.sysseek(0, IO::SEEK_CUR) |
| 238 |
assert_equal "mruby", dup.sysread(5) |
| 239 |
dup.close |
| 240 |
io.close |
| 241 |
end |
| 242 |
|
| 243 |
assert('IO.for_fd') do |
| 244 |
assert_io_open(:for_fd) |
| 245 |
end |
| 246 |
|
| 247 |
assert('IO.new') do |
| 248 |
assert_io_open(:new) |
| 249 |
end |
| 250 |
|
| 251 |
assert('IO gc check') do |
| 252 |
assert_nothing_raised { 100.times { IO.new(0) } } |
| 253 |
end |
| 254 |
|
| 255 |
assert('IO.sysopen("./nonexistent")') do |
| 256 |
if Object.const_defined? :Errno |
| 257 |
eclass = Errno::ENOENT |
| 258 |
else |
| 259 |
eclass = RuntimeError |
| 260 |
end |
| 261 |
assert_raise eclass do |
| 262 |
fd = IO.sysopen "./nonexistent" |
| 263 |
IO._sysclose fd |
| 264 |
end |
| 265 |
end |
| 266 |
|
| 267 |
assert('IO.sysopen, IO#sysread') do |
| 268 |
fd = IO.sysopen $mrbtest_io_rfname |
| 269 |
io = IO.new fd |
| 270 |
str1 = " " |
| 271 |
str2 = io.sysread(5, str1) |
| 272 |
assert_equal $mrbtest_io_msg[0,5], str1 |
| 273 |
assert_equal $mrbtest_io_msg[0,5], str2 |
| 274 |
assert_raise EOFError do |
| 275 |
io.sysread(10000) |
| 276 |
io.sysread(10000) |
| 277 |
end |
| 278 |
|
| 279 |
assert_raise RuntimeError do |
| 280 |
io.sysread(5, "abcde".freeze) |
| 281 |
end |
| 282 |
|
| 283 |
io.close |
| 284 |
assert_equal "", io.sysread(0) |
| 285 |
assert_raise(IOError) { io.sysread(1) } |
| 286 |
assert_raise(ArgumentError) { io.sysread(-1) } |
| 287 |
io.closed? |
| 288 |
|
| 289 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 290 |
io = IO.new fd, "w" |
| 291 |
assert_raise(IOError) { io.sysread(1) } |
| 292 |
io.close |
| 293 |
end |
| 294 |
|
| 295 |
assert('IO.sysopen, IO#syswrite') do |
| 296 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 297 |
io = IO.new fd, "w" |
| 298 |
str = "abcdefg" |
| 299 |
len = io.syswrite(str) |
| 300 |
assert_equal str.size, len |
| 301 |
io.close |
| 302 |
|
| 303 |
io = IO.new(IO.sysopen($mrbtest_io_rfname), "r") |
| 304 |
assert_raise(IOError) { io.syswrite("a") } |
| 305 |
io.close |
| 306 |
end |
| 307 |
|
| 308 |
assert('IO#ungetc') do |
| 309 |
io = IO.new(IO.sysopen($mrbtest_io_rfname)) |
| 310 |
assert_equal 'm', io.getc |
| 311 |
assert_nothing_raised{io.ungetc("M")} |
| 312 |
assert_equal 'M', io.getc |
| 313 |
io.close |
| 314 |
end |
| 315 |
|
| 316 |
assert('IO#isatty') do |
| 317 |
skip "isatty is not supported on this platform" if MRubyIOTestUtil.win? |
| 318 |
begin |
| 319 |
f = File.open("/dev/tty") |
| 320 |
rescue RuntimeError => e |
| 321 |
skip e.message |
| 322 |
else |
| 323 |
assert_true f.isatty |
| 324 |
ensure |
| 325 |
f&.close |
| 326 |
end |
| 327 |
begin |
| 328 |
f = File.open($mrbtest_io_rfname) |
| 329 |
assert_false f.isatty |
| 330 |
ensure |
| 331 |
f&.close |
| 332 |
end |
| 333 |
end |
| 334 |
|
| 335 |
assert('IO#pos=, IO#seek') do |
| 336 |
fd = IO.sysopen $mrbtest_io_rfname |
| 337 |
io = IO.new fd |
| 338 |
def io._buf |
| 339 |
@buf |
| 340 |
end |
| 341 |
assert_equal 'm', io.getc |
| 342 |
assert_equal 1, io.pos |
| 343 |
assert_equal 0, io.seek(0) |
| 344 |
assert_equal 0, io.pos |
| 345 |
io.close |
| 346 |
end |
| 347 |
|
| 348 |
assert('IO#rewind') do |
| 349 |
fd = IO.sysopen $mrbtest_io_rfname |
| 350 |
io = IO.new fd |
| 351 |
assert_equal 'm', io.getc |
| 352 |
assert_equal 1, io.pos |
| 353 |
assert_equal 0, io.rewind |
| 354 |
assert_equal 0, io.pos |
| 355 |
io.close |
| 356 |
end |
| 357 |
|
| 358 |
assert('IO#gets') do |
| 359 |
fd = IO.sysopen $mrbtest_io_rfname |
| 360 |
io = IO.new fd |
| 361 |
|
| 362 |
# gets without arguments |
| 363 |
assert_equal $mrbtest_io_msg, io.gets, "gets without arguments" |
| 364 |
assert_equal nil, io.gets, "gets returns nil, when EOF" |
| 365 |
|
| 366 |
# gets with limit |
| 367 |
io.pos = 0 |
| 368 |
assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit" |
| 369 |
|
| 370 |
# gets with rs |
| 371 |
io.pos = 0 |
| 372 |
assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs" |
| 373 |
assert_equal $mrbtest_io_msg[6, 3], io.gets(' '), "gets with rs(2)" |
| 374 |
|
| 375 |
# gets with rs, limit |
| 376 |
io.pos = 0 |
| 377 |
assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit" |
| 378 |
io.close |
| 379 |
assert_equal true, io.closed?, "close success" |
| 380 |
|
| 381 |
# reading many-lines file. |
| 382 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 383 |
io = IO.new fd, "w" |
| 384 |
io.write "0123456789" * 2 + "\na" |
| 385 |
assert_equal 22 + $cr, io.pos |
| 386 |
io.close |
| 387 |
assert_equal true, io.closed? |
| 388 |
|
| 389 |
fd = IO.sysopen $mrbtest_io_wfname |
| 390 |
io = IO.new fd |
| 391 |
line = io.gets |
| 392 |
|
| 393 |
# gets first line |
| 394 |
assert_equal "0123456789" * 2 + "\n", line, "gets first line" |
| 395 |
assert_equal 21, line.size |
| 396 |
assert_equal 21 + $cr, io.pos |
| 397 |
|
| 398 |
# gets second line |
| 399 |
assert_equal "a", io.gets, "gets second line" |
| 400 |
|
| 401 |
# gets third line |
| 402 |
assert_equal nil, io.gets, "gets third line; returns nil" |
| 403 |
|
| 404 |
io.close |
| 405 |
end |
| 406 |
|
| 407 |
assert('IO#gets - paragraph mode') do |
| 408 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 409 |
io = IO.new fd, "w" |
| 410 |
io.write "0" * 10 + "\n" |
| 411 |
io.write "1" * 10 + "\n\n" |
| 412 |
io.write "2" * 10 + "\n" |
| 413 |
assert_equal 34 + $cr * 4, io.pos |
| 414 |
io.close |
| 415 |
|
| 416 |
fd = IO.sysopen $mrbtest_io_wfname |
| 417 |
io = IO.new fd |
| 418 |
para1 = "#{'0' * 10}\n#{'1' * 10}\n\n" |
| 419 |
text1 = io.gets("") |
| 420 |
assert_equal para1, text1 |
| 421 |
para2 = "#{'2' * 10}\n" |
| 422 |
text2 = io.gets("") |
| 423 |
assert_equal para2, text2 |
| 424 |
io.close |
| 425 |
end |
| 426 |
|
| 427 |
assert('IO.popen') do |
| 428 |
begin |
| 429 |
$? = nil |
| 430 |
io = IO.popen("#{$cmd}echo mruby-io") |
| 431 |
assert_true io.close_on_exec? |
| 432 |
assert_equal Integer, io.pid.class |
| 433 |
|
| 434 |
out = io.read |
| 435 |
assert_equal out.class, String |
| 436 |
assert_include out, 'mruby-io' |
| 437 |
|
| 438 |
io.close |
| 439 |
if Object.const_defined? :Process |
| 440 |
assert_true $?.success? |
| 441 |
else |
| 442 |
assert_equal 0, $? |
| 443 |
end |
| 444 |
|
| 445 |
assert_true io.closed? |
| 446 |
rescue NotImplementedError => e |
| 447 |
skip e.message |
| 448 |
end |
| 449 |
end |
| 450 |
|
| 451 |
assert('IO.popen with in option') do |
| 452 |
begin |
| 453 |
IO.pipe do |r, w| |
| 454 |
w.write 'hello' |
| 455 |
w.close |
| 456 |
assert_equal "hello", IO.popen("cat", "r", in: r) { |i| i.read } |
| 457 |
assert_equal "", r.read |
| 458 |
end |
| 459 |
assert_raise(ArgumentError) { IO.popen("hello", "r", in: Object.new) } |
| 460 |
rescue NotImplementedError => e |
| 461 |
skip e.message |
| 462 |
end |
| 463 |
end |
| 464 |
|
| 465 |
assert('IO.popen with out option') do |
| 466 |
begin |
| 467 |
IO.pipe do |r, w| |
| 468 |
IO.popen("echo 'hello'", "w", out: w) {} |
| 469 |
w.close |
| 470 |
assert_equal "hello\n", r.read |
| 471 |
end |
| 472 |
rescue NotImplementedError => e |
| 473 |
skip e.message |
| 474 |
end |
| 475 |
end |
| 476 |
|
| 477 |
assert('IO.popen with err option') do |
| 478 |
begin |
| 479 |
IO.pipe do |r, w| |
| 480 |
assert_equal "", IO.popen("echo 'hello' 1>&2", "r", err: w) { |i| i.read } |
| 481 |
w.close |
| 482 |
assert_equal "hello\n", r.read |
| 483 |
end |
| 484 |
rescue NotImplementedError => e |
| 485 |
skip e.message |
| 486 |
end |
| 487 |
end |
| 488 |
|
| 489 |
assert('IO.read') do |
| 490 |
# empty file |
| 491 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 492 |
io = IO.new fd, "w" |
| 493 |
io.close |
| 494 |
assert_equal "", IO.read($mrbtest_io_wfname) |
| 495 |
assert_equal nil, IO.read($mrbtest_io_wfname, 1) |
| 496 |
|
| 497 |
# one byte file |
| 498 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 499 |
io = IO.new fd, "w" |
| 500 |
io.write "123" |
| 501 |
io.close |
| 502 |
assert_equal "123", IO.read($mrbtest_io_wfname) |
| 503 |
assert_equal "", IO.read($mrbtest_io_wfname, 0) |
| 504 |
assert_equal "1", IO.read($mrbtest_io_wfname, 1) |
| 505 |
assert_equal "", IO.read($mrbtest_io_wfname, 0, 10) |
| 506 |
assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1) |
| 507 |
assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1) |
| 508 |
assert_equal "", IO.read($mrbtest_io_wfname, nil, 10) |
| 509 |
assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10) |
| 510 |
end |
| 511 |
|
| 512 |
assert('IO#fileno') do |
| 513 |
fd = IO.sysopen $mrbtest_io_rfname |
| 514 |
io = IO.new fd |
| 515 |
assert_equal io.fileno, fd |
| 516 |
assert_equal io.to_i, fd |
| 517 |
io.close |
| 518 |
end |
| 519 |
|
| 520 |
assert('IO#close_on_exec') do |
| 521 |
fd = IO.sysopen $mrbtest_io_wfname, "w" |
| 522 |
io = IO.new fd, "w" |
| 523 |
begin |
| 524 |
# IO.sysopen opens a file descriptor with O_CLOEXEC flag. |
| 525 |
assert_true io.close_on_exec? |
| 526 |
rescue ScriptError |
| 527 |
io.close |
| 528 |
skip "IO\#close_on_exec is not implemented." |
| 529 |
end |
| 530 |
|
| 531 |
io.close_on_exec = false |
| 532 |
assert_equal(false, io.close_on_exec?) |
| 533 |
io.close_on_exec = true |
| 534 |
assert_equal(true, io.close_on_exec?) |
| 535 |
io.close_on_exec = false |
| 536 |
assert_equal(false, io.close_on_exec?) |
| 537 |
|
| 538 |
io.close |
| 539 |
|
| 540 |
begin |
| 541 |
r, w = IO.pipe |
| 542 |
assert_equal(true, r.close_on_exec?) |
| 543 |
r.close_on_exec = false |
| 544 |
assert_equal(false, r.close_on_exec?) |
| 545 |
r.close_on_exec = true |
| 546 |
assert_equal(true, r.close_on_exec?) |
| 547 |
|
| 548 |
assert_equal(true, w.close_on_exec?) |
| 549 |
w.close_on_exec = false |
| 550 |
assert_equal(false, w.close_on_exec?) |
| 551 |
w.close_on_exec = true |
| 552 |
assert_equal(true, w.close_on_exec?) |
| 553 |
ensure |
| 554 |
r.close unless r.closed? |
| 555 |
w.close unless w.closed? |
| 556 |
end |
| 557 |
end |
| 558 |
|
| 559 |
assert('IO#sysseek') do |
| 560 |
IO.open(IO.sysopen($mrbtest_io_rfname)) do |io| |
| 561 |
assert_equal 2, io.sysseek(2) |
| 562 |
assert_equal 5, io.sysseek(3, IO::SEEK_CUR) # 2 + 3 => 5 |
| 563 |
assert_equal $mrbtest_io_msg.size - 4, io.sysseek(-4, IO::SEEK_END) |
| 564 |
end |
| 565 |
end |
| 566 |
|
| 567 |
assert('IO#pread') do |
| 568 |
skip "IO#pread is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE |
| 569 |
|
| 570 |
IO.open(IO.sysopen($mrbtest_io_rfname, 'r'), 'r') do |io| |
| 571 |
assert_equal $mrbtest_io_msg.byteslice(5, 8), io.pread(8, 5) |
| 572 |
assert_equal 0, io.pos |
| 573 |
assert_equal $mrbtest_io_msg.byteslice(1, 5), io.pread(5, 1) |
| 574 |
assert_equal 0, io.pos |
| 575 |
assert_raise(RuntimeError) { io.pread(20, -9) } |
| 576 |
end |
| 577 |
end |
| 578 |
|
| 579 |
assert('IO#pwrite') do |
| 580 |
skip "IO#pwrite is not implemented on this configuration" unless MRubyIOTestUtil::MRB_WITH_IO_PREAD_PWRITE |
| 581 |
|
| 582 |
IO.open(IO.sysopen($mrbtest_io_wfname, 'w+'), 'w+') do |io| |
| 583 |
assert_equal 6, io.pwrite("Warld!", 7) |
| 584 |
assert_equal 0, io.pos |
| 585 |
assert_equal 7, io.pwrite("Hello, ", 0) |
| 586 |
assert_equal 0, io.pos |
| 587 |
assert_equal "Hello, Warld!", io.read |
| 588 |
assert_equal 6, io.pwrite("world!", 7) |
| 589 |
assert_equal 13, io.pos |
| 590 |
io.pos = 0 |
| 591 |
assert_equal "Hello, world!", io.read |
| 592 |
end |
| 593 |
end |
| 594 |
|
| 595 |
assert('IO.pipe') do |
| 596 |
begin |
| 597 |
called = false |
| 598 |
IO.pipe do |r, w| |
| 599 |
assert_true r.kind_of?(IO) |
| 600 |
assert_true w.kind_of?(IO) |
| 601 |
assert_false r.closed? |
| 602 |
assert_false w.closed? |
| 603 |
assert_true FileTest.pipe?(r) |
| 604 |
assert_true FileTest.pipe?(w) |
| 605 |
assert_nil r.pid |
| 606 |
assert_nil w.pid |
| 607 |
assert_true 2 < r.fileno |
| 608 |
assert_true 2 < w.fileno |
| 609 |
assert_true r.fileno != w.fileno |
| 610 |
assert_false r.sync |
| 611 |
assert_true w.sync |
| 612 |
assert_equal 8, w.write('test for') |
| 613 |
assert_equal 'test', r.read(4) |
| 614 |
assert_equal ' for', r.read(4) |
| 615 |
assert_equal 5, w.write(' pipe') |
| 616 |
assert_equal nil, w.close |
| 617 |
assert_equal ' pipe', r.read |
| 618 |
called = true |
| 619 |
assert_raise(IOError) { r.write 'test' } |
| 620 |
# TODO: |
| 621 |
# This assert expect raise IOError but got RuntimeError |
| 622 |
# Because mruby-io not have flag for I/O readable |
| 623 |
# assert_raise(IOError) { w.read } |
| 624 |
end |
| 625 |
assert_true called |
| 626 |
|
| 627 |
assert_nothing_raised do |
| 628 |
IO.pipe { |r, w| r.close; w.close } |
| 629 |
end |
| 630 |
rescue NotImplementedError => e |
| 631 |
skip e.message |
| 632 |
end |
| 633 |
end |
| 634 |
|
| 635 |
assert('`cmd`') do |
| 636 |
begin |
| 637 |
assert_equal `#{$cmd}echo foo`, "foo#{$crlf}" |
| 638 |
rescue NotImplementedError => e |
| 639 |
skip e.message |
| 640 |
end |
| 641 |
end |
| 642 |
|
| 643 |
MRubyIOTestUtil.io_test_cleanup |