[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 |
|
---|
[331] | 143 | assert "IO#read(n) with n > IO::BUF_SIZE" do
|
---|
| 144 | r,w = IO.pipe
|
---|
| 145 | n = IO::BUF_SIZE+1
|
---|
| 146 | w.write 'a'*n
|
---|
| 147 | assert_equal r.read(n), 'a'*n
|
---|
| 148 | end
|
---|
| 149 |
|
---|
[279] | 150 | assert('IO#readchar', '15.2.20.5.15') do
|
---|
| 151 | # almost same as IO#getc
|
---|
| 152 | IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
|
---|
| 153 | $mrbtest_io_msg.each_char { |ch|
|
---|
| 154 | assert_equal ch, io.readchar
|
---|
| 155 | }
|
---|
| 156 | assert_raise(EOFError) do
|
---|
| 157 | io.readchar
|
---|
| 158 | end
|
---|
| 159 | end
|
---|
| 160 | end
|
---|
| 161 |
|
---|
| 162 | #assert('IO#readline', '15.2.20.5.16') do
|
---|
| 163 | #assert('IO#readlines', '15.2.20.5.17') do
|
---|
| 164 |
|
---|
| 165 | assert('IO#sync', '15.2.20.5.18') do
|
---|
| 166 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
| 167 | s = io.sync
|
---|
| 168 | assert_true(s == true || s == false)
|
---|
| 169 | io.close
|
---|
| 170 | assert_raise(IOError) do
|
---|
| 171 | io.sync
|
---|
| 172 | end
|
---|
| 173 | end
|
---|
| 174 |
|
---|
| 175 | assert('IO#sync=', '15.2.20.5.19') do
|
---|
| 176 | io = IO.new(IO.sysopen($mrbtest_io_rfname))
|
---|
| 177 | io.sync = true
|
---|
| 178 | assert_true io.sync
|
---|
| 179 | io.sync = false
|
---|
| 180 | assert_false io.sync
|
---|
| 181 | io.close
|
---|
| 182 | assert_raise(IOError) do
|
---|
| 183 | io.sync = true
|
---|
| 184 | end
|
---|
| 185 | end
|
---|
| 186 |
|
---|
| 187 | assert('IO#write', '15.2.20.5.20') do
|
---|
| 188 | io = IO.open(IO.sysopen($mrbtest_io_wfname))
|
---|
| 189 | assert_equal 0, io.write("")
|
---|
| 190 | io.close
|
---|
[331] | 191 |
|
---|
| 192 | io = IO.open(IO.sysopen($mrbtest_io_wfname, "r+"), "r+")
|
---|
| 193 | assert_equal 7, io.write("abcdefg")
|
---|
| 194 | io.rewind
|
---|
| 195 | assert_equal "ab", io.read(2)
|
---|
| 196 | assert_equal 3, io.write("123")
|
---|
| 197 | io.rewind
|
---|
| 198 | assert_equal "ab123fg", io.read
|
---|
| 199 | io.close
|
---|
| 200 |
|
---|
[279] | 201 | true
|
---|
| 202 | end
|
---|
| 203 |
|
---|
| 204 | assert('IO#<<') do
|
---|
| 205 | io = IO.open(IO.sysopen($mrbtest_io_wfname))
|
---|
| 206 | io << "" << ""
|
---|
| 207 | assert_equal 0, io.pos
|
---|
| 208 | io.close
|
---|
| 209 | true
|
---|
| 210 | end
|
---|
| 211 |
|
---|
| 212 | assert('IO.for_fd') do
|
---|
| 213 | fd = IO.sysopen($mrbtest_io_rfname)
|
---|
| 214 | io = IO.for_fd(fd)
|
---|
| 215 | assert_equal $mrbtest_io_msg, io.read
|
---|
| 216 | io.close
|
---|
| 217 | true
|
---|
| 218 | end
|
---|
| 219 |
|
---|
| 220 | assert('IO.new') do
|
---|
| 221 | io = IO.new(0)
|
---|
| 222 | io.close
|
---|
| 223 | true
|
---|
| 224 | end
|
---|
| 225 |
|
---|
| 226 | assert('IO gc check') do
|
---|
| 227 | 100.times { IO.new(0) }
|
---|
| 228 | end
|
---|
| 229 |
|
---|
| 230 | assert('IO.sysopen("./nonexistent")') do
|
---|
| 231 | if Object.const_defined? :Errno
|
---|
| 232 | eclass = Errno::ENOENT
|
---|
| 233 | else
|
---|
| 234 | eclass = RuntimeError
|
---|
| 235 | end
|
---|
| 236 | assert_raise eclass do
|
---|
| 237 | fd = IO.sysopen "./nonexistent"
|
---|
| 238 | IO._sysclose fd
|
---|
| 239 | end
|
---|
| 240 | end
|
---|
| 241 |
|
---|
| 242 | assert('IO.sysopen, IO#sysread') do
|
---|
| 243 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
| 244 | io = IO.new fd
|
---|
| 245 | str1 = " "
|
---|
| 246 | str2 = io.sysread(5, str1)
|
---|
| 247 | assert_equal $mrbtest_io_msg[0,5], str1
|
---|
| 248 | assert_equal $mrbtest_io_msg[0,5], str2
|
---|
| 249 | assert_raise EOFError do
|
---|
| 250 | io.sysread(10000)
|
---|
| 251 | io.sysread(10000)
|
---|
| 252 | end
|
---|
[331] | 253 |
|
---|
| 254 | assert_raise RuntimeError do
|
---|
| 255 | io.sysread(5, "abcde".freeze)
|
---|
| 256 | end
|
---|
| 257 |
|
---|
[279] | 258 | io.close
|
---|
[331] | 259 | assert_equal "", io.sysread(0)
|
---|
| 260 | assert_raise(IOError) { io.sysread(1) }
|
---|
| 261 | assert_raise(ArgumentError) { io.sysread(-1) }
|
---|
[279] | 262 | io.closed?
|
---|
[331] | 263 |
|
---|
| 264 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 265 | io = IO.new fd, "w"
|
---|
| 266 | assert_raise(IOError) { io.sysread(1) }
|
---|
| 267 | io.close
|
---|
| 268 | true
|
---|
[279] | 269 | end
|
---|
| 270 |
|
---|
| 271 | assert('IO.sysopen, IO#syswrite') do
|
---|
| 272 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 273 | io = IO.new fd, "w"
|
---|
| 274 | str = "abcdefg"
|
---|
| 275 | len = io.syswrite(str)
|
---|
| 276 | assert_equal str.size, len
|
---|
| 277 | io.close
|
---|
| 278 |
|
---|
| 279 | io = IO.new(IO.sysopen($mrbtest_io_rfname), "r")
|
---|
| 280 | assert_raise(IOError) { io.syswrite("a") }
|
---|
| 281 | io.close
|
---|
| 282 |
|
---|
| 283 | true
|
---|
| 284 | end
|
---|
| 285 |
|
---|
| 286 | assert('IO#_read_buf') do
|
---|
| 287 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
| 288 | io = IO.new fd
|
---|
| 289 | def io._buf
|
---|
| 290 | @buf
|
---|
| 291 | end
|
---|
| 292 | msg_len = $mrbtest_io_msg.size
|
---|
| 293 | assert_equal '', io._buf
|
---|
| 294 | assert_equal $mrbtest_io_msg, io._read_buf
|
---|
| 295 | assert_equal $mrbtest_io_msg, io._buf
|
---|
| 296 | assert_equal 'mruby', io.read(5)
|
---|
| 297 | assert_equal 5, io.pos
|
---|
| 298 | assert_equal msg_len - 5, io._buf.size
|
---|
| 299 | assert_equal $mrbtest_io_msg[5,100], io.read
|
---|
| 300 | assert_equal 0, io._buf.size
|
---|
| 301 | assert_raise EOFError do
|
---|
| 302 | io._read_buf
|
---|
| 303 | end
|
---|
| 304 | assert_equal true, io.eof
|
---|
| 305 | assert_equal true, io.eof?
|
---|
| 306 | io.close
|
---|
| 307 | io.closed?
|
---|
| 308 | end
|
---|
| 309 |
|
---|
| 310 | assert('IO#isatty') do
|
---|
| 311 | f1 = File.open("/dev/tty")
|
---|
| 312 | f2 = File.open($mrbtest_io_rfname)
|
---|
| 313 |
|
---|
| 314 | assert_true f1.isatty
|
---|
| 315 | assert_false f2.isatty
|
---|
| 316 |
|
---|
| 317 | f1.close
|
---|
| 318 | f2.close
|
---|
| 319 | true
|
---|
| 320 | end
|
---|
| 321 |
|
---|
| 322 | assert('IO#pos=, IO#seek') do
|
---|
| 323 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
| 324 | io = IO.new fd
|
---|
| 325 | def io._buf
|
---|
| 326 | @buf
|
---|
| 327 | end
|
---|
| 328 | assert_equal 'm', io.getc
|
---|
| 329 | assert_equal 1, io.pos
|
---|
| 330 | assert_equal 0, io.seek(0)
|
---|
| 331 | assert_equal 0, io.pos
|
---|
| 332 | io.close
|
---|
| 333 | io.closed?
|
---|
| 334 | end
|
---|
| 335 |
|
---|
| 336 | assert('IO#rewind') do
|
---|
| 337 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
| 338 | io = IO.new fd
|
---|
| 339 | assert_equal 'm', io.getc
|
---|
| 340 | assert_equal 1, io.pos
|
---|
| 341 | assert_equal 0, io.rewind
|
---|
| 342 | assert_equal 0, io.pos
|
---|
| 343 | io.close
|
---|
| 344 | io.closed?
|
---|
| 345 | end
|
---|
| 346 |
|
---|
| 347 | assert('IO#gets') do
|
---|
| 348 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
| 349 | io = IO.new fd
|
---|
| 350 |
|
---|
| 351 | # gets without arguments
|
---|
| 352 | assert_equal $mrbtest_io_msg, io.gets, "gets without arguments"
|
---|
| 353 | assert_equal nil, io.gets, "gets returns nil, when EOF"
|
---|
| 354 |
|
---|
| 355 | # gets with limit
|
---|
| 356 | io.pos = 0
|
---|
| 357 | assert_equal $mrbtest_io_msg[0, 5], io.gets(5), "gets with limit"
|
---|
| 358 |
|
---|
| 359 | # gets with rs
|
---|
| 360 | io.pos = 0
|
---|
| 361 | assert_equal $mrbtest_io_msg[0, 6], io.gets(' '), "gets with rs"
|
---|
| 362 |
|
---|
| 363 | # gets with rs, limit
|
---|
| 364 | io.pos = 0
|
---|
| 365 | assert_equal $mrbtest_io_msg[0, 5], io.gets(' ', 5), "gets with rs, limit"
|
---|
| 366 | io.close
|
---|
| 367 | assert_equal true, io.closed?, "close success"
|
---|
| 368 |
|
---|
| 369 | # reading many-lines file.
|
---|
| 370 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 371 | io = IO.new fd, "w"
|
---|
| 372 | io.write "0123456789" * 2 + "\na"
|
---|
| 373 | assert_equal 22, io.pos
|
---|
| 374 | io.close
|
---|
| 375 | assert_equal true, io.closed?
|
---|
| 376 |
|
---|
| 377 | fd = IO.sysopen $mrbtest_io_wfname
|
---|
| 378 | io = IO.new fd
|
---|
| 379 | line = io.gets
|
---|
| 380 |
|
---|
| 381 | # gets first line
|
---|
| 382 | assert_equal "0123456789" * 2 + "\n", line, "gets first line"
|
---|
| 383 | assert_equal 21, line.size
|
---|
| 384 | assert_equal 21, io.pos
|
---|
| 385 |
|
---|
| 386 | # gets second line
|
---|
| 387 | assert_equal "a", io.gets, "gets second line"
|
---|
| 388 |
|
---|
| 389 | # gets third line
|
---|
| 390 | assert_equal nil, io.gets, "gets third line; returns nil"
|
---|
| 391 |
|
---|
| 392 | io.close
|
---|
| 393 | io.closed?
|
---|
| 394 | end
|
---|
| 395 |
|
---|
| 396 | assert('IO#gets - paragraph mode') do
|
---|
| 397 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 398 | io = IO.new fd, "w"
|
---|
| 399 | io.write "0" * 10 + "\n"
|
---|
| 400 | io.write "1" * 10 + "\n\n"
|
---|
| 401 | io.write "2" * 10 + "\n"
|
---|
| 402 | assert_equal 34, io.pos
|
---|
| 403 | io.close
|
---|
| 404 | assert_equal true, io.closed?
|
---|
| 405 |
|
---|
| 406 | fd = IO.sysopen $mrbtest_io_wfname
|
---|
| 407 | io = IO.new fd
|
---|
| 408 | para1 = "#{'0' * 10}\n#{'1' * 10}\n\n"
|
---|
| 409 | text1 = io.gets("")
|
---|
| 410 | assert_equal para1, text1
|
---|
| 411 | para2 = "#{'2' * 10}\n"
|
---|
| 412 | text2 = io.gets("")
|
---|
| 413 | assert_equal para2, text2
|
---|
| 414 | io.close
|
---|
| 415 | io.closed?
|
---|
| 416 | end
|
---|
| 417 |
|
---|
| 418 | assert('IO.popen') do
|
---|
| 419 | begin
|
---|
| 420 | $? = nil
|
---|
| 421 | io = IO.popen("echo mruby-io")
|
---|
| 422 | assert_true io.close_on_exec?
|
---|
| 423 | assert_equal Fixnum, io.pid.class
|
---|
| 424 |
|
---|
| 425 | out = io.read
|
---|
| 426 | assert_equal out.class, String
|
---|
| 427 | assert_include out, 'mruby-io'
|
---|
| 428 |
|
---|
| 429 | io.close
|
---|
| 430 | if Object.const_defined? :Process
|
---|
| 431 | assert_true $?.success?
|
---|
| 432 | else
|
---|
| 433 | assert_equal 0, $?
|
---|
| 434 | end
|
---|
| 435 |
|
---|
| 436 | assert_true io.closed?
|
---|
| 437 | rescue NotImplementedError => e
|
---|
| 438 | skip e.message
|
---|
| 439 | end
|
---|
| 440 | end
|
---|
| 441 |
|
---|
| 442 | assert('IO.popen with in option') do
|
---|
| 443 | begin
|
---|
| 444 | IO.pipe do |r, w|
|
---|
| 445 | w.write 'hello'
|
---|
| 446 | w.close
|
---|
| 447 | assert_equal "hello", IO.popen("cat", "r", in: r) { |i| i.read }
|
---|
| 448 | assert_equal "", r.read
|
---|
| 449 | end
|
---|
| 450 | assert_raise(ArgumentError) { IO.popen("hello", "r", in: Object.new) }
|
---|
| 451 | rescue NotImplementedError => e
|
---|
| 452 | skip e.message
|
---|
| 453 | end
|
---|
| 454 | end
|
---|
| 455 |
|
---|
| 456 | assert('IO.popen with out option') do
|
---|
| 457 | begin
|
---|
| 458 | IO.pipe do |r, w|
|
---|
| 459 | IO.popen("echo 'hello'", "w", out: w) {}
|
---|
| 460 | w.close
|
---|
| 461 | assert_equal "hello\n", r.read
|
---|
| 462 | end
|
---|
| 463 | rescue NotImplementedError => e
|
---|
| 464 | skip e.message
|
---|
| 465 | end
|
---|
| 466 | end
|
---|
| 467 |
|
---|
| 468 | assert('IO.popen with err option') do
|
---|
| 469 | begin
|
---|
| 470 | IO.pipe do |r, w|
|
---|
| 471 | assert_equal "", IO.popen("echo 'hello' 1>&2", "r", err: w) { |i| i.read }
|
---|
| 472 | w.close
|
---|
| 473 | assert_equal "hello\n", r.read
|
---|
| 474 | end
|
---|
| 475 | rescue NotImplementedError => e
|
---|
| 476 | skip e.message
|
---|
| 477 | end
|
---|
| 478 | end
|
---|
| 479 |
|
---|
| 480 | assert('IO.read') do
|
---|
| 481 | # empty file
|
---|
| 482 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 483 | io = IO.new fd, "w"
|
---|
| 484 | io.close
|
---|
| 485 | assert_equal "", IO.read($mrbtest_io_wfname)
|
---|
| 486 | assert_equal nil, IO.read($mrbtest_io_wfname, 1)
|
---|
| 487 |
|
---|
| 488 | # one byte file
|
---|
| 489 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 490 | io = IO.new fd, "w"
|
---|
| 491 | io.write "123"
|
---|
| 492 | io.close
|
---|
| 493 | assert_equal "123", IO.read($mrbtest_io_wfname)
|
---|
| 494 | assert_equal "", IO.read($mrbtest_io_wfname, 0)
|
---|
| 495 | assert_equal "1", IO.read($mrbtest_io_wfname, 1)
|
---|
| 496 | assert_equal "", IO.read($mrbtest_io_wfname, 0, 10)
|
---|
| 497 | assert_equal "23", IO.read($mrbtest_io_wfname, 2, 1)
|
---|
| 498 | assert_equal "23", IO.read($mrbtest_io_wfname, 10, 1)
|
---|
| 499 | assert_equal "", IO.read($mrbtest_io_wfname, nil, 10)
|
---|
| 500 | assert_equal nil, IO.read($mrbtest_io_wfname, 1, 10)
|
---|
| 501 | end
|
---|
| 502 |
|
---|
| 503 | assert('IO#fileno') do
|
---|
| 504 | fd = IO.sysopen $mrbtest_io_rfname
|
---|
| 505 | io = IO.new fd
|
---|
| 506 | assert_equal io.fileno, fd
|
---|
| 507 | assert_equal io.to_i, fd
|
---|
| 508 | io.close
|
---|
| 509 | io.closed?
|
---|
| 510 | end
|
---|
| 511 |
|
---|
| 512 | assert('IO#close_on_exec') do
|
---|
| 513 | fd = IO.sysopen $mrbtest_io_wfname, "w"
|
---|
| 514 | io = IO.new fd, "w"
|
---|
| 515 | begin
|
---|
| 516 | # IO.sysopen opens a file descripter with O_CLOEXEC flag.
|
---|
| 517 | assert_true io.close_on_exec?
|
---|
| 518 | rescue ScriptError
|
---|
| 519 | skip "IO\#close_on_exec is not implemented."
|
---|
| 520 | end
|
---|
| 521 |
|
---|
| 522 | io.close_on_exec = false
|
---|
| 523 | assert_equal(false, io.close_on_exec?)
|
---|
| 524 | io.close_on_exec = true
|
---|
| 525 | assert_equal(true, io.close_on_exec?)
|
---|
| 526 | io.close_on_exec = false
|
---|
| 527 | assert_equal(false, io.close_on_exec?)
|
---|
| 528 |
|
---|
| 529 | io.close
|
---|
| 530 | io.closed?
|
---|
| 531 |
|
---|
| 532 | begin
|
---|
| 533 | r, w = IO.pipe
|
---|
| 534 | assert_equal(true, r.close_on_exec?)
|
---|
| 535 | r.close_on_exec = false
|
---|
| 536 | assert_equal(false, r.close_on_exec?)
|
---|
| 537 | r.close_on_exec = true
|
---|
| 538 | assert_equal(true, r.close_on_exec?)
|
---|
| 539 |
|
---|
| 540 | assert_equal(true, w.close_on_exec?)
|
---|
| 541 | w.close_on_exec = false
|
---|
| 542 | assert_equal(false, w.close_on_exec?)
|
---|
| 543 | w.close_on_exec = true
|
---|
| 544 | assert_equal(true, w.close_on_exec?)
|
---|
| 545 | ensure
|
---|
| 546 | r.close unless r.closed?
|
---|
| 547 | w.close unless w.closed?
|
---|
| 548 | end
|
---|
| 549 | end
|
---|
| 550 |
|
---|
| 551 | assert('IO#sysseek') do
|
---|
| 552 | IO.open(IO.sysopen($mrbtest_io_rfname)) do |io|
|
---|
| 553 | assert_equal 2, io.sysseek(2)
|
---|
| 554 | assert_equal 5, io.sysseek(3, IO::SEEK_CUR) # 2 + 3 => 5
|
---|
| 555 | assert_equal $mrbtest_io_msg.size - 4, io.sysseek(-4, IO::SEEK_END)
|
---|
| 556 | end
|
---|
| 557 | end
|
---|
| 558 |
|
---|
| 559 | assert('IO.pipe') do
|
---|
| 560 | begin
|
---|
| 561 | called = false
|
---|
| 562 | IO.pipe do |r, w|
|
---|
| 563 | assert_true r.kind_of?(IO)
|
---|
| 564 | assert_true w.kind_of?(IO)
|
---|
| 565 | assert_false r.closed?
|
---|
| 566 | assert_false w.closed?
|
---|
| 567 | assert_true FileTest.pipe?(r)
|
---|
| 568 | assert_true FileTest.pipe?(w)
|
---|
| 569 | assert_nil r.pid
|
---|
| 570 | assert_nil w.pid
|
---|
| 571 | assert_true 2 < r.fileno
|
---|
| 572 | assert_true 2 < w.fileno
|
---|
| 573 | assert_true r.fileno != w.fileno
|
---|
| 574 | assert_false r.sync
|
---|
| 575 | assert_true w.sync
|
---|
| 576 | assert_equal 8, w.write('test for')
|
---|
| 577 | assert_equal 'test', r.read(4)
|
---|
| 578 | assert_equal ' for', r.read(4)
|
---|
| 579 | assert_equal 5, w.write(' pipe')
|
---|
| 580 | assert_equal nil, w.close
|
---|
| 581 | assert_equal ' pipe', r.read
|
---|
| 582 | called = true
|
---|
| 583 | assert_raise(IOError) { r.write 'test' }
|
---|
| 584 | # TODO:
|
---|
| 585 | # This assert expect raise IOError but got RuntimeError
|
---|
| 586 | # Because mruby-io not have flag for I/O readable
|
---|
| 587 | # assert_raise(IOError) { w.read }
|
---|
| 588 | end
|
---|
| 589 | assert_true called
|
---|
| 590 |
|
---|
| 591 | assert_nothing_raised do
|
---|
| 592 | IO.pipe { |r, w| r.close; w.close }
|
---|
| 593 | end
|
---|
| 594 | rescue NotImplementedError => e
|
---|
| 595 | skip e.message
|
---|
| 596 | end
|
---|
| 597 | end
|
---|
| 598 |
|
---|
| 599 | assert('`cmd`') do
|
---|
| 600 | begin
|
---|
| 601 | assert_equal `echo foo`, "foo\n"
|
---|
| 602 | rescue NotImplementedError => e
|
---|
| 603 | skip e.message
|
---|
| 604 | end
|
---|
| 605 | end
|
---|
| 606 |
|
---|
| 607 | assert('IO TEST CLEANUP') do
|
---|
| 608 | assert_nil MRubyIOTestUtil.io_test_cleanup
|
---|
| 609 | end
|
---|