PageRenderTime 32ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_argf.rb

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