summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-07 07:31:09 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-07 07:31:09 +0000
commit415059abf14c63979621be79512713772f9e9970 (patch)
tree193320aa64f741d8469857aac414521a2d6da9b8 /test
parent8667e8b1864379bab0bfe4ac97d4c5eeb61d684f (diff)
io.c: convert arguments just once
* io.c (rb_io_s_foreach, rb_io_s_readlines): convert arguments just once before reading, instead of conversions for each lines. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_io.rb68
1 files changed, 68 insertions, 0 deletions
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 300217be07..505525d95e 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -3276,4 +3276,72 @@ End
end
end
end if File::BINARY != 0
+
+ if RUBY_ENGINE == "ruby" # implementation details
+ def test_foreach_rs_conversion
+ make_tempfile {|t|
+ a = []
+ rs = Struct.new(:count).new(0)
+ def rs.to_str; self.count += 1; "\n"; end
+ IO.foreach(t.path, rs) {|x| a << x }
+ assert_equal(["foo\n", "bar\n", "baz\n"], a)
+ assert_equal(1, rs.count)
+ }
+ end
+
+ def test_foreach_rs_invalid
+ make_tempfile {|t|
+ rs = Object.new
+ def rs.to_str; raise "invalid rs"; end
+ assert_raise(RuntimeError) do
+ IO.foreach(t.path, rs, mode:"w") {}
+ end
+ assert_equal(["foo\n", "bar\n", "baz\n"], IO.foreach(t.path).to_a)
+ }
+ end
+
+ def test_foreach_limit_conversion
+ make_tempfile {|t|
+ a = []
+ lim = Struct.new(:count).new(0)
+ def lim.to_int; self.count += 1; -1; end
+ IO.foreach(t.path, lim) {|x| a << x }
+ assert_equal(["foo\n", "bar\n", "baz\n"], a)
+ assert_equal(1, lim.count)
+ }
+ end
+
+ def test_foreach_limit_invalid
+ make_tempfile {|t|
+ lim = Object.new
+ def lim.to_int; raise "invalid limit"; end
+ assert_raise(RuntimeError) do
+ IO.foreach(t.path, lim, mode:"w") {}
+ end
+ assert_equal(["foo\n", "bar\n", "baz\n"], IO.foreach(t.path).to_a)
+ }
+ end
+
+ def test_readlines_rs_invalid
+ make_tempfile {|t|
+ rs = Object.new
+ def rs.to_str; raise "invalid rs"; end
+ assert_raise(RuntimeError) do
+ IO.readlines(t.path, rs, mode:"w")
+ end
+ assert_equal(["foo\n", "bar\n", "baz\n"], IO.readlines(t.path))
+ }
+ end
+
+ def test_readlines_limit_invalid
+ make_tempfile {|t|
+ lim = Object.new
+ def lim.to_int; raise "invalid limit"; end
+ assert_raise(RuntimeError) do
+ IO.readlines(t.path, lim, mode:"w")
+ end
+ assert_equal(["foo\n", "bar\n", "baz\n"], IO.readlines(t.path))
+ }
+ end
+ end
end