PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/stringio/test_stringio.rb

http://github.com/ruby/ruby
Ruby | 815 lines | 705 code | 103 blank | 7 comment | 6 complexity | dcc38b45e83a0186053f0396f00b8a95 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'stringio'
  4. require "rbconfig/sizeof"
  5. require_relative '../ruby/ut_eof'
  6. class TestStringIO < Test::Unit::TestCase
  7. include TestEOF
  8. def open_file(content)
  9. f = StringIO.new(content)
  10. yield f
  11. end
  12. alias open_file_rw open_file
  13. include TestEOF::Seek
  14. def test_initialize
  15. assert_kind_of StringIO, StringIO.new
  16. assert_kind_of StringIO, StringIO.new('str')
  17. assert_kind_of StringIO, StringIO.new('str', 'r+')
  18. assert_raise(ArgumentError) { StringIO.new('', 'x') }
  19. assert_raise(ArgumentError) { StringIO.new('', 'rx') }
  20. assert_raise(ArgumentError) { StringIO.new('', 'rbt') }
  21. assert_raise(TypeError) { StringIO.new(nil) }
  22. o = Object.new
  23. def o.to_str
  24. nil
  25. end
  26. assert_raise(TypeError) { StringIO.new(o) }
  27. o = Object.new
  28. def o.to_str
  29. 'str'
  30. end
  31. assert_kind_of StringIO, StringIO.new(o)
  32. end
  33. def test_truncate
  34. io = StringIO.new("")
  35. io.puts "abc"
  36. io.truncate(0)
  37. io.puts "def"
  38. assert_equal("\0\0\0\0def\n", io.string, "[ruby-dev:24190]")
  39. assert_raise(Errno::EINVAL) { io.truncate(-1) }
  40. io.truncate(10)
  41. assert_equal("\0\0\0\0def\n\0\0", io.string)
  42. end
  43. def test_seek_beyond_eof
  44. io = StringIO.new
  45. n = 100
  46. io.seek(n)
  47. io.print "last"
  48. assert_equal("\0" * n + "last", io.string, "[ruby-dev:24194]")
  49. end
  50. def test_overwrite
  51. stringio = StringIO.new
  52. responses = ['', 'just another ruby', 'hacker']
  53. responses.each do |resp|
  54. stringio.puts(resp)
  55. stringio.rewind
  56. end
  57. assert_equal("hacker\nother ruby\n", stringio.string, "[ruby-core:3836]")
  58. end
  59. def test_gets
  60. assert_equal(nil, StringIO.new("").gets)
  61. assert_equal("\n", StringIO.new("\n").gets)
  62. assert_equal("a\n", StringIO.new("a\n").gets)
  63. assert_equal("a\n", StringIO.new("a\nb\n").gets)
  64. assert_equal("a", StringIO.new("a").gets)
  65. assert_equal("a\n", StringIO.new("a\nb").gets)
  66. assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets)
  67. assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil))
  68. assert_equal("abc\n\n", StringIO.new("abc\n\ndef\n").gets(""))
  69. stringio = StringIO.new("abc\n\ndef\n")
  70. assert_equal("abc\n\n", stringio.gets(""))
  71. assert_equal("def\n", stringio.gets(""))
  72. assert_raise(TypeError){StringIO.new("").gets(1, 1)}
  73. assert_nothing_raised {StringIO.new("").gets(nil, nil)}
  74. assert_string("", Encoding::UTF_8, StringIO.new("foo").gets(0))
  75. end
  76. def test_gets_chomp
  77. assert_equal(nil, StringIO.new("").gets(chomp: true))
  78. assert_equal("", StringIO.new("\n").gets(chomp: true))
  79. assert_equal("a", StringIO.new("a\n").gets(chomp: true))
  80. assert_equal("a", StringIO.new("a\nb\n").gets(chomp: true))
  81. assert_equal("a", StringIO.new("a").gets(chomp: true))
  82. assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
  83. assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
  84. assert_equal("abc\n\ndef", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
  85. assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
  86. stringio = StringIO.new("abc\n\ndef\n")
  87. assert_equal("abc\n", stringio.gets("", chomp: true))
  88. assert_equal("def", stringio.gets("", chomp: true))
  89. assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true))
  90. end
  91. def test_gets_chomp_eol
  92. assert_equal(nil, StringIO.new("").gets(chomp: true))
  93. assert_equal("", StringIO.new("\r\n").gets(chomp: true))
  94. assert_equal("a", StringIO.new("a\r\n").gets(chomp: true))
  95. assert_equal("a", StringIO.new("a\r\nb\r\n").gets(chomp: true))
  96. assert_equal("a", StringIO.new("a").gets(chomp: true))
  97. assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true))
  98. assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
  99. assert_equal("abc\r\n\r\ndef", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
  100. assert_equal("abc\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
  101. stringio = StringIO.new("abc\r\n\r\ndef\r\n")
  102. assert_equal("abc\r\n", stringio.gets("", chomp: true))
  103. assert_equal("def", stringio.gets("", chomp: true))
  104. end
  105. def test_readlines
  106. assert_equal([], StringIO.new("").readlines)
  107. assert_equal(["\n"], StringIO.new("\n").readlines)
  108. assert_equal(["a\n"], StringIO.new("a\n").readlines)
  109. assert_equal(["a\n", "b\n"], StringIO.new("a\nb\n").readlines)
  110. assert_equal(["a"], StringIO.new("a").readlines)
  111. assert_equal(["a\n", "b"], StringIO.new("a\nb").readlines)
  112. assert_equal(["abc\n", "\n", "def\n"], StringIO.new("abc\n\ndef\n").readlines)
  113. assert_equal(["abc\n\ndef\n"], StringIO.new("abc\n\ndef\n").readlines(nil), "[ruby-dev:34591]")
  114. assert_equal(["abc\n\n", "def\n"], StringIO.new("abc\n\ndef\n").readlines(""))
  115. end
  116. def test_write
  117. s = ""
  118. f = StringIO.new(s, "w")
  119. f.print("foo")
  120. f.close
  121. assert_equal("foo", s)
  122. f = StringIO.new(s, File::WRONLY)
  123. f.print("bar")
  124. f.close
  125. assert_equal("bar", s)
  126. f = StringIO.new(s, "a")
  127. o = Object.new
  128. def o.to_s; "baz"; end
  129. f.print(o)
  130. f.close
  131. assert_equal("barbaz", s)
  132. ensure
  133. f.close unless f.closed?
  134. end
  135. def test_write_nonblock_no_exceptions
  136. s = ""
  137. f = StringIO.new(s, "w")
  138. f.write_nonblock("foo", exception: false)
  139. f.close
  140. assert_equal("foo", s)
  141. end
  142. def test_write_nonblock
  143. s = ""
  144. f = StringIO.new(s, "w")
  145. f.write_nonblock("foo")
  146. f.close
  147. assert_equal("foo", s)
  148. f = StringIO.new(s, File::WRONLY)
  149. f.write_nonblock("bar")
  150. f.close
  151. assert_equal("bar", s)
  152. f = StringIO.new(s, "a")
  153. o = Object.new
  154. def o.to_s; "baz"; end
  155. f.write_nonblock(o)
  156. f.close
  157. assert_equal("barbaz", s)
  158. ensure
  159. f.close unless f.closed?
  160. end
  161. def test_write_encoding
  162. s = "".force_encoding(Encoding::UTF_8)
  163. f = StringIO.new(s)
  164. f.print("\u{3053 3093 306b 3061 306f ff01}".b)
  165. assert_equal(Encoding::UTF_8, s.encoding, "honor the original encoding over ASCII-8BIT")
  166. end
  167. def test_write_integer_overflow
  168. f = StringIO.new
  169. f.pos = RbConfig::LIMITS["LONG_MAX"]
  170. assert_raise(ArgumentError) {
  171. f.write("pos + len overflows")
  172. }
  173. end
  174. def test_write_with_multiple_arguments
  175. s = ""
  176. f = StringIO.new(s, "w")
  177. f.write("foo", "bar")
  178. f.close
  179. assert_equal("foobar", s)
  180. ensure
  181. f.close unless f.closed?
  182. end
  183. def test_set_encoding
  184. bug10285 = '[ruby-core:65240] [Bug #10285]'
  185. f = StringIO.new()
  186. f.set_encoding(Encoding::ASCII_8BIT)
  187. f.write("quz \x83 mat".b)
  188. s = "foo \x97 bar".force_encoding(Encoding::WINDOWS_1252)
  189. assert_nothing_raised(Encoding::CompatibilityError, bug10285) {
  190. f.write(s)
  191. }
  192. assert_equal(Encoding::ASCII_8BIT, f.string.encoding, bug10285)
  193. bug11827 = '[ruby-core:72189] [Bug #11827]'
  194. f = StringIO.new("foo\x83".freeze)
  195. assert_nothing_raised(RuntimeError, bug11827) {
  196. f.set_encoding(Encoding::ASCII_8BIT)
  197. }
  198. assert_equal("foo\x83".b, f.gets)
  199. end
  200. def test_mode_error
  201. f = StringIO.new("", "r")
  202. assert_raise(IOError) { f.write("foo") }
  203. f = StringIO.new("", "w")
  204. assert_raise(IOError) { f.read }
  205. assert_raise(Errno::EACCES) { StringIO.new("".freeze, "w") }
  206. s = ""
  207. f = StringIO.new(s, "w")
  208. s.freeze
  209. assert_raise(IOError) { f.write("foo") }
  210. assert_raise(IOError) { StringIO.allocate.read }
  211. ensure
  212. f.close unless f.closed?
  213. end
  214. def test_open
  215. s = ""
  216. StringIO.open("foo") {|f| s = f.read }
  217. assert_equal("foo", s)
  218. end
  219. def test_isatty
  220. assert_equal(false, StringIO.new("").isatty)
  221. end
  222. def test_fsync
  223. assert_equal(0, StringIO.new("").fsync)
  224. end
  225. def test_sync
  226. assert_equal(true, StringIO.new("").sync)
  227. assert_equal(false, StringIO.new("").sync = false)
  228. end
  229. def test_set_fcntl
  230. assert_raise(NotImplementedError) { StringIO.new("").fcntl }
  231. end
  232. def test_close
  233. f = StringIO.new("")
  234. f.close
  235. assert_nil(f.close)
  236. f = StringIO.new("")
  237. f.close_read
  238. f.close_write
  239. assert_nil(f.close)
  240. ensure
  241. f.close unless f.closed?
  242. end
  243. def test_close_read
  244. f = StringIO.new("")
  245. f.close_read
  246. assert_raise(IOError) { f.read }
  247. assert_nothing_raised(IOError) {f.close_read}
  248. f.close
  249. f = StringIO.new("", "w")
  250. assert_raise(IOError) { f.close_read }
  251. f.close
  252. ensure
  253. f.close unless f.closed?
  254. end
  255. def test_close_write
  256. f = StringIO.new("")
  257. f.close_write
  258. assert_raise(IOError) { f.write("foo") }
  259. assert_nothing_raised(IOError) {f.close_write}
  260. f.close
  261. f = StringIO.new("", "r")
  262. assert_raise(IOError) { f.close_write }
  263. f.close
  264. ensure
  265. f.close unless f.closed?
  266. end
  267. def test_closed
  268. f = StringIO.new("")
  269. assert_equal(false, f.closed?)
  270. f.close
  271. assert_equal(true, f.closed?)
  272. ensure
  273. f.close unless f.closed?
  274. end
  275. def test_closed_read
  276. f = StringIO.new("")
  277. assert_equal(false, f.closed_read?)
  278. f.close_write
  279. assert_equal(false, f.closed_read?)
  280. f.close_read
  281. assert_equal(true, f.closed_read?)
  282. ensure
  283. f.close unless f.closed?
  284. end
  285. def test_closed_write
  286. f = StringIO.new("")
  287. assert_equal(false, f.closed_write?)
  288. f.close_read
  289. assert_equal(false, f.closed_write?)
  290. f.close_write
  291. assert_equal(true, f.closed_write?)
  292. ensure
  293. f.close unless f.closed?
  294. end
  295. def test_dup
  296. f1 = StringIO.new("1234")
  297. assert_equal("1", f1.getc)
  298. f2 = f1.dup
  299. assert_equal("2", f2.getc)
  300. assert_equal("3", f1.getc)
  301. assert_equal("4", f2.getc)
  302. assert_equal(nil, f1.getc)
  303. assert_equal(true, f2.eof?)
  304. f1.close
  305. assert_equal(false, f2.closed?, '[ruby-core:48443]')
  306. ensure
  307. f1.close unless f1.closed?
  308. f2.close unless f2.closed?
  309. end
  310. def test_lineno
  311. f = StringIO.new("foo\nbar\nbaz\n")
  312. assert_equal([0, "foo\n"], [f.lineno, f.gets])
  313. assert_equal([1, "bar\n"], [f.lineno, f.gets])
  314. f.lineno = 1000
  315. assert_equal([1000, "baz\n"], [f.lineno, f.gets])
  316. assert_equal([1001, nil], [f.lineno, f.gets])
  317. ensure
  318. f.close unless f.closed?
  319. end
  320. def test_pos
  321. f = StringIO.new("foo\nbar\nbaz\n")
  322. assert_equal([0, "foo\n"], [f.pos, f.gets])
  323. assert_equal([4, "bar\n"], [f.pos, f.gets])
  324. assert_raise(Errno::EINVAL) { f.pos = -1 }
  325. f.pos = 1
  326. assert_equal([1, "oo\n"], [f.pos, f.gets])
  327. assert_equal([4, "bar\n"], [f.pos, f.gets])
  328. assert_equal([8, "baz\n"], [f.pos, f.gets])
  329. assert_equal([12, nil], [f.pos, f.gets])
  330. ensure
  331. f.close unless f.closed?
  332. end
  333. def test_reopen
  334. f = StringIO.new("foo\nbar\nbaz\n")
  335. assert_equal("foo\n", f.gets)
  336. f.reopen("qux\nquux\nquuux\n")
  337. assert_equal("qux\n", f.gets)
  338. f2 = StringIO.new("")
  339. f2.reopen(f)
  340. assert_equal("quux\n", f2.gets)
  341. ensure
  342. f.close unless f.closed?
  343. end
  344. def test_seek
  345. f = StringIO.new("1234")
  346. assert_raise(Errno::EINVAL) { f.seek(-1) }
  347. f.seek(-1, 2)
  348. assert_equal("4", f.getc)
  349. assert_raise(Errno::EINVAL) { f.seek(1, 3) }
  350. f.close
  351. assert_raise(IOError) { f.seek(0) }
  352. ensure
  353. f.close unless f.closed?
  354. end
  355. def test_each_byte
  356. f = StringIO.new("1234")
  357. a = []
  358. f.each_byte {|c| a << c }
  359. assert_equal(%w(1 2 3 4).map {|c| c.ord }, a)
  360. ensure
  361. f.close unless f.closed?
  362. end
  363. def test_getbyte
  364. f = StringIO.new("1234")
  365. assert_equal("1".ord, f.getbyte)
  366. assert_equal("2".ord, f.getbyte)
  367. assert_equal("3".ord, f.getbyte)
  368. assert_equal("4".ord, f.getbyte)
  369. assert_equal(nil, f.getbyte)
  370. ensure
  371. f.close unless f.closed?
  372. end
  373. def test_ungetbyte
  374. s = "foo\nbar\n"
  375. t = StringIO.new(s, "r")
  376. t.ungetbyte(0x41)
  377. assert_equal(0x41, t.getbyte)
  378. t.ungetbyte("qux")
  379. assert_equal("quxfoo\n", t.gets)
  380. t.set_encoding("utf-8")
  381. t.ungetbyte(0x89)
  382. t.ungetbyte(0x8e)
  383. t.ungetbyte("\xe7")
  384. t.ungetbyte("\xe7\xb4\x85")
  385. assert_equal("\u7d05\u7389bar\n", t.gets)
  386. assert_equal("q\u7d05\u7389bar\n", s)
  387. t.pos = 1
  388. t.ungetbyte("\u{30eb 30d3 30fc}")
  389. assert_equal(0, t.pos)
  390. assert_equal("\u{30eb 30d3 30fc}\u7d05\u7389bar\n", s)
  391. assert_nothing_raised {t.ungetbyte(-1)}
  392. assert_nothing_raised {t.ungetbyte(256)}
  393. assert_nothing_raised {t.ungetbyte(1<<64)}
  394. end
  395. def test_ungetc
  396. s = "1234"
  397. f = StringIO.new(s, "r")
  398. assert_nothing_raised { f.ungetc("x") }
  399. assert_equal("x", f.getc) # bug? -> it's a feature from 1.9.
  400. assert_equal("1", f.getc)
  401. s = "1234"
  402. f = StringIO.new(s, "r")
  403. assert_equal("1", f.getc)
  404. f.ungetc("y".ord)
  405. assert_equal("y", f.getc)
  406. assert_equal("2", f.getc)
  407. assert_raise(RangeError) {f.ungetc(0x1ffffff)}
  408. assert_raise(RangeError) {f.ungetc(0xffffffffffffff)}
  409. ensure
  410. f.close unless f.closed?
  411. end
  412. def test_readchar
  413. f = StringIO.new("1234")
  414. a = ""
  415. assert_raise(EOFError) { loop { a << f.readchar } }
  416. assert_equal("1234", a)
  417. end
  418. def test_readbyte
  419. f = StringIO.new("1234")
  420. a = []
  421. assert_raise(EOFError) { loop { a << f.readbyte } }
  422. assert_equal("1234".unpack("C*"), a)
  423. end
  424. def test_each_char
  425. f = StringIO.new("1234")
  426. assert_equal(%w(1 2 3 4), f.each_char.to_a)
  427. end
  428. def test_each_codepoint
  429. f = StringIO.new("1234")
  430. assert_equal([49, 50, 51, 52], f.each_codepoint.to_a)
  431. end
  432. def test_gets2
  433. f = StringIO.new("foo\nbar\nbaz\n")
  434. assert_equal("fo", f.gets(2))
  435. o = Object.new
  436. def o.to_str; "z"; end
  437. assert_equal("o\nbar\nbaz", f.gets(o))
  438. f = StringIO.new("foo\nbar\nbaz\n")
  439. assert_equal("foo\nbar\nbaz", f.gets("az"))
  440. f = StringIO.new("a" * 10000 + "zz!")
  441. assert_equal("a" * 10000 + "zz", f.gets("zz"))
  442. f = StringIO.new("a" * 10000 + "zz!")
  443. assert_equal("a" * 10000 + "zz!", f.gets("zzz"))
  444. bug4112 = '[ruby-dev:42674]'
  445. ["a".encode("utf-16be"), "\u3042"].each do |s|
  446. assert_equal(s, StringIO.new(s).gets(1), bug4112)
  447. assert_equal(s, StringIO.new(s).gets(nil, 1), bug4112)
  448. end
  449. end
  450. def test_each
  451. f = StringIO.new("foo\nbar\nbaz\n")
  452. assert_equal(["foo\n", "bar\n", "baz\n"], f.each.to_a)
  453. f.rewind
  454. assert_equal(["foo", "bar", "baz"], f.each(chomp: true).to_a)
  455. f = StringIO.new("foo\nbar\n\nbaz\n")
  456. assert_equal(["foo\nbar\n\n", "baz\n"], f.each("").to_a)
  457. f.rewind
  458. assert_equal(["foo\nbar\n", "baz"], f.each("", chomp: true).to_a)
  459. f = StringIO.new("foo\r\nbar\r\n\r\nbaz\r\n")
  460. assert_equal(["foo\r\nbar\r\n\r\n", "baz\r\n"], f.each("").to_a)
  461. f.rewind
  462. assert_equal(["foo\r\nbar\r\n", "baz"], f.each("", chomp: true).to_a)
  463. end
  464. def test_putc
  465. s = ""
  466. f = StringIO.new(s, "w")
  467. f.putc("1")
  468. f.putc("2")
  469. f.putc("3")
  470. f.close
  471. assert_equal("123", s)
  472. s = "foo"
  473. f = StringIO.new(s, "a")
  474. f.putc("1")
  475. f.putc("2")
  476. f.putc("3")
  477. f.close
  478. assert_equal("foo123", s)
  479. end
  480. def test_putc_nonascii
  481. s = ""
  482. f = StringIO.new(s, "w")
  483. f.putc("\u{3042}")
  484. f.putc(0x3044)
  485. f.close
  486. assert_equal("\u{3042}D", s)
  487. s = "foo"
  488. f = StringIO.new(s, "a")
  489. f.putc("\u{3042}")
  490. f.putc(0x3044)
  491. f.close
  492. assert_equal("foo\u{3042}D", s)
  493. end
  494. def test_read
  495. f = StringIO.new("\u3042\u3044")
  496. assert_raise(ArgumentError) { f.read(-1) }
  497. assert_raise(ArgumentError) { f.read(1, 2, 3) }
  498. assert_equal("\u3042\u3044", f.read)
  499. assert_nil(f.read(1))
  500. f.rewind
  501. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read(f.size))
  502. bug5207 = '[ruby-core:39026]'
  503. f.rewind
  504. assert_equal("\u3042\u3044", f.read(nil, nil), bug5207)
  505. f.rewind
  506. s = ""
  507. assert_same(s, f.read(nil, s))
  508. assert_equal("\u3042\u3044", s, bug5207)
  509. f.rewind
  510. # not empty buffer
  511. s = "0123456789"
  512. assert_same(s, f.read(nil, s))
  513. assert_equal("\u3042\u3044", s)
  514. bug13806 = '[ruby-core:82349] [Bug #13806]'
  515. assert_string("", Encoding::UTF_8, f.read, bug13806)
  516. assert_string("", Encoding::UTF_8, f.read(nil, nil), bug13806)
  517. s.force_encoding(Encoding::US_ASCII)
  518. assert_same(s, f.read(nil, s))
  519. assert_string("", Encoding::UTF_8, s, bug13806)
  520. end
  521. def test_readpartial
  522. f = StringIO.new("\u3042\u3044")
  523. assert_raise(ArgumentError) { f.readpartial(-1) }
  524. assert_raise(ArgumentError) { f.readpartial(1, 2, 3) }
  525. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(100))
  526. f.rewind
  527. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size))
  528. f.rewind
  529. # not empty buffer
  530. s = '0123456789'
  531. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size, s))
  532. end
  533. def test_read_nonblock
  534. f = StringIO.new("\u3042\u3044")
  535. assert_raise(ArgumentError) { f.read_nonblock(-1) }
  536. assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3) }
  537. assert_equal("\u3042\u3044".force_encoding("BINARY"), f.read_nonblock(100))
  538. assert_raise(EOFError) { f.read_nonblock(10) }
  539. f.rewind
  540. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
  541. end
  542. def test_read_nonblock_no_exceptions
  543. f = StringIO.new("\u3042\u3044")
  544. assert_raise(ArgumentError) { f.read_nonblock(-1, exception: false) }
  545. assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3, exception: false) }
  546. assert_raise(ArgumentError) { f.read_nonblock }
  547. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(100, exception: false))
  548. assert_equal(nil, f.read_nonblock(10, exception: false))
  549. f.rewind
  550. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
  551. f.rewind
  552. # not empty buffer
  553. s = '0123456789'
  554. assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size, s))
  555. end
  556. def test_sysread
  557. f = StringIO.new("sysread \u{30c6 30b9 30c8}")
  558. assert_equal "sysread \u{30c6 30b9 30c8}", f.sysread
  559. assert_equal "", f.sysread
  560. assert_raise(EOFError) { f.sysread(1) }
  561. f.rewind
  562. assert_equal Encoding::ASCII_8BIT, f.sysread(3).encoding
  563. end
  564. def test_size
  565. f = StringIO.new("1234")
  566. assert_equal(4, f.size)
  567. end
  568. # This test is should in ruby/test_method.rb
  569. # However this test depends on stringio library,
  570. # we write it here.
  571. class C < StringIO
  572. alias old_init initialize
  573. attr_reader :foo
  574. def initialize
  575. @foo = :ok
  576. old_init
  577. end
  578. end
  579. def test_method
  580. assert_equal(:ok, C.new.foo, 'Bug #632 [ruby-core:19282]')
  581. end
  582. def test_ungetc_pos
  583. b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
  584. s = StringIO.new( b )
  585. expected_pos = 0
  586. while n = s.getc
  587. assert_equal( expected_pos + 1, s.pos )
  588. s.ungetc( n )
  589. assert_equal( expected_pos, s.pos )
  590. assert_equal( n, s.getc )
  591. expected_pos += 1
  592. end
  593. end
  594. def test_ungetc_padding
  595. s = StringIO.new()
  596. s.pos = 2
  597. s.ungetc("a")
  598. assert_equal("\0""a", s.string)
  599. s.pos = 0
  600. s.ungetc("b")
  601. assert_equal("b""\0""a", s.string)
  602. end
  603. def test_ungetbyte_pos
  604. b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
  605. s = StringIO.new( b )
  606. expected_pos = 0
  607. while n = s.getbyte
  608. assert_equal( expected_pos + 1, s.pos )
  609. s.ungetbyte( n )
  610. assert_equal( expected_pos, s.pos )
  611. assert_equal( n, s.getbyte )
  612. expected_pos += 1
  613. end
  614. end
  615. def test_ungetbyte_padding
  616. s = StringIO.new()
  617. s.pos = 2
  618. s.ungetbyte("a".ord)
  619. assert_equal("\0""a", s.string)
  620. s.pos = 0
  621. s.ungetbyte("b".ord)
  622. assert_equal("b""\0""a", s.string)
  623. end
  624. def test_frozen
  625. s = StringIO.new
  626. s.freeze
  627. bug = '[ruby-core:33648]'
  628. exception_class = defined?(FrozenError) ? FrozenError : RuntimeError
  629. assert_raise(exception_class, bug) {s.puts("foo")}
  630. assert_raise(exception_class, bug) {s.string = "foo"}
  631. assert_raise(exception_class, bug) {s.reopen("")}
  632. end
  633. def test_frozen_string
  634. s = StringIO.new("".freeze)
  635. bug = '[ruby-core:48530]'
  636. assert_raise(IOError, bug) {s.write("foo")}
  637. assert_raise(IOError, bug) {s.ungetc("a")}
  638. assert_raise(IOError, bug) {s.ungetbyte("a")}
  639. end
  640. def test_readlines_limit_0
  641. assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.readlines(0) }
  642. end
  643. def test_each_line_limit_0
  644. assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.each_line(0){} }
  645. assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.each_line("a",0){} }
  646. end
  647. def test_binmode
  648. s = StringIO.new
  649. s.set_encoding('utf-8')
  650. assert_same s, s.binmode
  651. bug_11945 = '[ruby-core:72699] [Bug #11945]'
  652. assert_equal Encoding::ASCII_8BIT, s.external_encoding, bug_11945
  653. end
  654. def test_new_block_warning
  655. assert_warn(/does not take block/) do
  656. StringIO.new {}
  657. end
  658. end
  659. def test_overflow
  660. skip if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
  661. limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
  662. assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
  663. begin;
  664. limit = #{limit}
  665. ary = []
  666. while true
  667. x = "a"*0x100000
  668. break if [x].pack("p").unpack("i!")[0] < 0
  669. ary << x
  670. skip if ary.size > 100
  671. end
  672. s = StringIO.new(x)
  673. s.gets("xxx", limit)
  674. assert_equal(0x100000, s.pos)
  675. end;
  676. end
  677. def test_encoding_write
  678. s = StringIO.new("", "w:utf-32be")
  679. s.print "abc"
  680. assert_equal("abc".encode("utf-32be"), s.string)
  681. end
  682. def test_encoding_read
  683. s = StringIO.new("abc".encode("utf-32be"), "r:utf-8")
  684. assert_equal("\0\0\0a\0\0\0b\0\0\0c", s.read)
  685. end
  686. %w/UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE/.each do |name|
  687. define_method("test_strip_bom:#{name}") do
  688. text = "\uFEFF\u0100a"
  689. content = text.encode(name)
  690. result = StringIO.new(content, mode: 'rb:BOM|UTF-8').read
  691. assert_equal(Encoding.find(name), result.encoding, name)
  692. assert_equal(content[1..-1].b, result.b, name)
  693. StringIO.open(content) {|f|
  694. assert_equal(Encoding.find(name), f.set_encoding_by_bom)
  695. }
  696. end
  697. end
  698. def test_binary_encoding_read_and_default_internal
  699. verbose, $VERBOSE = $VERBOSE, nil
  700. default_internal = Encoding.default_internal
  701. Encoding.default_internal = Encoding::UTF_8
  702. $VERBOSE = verbose
  703. assert_equal Encoding::BINARY, StringIO.new("Hello".b).read.encoding
  704. ensure
  705. $VERBOSE = nil
  706. Encoding.default_internal = default_internal
  707. $VERBOSE = verbose
  708. end
  709. def assert_string(content, encoding, str, mesg = nil)
  710. assert_equal([content, encoding], [str, str.encoding], mesg)
  711. end
  712. end