1require 'test/unit' 2require 'timeout' 3require 'tmpdir' 4require 'tempfile' 5require 'fileutils' 6require_relative 'envutil' 7 8class TestArgf < Test::Unit::TestCase 9 def setup 10 @tmpdir = Dir.mktmpdir 11 @tmp_count = 0 12 @t1 = make_tempfile0("argf-foo") 13 @t1.binmode 14 @t1.puts "1" 15 @t1.puts "2" 16 @t1.close 17 @t2 = make_tempfile0("argf-bar") 18 @t2.binmode 19 @t2.puts "3" 20 @t2.puts "4" 21 @t2.close 22 @t3 = make_tempfile0("argf-baz") 23 @t3.binmode 24 @t3.puts "5" 25 @t3.puts "6" 26 @t3.close 27 end 28 29 def teardown 30 FileUtils.rmtree(@tmpdir) 31 end 32 33 def make_tempfile0(basename) 34 @tmp_count += 1 35 open("#{@tmpdir}/#{basename}-#{@tmp_count}", "w") 36 end 37 38 def make_tempfile 39 t = make_tempfile0("argf-qux") 40 t.puts "foo" 41 t.puts "bar" 42 t.puts "baz" 43 t.close 44 t 45 end 46 47 def ruby(*args) 48 args = ['-e', '$>.write($<.read)'] if args.empty? 49 ruby = EnvUtil.rubybin 50 f = IO.popen([ruby] + args, 'r+') 51 yield(f) 52 ensure 53 f.close unless !f || f.closed? 54 end 55 56 def no_safe_rename 57 /cygwin|mswin|mingw|bccwin/ =~ RUBY_PLATFORM 58 end 59 60 def assert_src_expected(line, src, args = nil) 61 args ||= [@t1.path, @t2.path, @t3.path] 62 expected = src.split(/^/) 63 ruby('-e', src, *args) do |f| 64 expected.each_with_index do |e, i| 65 /#=> *(.*)/ =~ e or next 66 a = f.gets 67 assert_not_nil(a, "[ruby-dev:34445]: remained") 68 assert_equal($1, a.chomp, "[ruby-dev:34445]: line #{line+i}") 69 end 70 end 71 end 72 73 def test_argf 74 assert_src_expected(__LINE__+1, <<-'SRC') 75 a = ARGF 76 b = a.dup 77 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, "1", 1] 78 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, "2", 2] 79 a.rewind 80 b.rewind 81 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, "1", 3] 82 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, "2", 4] 83 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, "3", 5] 84 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, "4", 6] 85 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 7] 86 a.rewind 87 b.rewind 88 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 8] 89 p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, "6", 9] 90 SRC 91 end 92 93 def test_lineno 94 assert_src_expected(__LINE__+1, <<-'SRC') 95 a = ARGF 96 a.gets; p $. #=> 1 97 a.gets; p $. #=> 2 98 a.gets; p $. #=> 3 99 a.rewind; p $. #=> 3 100 a.gets; p $. #=> 3 101 a.gets; p $. #=> 4 102 a.rewind; p $. #=> 4 103 a.gets; p $. #=> 3 104 a.lineno = 1000; p $. #=> 1000 105 a.gets; p $. #=> 1001 106 a.gets; p $. #=> 1002 107 $. = 2000 108 a.gets; p $. #=> 2001 109 a.gets; p $. #=> 2001 110 SRC 111 end 112 113 def test_lineno2 114 assert_src_expected(__LINE__+1, <<-'SRC') 115 a = ARGF.dup 116 a.gets; p $. #=> 1 117 a.gets; p $. #=> 2 118 a.gets; p $. #=> 1 119 a.rewind; p $. #=> 1 120 a.gets; p $. #=> 1 121 a.gets; p $. #=> 2 122 a.gets; p $. #=> 1 123 a.lineno = 1000; p $. #=> 1 124 a.gets; p $. #=> 2 125 a.gets; p $. #=> 2 126 $. = 2000 127 a.gets; p $. #=> 2000 128 a.gets; p $. #=> 2000 129 SRC 130 end 131 132 def test_lineno3 133 assert_in_out_err(["-", @t1.path, @t2.path], <<-INPUT, %w"1 1 1 2 2 2 3 3 1 4 4 2", [], "[ruby-core:25205]") 134 ARGF.each do |line| 135 puts [$., ARGF.lineno, ARGF.file.lineno] 136 end 137 INPUT 138 end 139 140 def test_inplace 141 assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) 142 ARGF.inplace_mode = '.bak' 143 while line = ARGF.gets 144 puts line.chomp + '.new' 145 end 146 INPUT 147 assert_equal("1.new\n2.new\n", File.read(@t1.path)) 148 assert_equal("3.new\n4.new\n", File.read(@t2.path)) 149 assert_equal("5.new\n6.new\n", File.read(@t3.path)) 150 assert_equal("1\n2\n", File.read(@t1.path + ".bak")) 151 assert_equal("3\n4\n", File.read(@t2.path + ".bak")) 152 assert_equal("5\n6\n", File.read(@t3.path + ".bak")) 153 end 154 155 def test_inplace2 156 assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) 157 ARGF.inplace_mode = '.bak' 158 puts ARGF.gets.chomp + '.new' 159 puts ARGF.gets.chomp + '.new' 160 p ARGF.inplace_mode 161 ARGF.inplace_mode = nil 162 puts ARGF.gets.chomp + '.new' 163 puts ARGF.gets.chomp + '.new' 164 p ARGF.inplace_mode 165 ARGF.inplace_mode = '.bak' 166 puts ARGF.gets.chomp + '.new' 167 p ARGF.inplace_mode 168 ARGF.inplace_mode = nil 169 puts ARGF.gets.chomp + '.new' 170 INPUT 171 assert_equal("1.new\n2.new\n\".bak\"\n3.new\n4.new\nnil\n", File.read(@t1.path)) 172 assert_equal("3\n4\n", File.read(@t2.path)) 173 assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path)) 174 assert_equal("1\n2\n", File.read(@t1.path + ".bak")) 175 assert_equal(false, File.file?(@t2.path + ".bak")) 176 assert_equal("5\n6\n", File.read(@t3.path + ".bak")) 177 end 178 179 def test_inplace3 180 assert_in_out_err(["-i.bak", "-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) 181 puts ARGF.gets.chomp + '.new' 182 puts ARGF.gets.chomp + '.new' 183 p $-i 184 $-i = nil 185 puts ARGF.gets.chomp + '.new' 186 puts ARGF.gets.chomp + '.new' 187 p $-i 188 $-i = '.bak' 189 puts ARGF.gets.chomp + '.new' 190 p $-i 191 $-i = nil 192 puts ARGF.gets.chomp + '.new' 193 INPUT 194 assert_equal("1.new\n2.new\n\".bak\"\n3.new\n4.new\nnil\n", File.read(@t1.path)) 195 assert_equal("3\n4\n", File.read(@t2.path)) 196 assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path)) 197 assert_equal("1\n2\n", File.read(@t1.path + ".bak")) 198 assert_equal(false, File.file?(@t2.path + ".bak")) 199 assert_equal("5\n6\n", File.read(@t3.path + ".bak")) 200 end 201 202 def test_inplace_rename_impossible 203 t = make_tempfile 204 205 assert_in_out_err(["-", t.path], <<-INPUT) do |r, e| 206 ARGF.inplace_mode = '/\\\\:' 207 while line = ARGF.gets 208 puts line.chomp + '.new' 209 end 210 INPUT 211 assert_match(/Can't rename .* to .*: .*. skipping file/, e.first) #' 212 assert_equal([], r) 213 assert_equal("foo\nbar\nbaz\n", File.read(t.path)) 214 end 215 end 216 217 def test_inplace_no_backup 218 t = make_tempfile 219 220 assert_in_out_err(["-", t.path], <<-INPUT) do |r, e| 221 ARGF.inplace_mode = '' 222 while line = ARGF.gets 223 puts line.chomp + '.new' 224 end 225 INPUT 226 if no_safe_rename 227 assert_match(/Can't do inplace edit without backup/, e.join) #' 228 else 229 assert_equal([], e) 230 assert_equal([], r) 231 assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path)) 232 end 233 end 234 end 235 236 def test_inplace_dup 237 t = make_tempfile 238 239 assert_in_out_err(["-", t.path], <<-INPUT, [], []) 240 ARGF.inplace_mode = '.bak' 241 f = ARGF.dup 242 while line = f.gets 243 puts line.chomp + '.new' 244 end 245 INPUT 246 assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path)) 247 end 248 249 def test_inplace_stdin 250 t = make_tempfile 251 252 assert_in_out_err(["-", "-"], <<-INPUT, [], /Can't do inplace edit for stdio; skipping/) 253 ARGF.inplace_mode = '.bak' 254 f = ARGF.dup 255 while line = f.gets 256 puts line.chomp + '.new' 257 end 258 INPUT 259 end 260 261 def test_inplace_stdin2 262 t = make_tempfile 263 264 assert_in_out_err(["-"], <<-INPUT, [], /Can't do inplace edit for stdio/) 265 ARGF.inplace_mode = '.bak' 266 while line = ARGF.gets 267 puts line.chomp + '.new' 268 end 269 INPUT 270 end 271 272 def test_encoding 273 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 274 p ARGF.external_encoding.is_a?(Encoding) 275 p ARGF.internal_encoding.is_a?(Encoding) 276 ARGF.gets 277 p ARGF.external_encoding.is_a?(Encoding) 278 p ARGF.internal_encoding 279 SRC 280 assert_equal("true\ntrue\ntrue\nnil\n", f.read) 281 end 282 end 283 284 def test_tell 285 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 286 begin 287 ARGF.binmode 288 loop do 289 p ARGF.tell 290 p ARGF.gets 291 end 292 rescue ArgumentError 293 puts "end" 294 end 295 SRC 296 a = f.read.split("\n") 297 [0, 2, 4, 2, 4, 2, 4].map {|i| i.to_s }. 298 zip((1..6).map {|i| '"' + i.to_s + '\n"' } + ["nil"]).flatten. 299 each do |x| 300 assert_equal(x, a.shift) 301 end 302 assert_equal('end', a.shift) 303 end 304 end 305 306 def test_seek 307 assert_src_expected(__LINE__+1, <<-'SRC') 308 ARGF.seek(4) 309 p ARGF.gets #=> "3\n" 310 ARGF.seek(0, IO::SEEK_END) 311 p ARGF.gets #=> "5\n" 312 ARGF.seek(4) 313 p ARGF.gets #=> nil 314 begin 315 ARGF.seek(0) 316 rescue 317 puts "end" #=> end 318 end 319 SRC 320 end 321 322 def test_set_pos 323 assert_src_expected(__LINE__+1, <<-'SRC') 324 ARGF.pos = 4 325 p ARGF.gets #=> "3\n" 326 ARGF.pos = 4 327 p ARGF.gets #=> "5\n" 328 ARGF.pos = 4 329 p ARGF.gets #=> nil 330 begin 331 ARGF.pos = 4 332 rescue 333 puts "end" #=> end 334 end 335 SRC 336 end 337 338 def test_rewind 339 assert_src_expected(__LINE__+1, <<-'SRC') 340 ARGF.pos = 4 341 ARGF.rewind 342 p ARGF.gets #=> "1\n" 343 ARGF.pos = 4 344 p ARGF.gets #=> "3\n" 345 ARGF.pos = 4 346 p ARGF.gets #=> "5\n" 347 ARGF.pos = 4 348 p ARGF.gets #=> nil 349 begin 350 ARGF.rewind 351 rescue 352 puts "end" #=> end 353 end 354 SRC 355 end 356 357 def test_fileno 358 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 359 p ARGF.fileno 360 ARGF.gets 361 ARGF.gets 362 p ARGF.fileno 363 ARGF.gets 364 ARGF.gets 365 p ARGF.fileno 366 ARGF.gets 367 ARGF.gets 368 p ARGF.fileno 369 ARGF.gets 370 begin 371 ARGF.fileno 372 rescue 373 puts "end" 374 end 375 SRC 376 a = f.read.split("\n") 377 fd1, fd2, fd3, fd4, tag = a 378 assert_match(/^\d+$/, fd1) 379 assert_match(/^\d+$/, fd2) 380 assert_match(/^\d+$/, fd3) 381 assert_match(/^\d+$/, fd4) 382 assert_equal('end', tag) 383 end 384 end 385 386 def test_to_io 387 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 388 8.times do 389 p ARGF.to_io 390 ARGF.gets 391 end 392 SRC 393 a = f.read.split("\n") 394 f11, f12, f13, f21, f22, f31, f32, f4 = a 395 assert_equal(f11, f12) 396 assert_equal(f11, f13) 397 assert_equal(f21, f22) 398 assert_equal(f31, f32) 399 assert_match(/\(closed\)/, f4) 400 f4.sub!(/ \(closed\)/, "") 401 assert_equal(f31, f4) 402 end 403 end 404 405 def test_eof 406 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 407 begin 408 8.times do 409 p ARGF.eof? 410 ARGF.gets 411 end 412 rescue IOError 413 puts "end" 414 end 415 SRC 416 a = f.read.split("\n") 417 (%w(false) + (%w(false true) * 3) + %w(end)).each do |x| 418 assert_equal(x, a.shift) 419 end 420 end 421 422 t1 = open("#{@tmpdir}/argf-hoge", "w") 423 t1.binmode 424 t1.puts "foo" 425 t1.close 426 t2 = open("#{@tmpdir}/argf-moge", "w") 427 t2.binmode 428 t2.puts "bar" 429 t2.close 430 ruby('-e', 'STDERR.reopen(STDOUT); ARGF.gets; ARGF.skip; p ARGF.eof?', t1.path, t2.path) do |f| 431 assert_equal(%w(false), f.read.split(/\n/)) 432 end 433 end 434 435 def test_read 436 ruby('-e', "p ARGF.read(8)", @t1.path, @t2.path, @t3.path) do |f| 437 assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) 438 end 439 end 440 441 def test_read2 442 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 443 s = "" 444 ARGF.read(8, s) 445 p s 446 SRC 447 assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) 448 end 449 end 450 451 def test_read2_with_not_empty_buffer 452 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 453 s = "0123456789" 454 ARGF.read(8, s) 455 p s 456 SRC 457 assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) 458 end 459 end 460 461 def test_read3 462 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 463 nil while ARGF.gets 464 p ARGF.read 465 p ARGF.read(0, "") 466 SRC 467 assert_equal("nil\n\"\"\n", f.read) 468 end 469 end 470 471 def test_readpartial 472 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 473 s = "" 474 begin 475 loop do 476 s << ARGF.readpartial(1) 477 t = ""; ARGF.readpartial(1, t); s << t 478 # not empty buffer 479 u = "abcdef"; ARGF.readpartial(1, u); s << u 480 end 481 rescue EOFError 482 puts s 483 end 484 SRC 485 assert_equal("1\n2\n3\n4\n5\n6\n", f.read) 486 end 487 end 488 489 def test_readpartial2 490 ruby('-e', <<-SRC) do |f| 491 s = "" 492 begin 493 loop do 494 s << ARGF.readpartial(1) 495 t = ""; ARGF.readpartial(1, t); s << t 496 end 497 rescue EOFError 498 $stdout.binmode 499 puts s 500 end 501 SRC 502 f.binmode 503 f.puts("foo") 504 f.puts("bar") 505 f.puts("baz") 506 f.close_write 507 assert_equal("foo\nbar\nbaz\n", f.read) 508 end 509 end 510 511 def test_getc 512 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 513 s = "" 514 while c = ARGF.getc 515 s << c 516 end 517 puts s 518 SRC 519 assert_equal("1\n2\n3\n4\n5\n6\n", f.read) 520 end 521 end 522 523 def test_getbyte 524 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 525 s = [] 526 while c = ARGF.getbyte 527 s << c 528 end 529 p s 530 SRC 531 assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read) 532 end 533 end 534 535 def test_readchar 536 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 537 s = "" 538 begin 539 while c = ARGF.readchar 540 s << c 541 end 542 rescue EOFError 543 puts s 544 end 545 SRC 546 assert_equal("1\n2\n3\n4\n5\n6\n", f.read) 547 end 548 end 549 550 def test_readbyte 551 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 552 begin 553 s = [] 554 while c = ARGF.readbyte 555 s << c 556 end 557 rescue EOFError 558 p s 559 end 560 SRC 561 assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read) 562 end 563 end 564 565 def test_each_line 566 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 567 s = [] 568 ARGF.each_line {|l| s << l } 569 p s 570 SRC 571 assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read) 572 end 573 end 574 575 def test_each_line_paragraph 576 assert_in_out_err(['-e', 'ARGF.each_line("") {|para| p para}'], "a\n\nb\n", 577 ["\"a\\n\\n\"", "\"b\\n\""], []) 578 end 579 580 def test_each_byte 581 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 582 s = [] 583 ARGF.each_byte {|c| s << c } 584 p s 585 SRC 586 assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read) 587 end 588 end 589 590 def test_each_char 591 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 592 s = "" 593 ARGF.each_char {|c| s << c } 594 puts s 595 SRC 596 assert_equal("1\n2\n3\n4\n5\n6\n", f.read) 597 end 598 end 599 600 def test_filename 601 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 602 begin 603 puts ARGF.filename.dump 604 end while ARGF.gets 605 puts ARGF.filename.dump 606 SRC 607 a = f.read.split("\n") 608 assert_equal(@t1.path.dump, a.shift) 609 assert_equal(@t1.path.dump, a.shift) 610 assert_equal(@t1.path.dump, a.shift) 611 assert_equal(@t2.path.dump, a.shift) 612 assert_equal(@t2.path.dump, a.shift) 613 assert_equal(@t3.path.dump, a.shift) 614 assert_equal(@t3.path.dump, a.shift) 615 assert_equal(@t3.path.dump, a.shift) 616 end 617 end 618 619 def test_filename2 620 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 621 begin 622 puts $FILENAME.dump 623 end while ARGF.gets 624 puts $FILENAME.dump 625 SRC 626 a = f.read.split("\n") 627 assert_equal(@t1.path.dump, a.shift) 628 assert_equal(@t1.path.dump, a.shift) 629 assert_equal(@t1.path.dump, a.shift) 630 assert_equal(@t2.path.dump, a.shift) 631 assert_equal(@t2.path.dump, a.shift) 632 assert_equal(@t3.path.dump, a.shift) 633 assert_equal(@t3.path.dump, a.shift) 634 assert_equal(@t3.path.dump, a.shift) 635 end 636 end 637 638 def test_file 639 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 640 begin 641 puts ARGF.file.path.dump 642 end while ARGF.gets 643 puts ARGF.file.path.dump 644 SRC 645 a = f.read.split("\n") 646 assert_equal(@t1.path.dump, a.shift) 647 assert_equal(@t1.path.dump, a.shift) 648 assert_equal(@t1.path.dump, a.shift) 649 assert_equal(@t2.path.dump, a.shift) 650 assert_equal(@t2.path.dump, a.shift) 651 assert_equal(@t3.path.dump, a.shift) 652 assert_equal(@t3.path.dump, a.shift) 653 assert_equal(@t3.path.dump, a.shift) 654 end 655 end 656 657 def test_binmode 658 bug5268 = '[ruby-core:39234]' 659 open(@t3.path, "wb") {|f| f.write "5\r\n6\r\n"} 660 ruby('-e', "ARGF.binmode; STDOUT.binmode; puts ARGF.read", @t1.path, @t2.path, @t3.path) do |f| 661 f.binmode 662 assert_equal("1\n2\n3\n4\n5\r\n6\r\n", f.read, bug5268) 663 end 664 end 665 666 def test_textmode 667 bug5268 = '[ruby-core:39234]' 668 open(@t3.path, "wb") {|f| f.write "5\r\n6\r\n"} 669 ruby('-e', "STDOUT.binmode; puts ARGF.read", @t1.path, @t2.path, @t3.path) do |f| 670 f.binmode 671 assert_equal("1\n2\n3\n4\n5\n6\n", f.read, bug5268) 672 end 673 end unless IO::BINARY.zero? 674 675 def test_skip 676 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 677 ARGF.skip 678 puts ARGF.gets 679 ARGF.skip 680 puts ARGF.read 681 SRC 682 assert_equal("1\n3\n4\n5\n6\n", f.read) 683 end 684 end 685 686 def test_close 687 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 688 ARGF.close 689 puts ARGF.read 690 SRC 691 assert_equal("3\n4\n5\n6\n", f.read) 692 end 693 end 694 695 def test_close_replace 696 ruby('-e', <<-SRC) do |f| 697 ARGF.close 698 ARGV.replace ['#{@t1.path}', '#{@t2.path}', '#{@t3.path}'] 699 puts ARGF.read 700 SRC 701 assert_equal("1\n2\n3\n4\n5\n6\n", f.read) 702 end 703 end 704 705 def test_closed 706 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 707 3.times do 708 p ARGF.closed? 709 ARGF.gets 710 ARGF.gets 711 end 712 p ARGF.closed? 713 ARGF.gets 714 p ARGF.closed? 715 SRC 716 assert_equal("false\nfalse\nfalse\nfalse\ntrue\n", f.read) 717 end 718 end 719 720 def test_argv 721 ruby('-e', "p ARGF.argv; p $*", @t1.path, @t2.path, @t3.path) do |f| 722 assert_equal([@t1.path, @t2.path, @t3.path].inspect, f.gets.chomp) 723 assert_equal([@t1.path, @t2.path, @t3.path].inspect, f.gets.chomp) 724 end 725 end 726 727 def test_readlines_limit_0 728 bug4024 = '[ruby-dev:42538]' 729 t = make_tempfile 730 argf = ARGF.class.new(t.path) 731 begin 732 assert_raise(ArgumentError, bug4024) do 733 argf.readlines(0) 734 end 735 ensure 736 argf.close 737 end 738 end 739 740 def test_each_line_limit_0 741 bug4024 = '[ruby-dev:42538]' 742 t = make_tempfile 743 argf = ARGF.class.new(t.path) 744 begin 745 assert_raise(ArgumentError, bug4024) do 746 argf.each_line(0).next 747 end 748 ensure 749 argf.close 750 end 751 end 752 753 def test_unreadable 754 bug4274 = '[ruby-core:34446]' 755 paths = (1..2).map do 756 t = Tempfile.new("bug4274-") 757 path = t.path 758 t.close! 759 path 760 end 761 argf = ARGF.class.new(*paths) 762 paths.each do |path| 763 e = assert_raise(Errno::ENOENT) {argf.gets} 764 assert_match(/- #{Regexp.quote(path)}\z/, e.message) 765 end 766 assert_nil(argf.gets, bug4274) 767 end 768 769 def test_readlines_twice 770 bug5952 = '[ruby-dev:45160]' 771 assert_ruby_status(["-e", "2.times {STDIN.tty?; readlines}"], "", bug5952) 772 end 773 774 def test_lines 775 ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 776 $stderr = $stdout 777 s = [] 778 ARGF.lines {|l| s << l } 779 p s 780 SRC 781 assert_match(/deprecated/, f.gets) 782 assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read) 783 end 784 end 785 786 def test_bytes 787 ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 788 $stderr = $stdout 789 print Marshal.dump(ARGF.bytes.to_a) 790 SRC 791 assert_match(/deprecated/, f.gets) 792 assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) 793 end 794 end 795 796 def test_chars 797 ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 798 $stderr = $stdout 799 print [Marshal.dump(ARGF.chars.to_a)].pack('m') 800 SRC 801 assert_match(/deprecated/, f.gets) 802 assert_equal(["1", "\n", "2", "\n", "3", "\n", "4", "\n", "5", "\n", "6", "\n"], Marshal.load(f.read.unpack('m').first)) 803 end 804 end 805 806 def test_codepoints 807 ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| 808 $stderr = $stdout 809 print Marshal.dump(ARGF.codepoints.to_a) 810 SRC 811 assert_match(/deprecated/, f.gets) 812 assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) 813 end 814 end 815end 816