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