summaryrefslogtreecommitdiff
path: root/test/ruby/test_file.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2024-05-25 19:15:25 +0900
committerNobuyoshi Nakada <[email protected]>2024-05-25 19:15:25 +0900
commit0bae2f00025b6b51a9e0c9e3348d5f5d16c2aae4 (patch)
treeddc7f328efdafc77ce3491dff3c05a68090a4c3b /test/ruby/test_file.rb
parent4d0c5486a2773f0474a51033d4f685e5c766ec30 (diff)
[Bug #20510] Do not count optional hash argument for `IO.new`
Since `IO.new` accepts one or two positional arguments except for the optional hash argument, exclude the optional hash argument from the check for delegation to `IO.new`.
Diffstat (limited to 'test/ruby/test_file.rb')
-rw-r--r--test/ruby/test_file.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/test/ruby/test_file.rb b/test/ruby/test_file.rb
index aa10566bfa..9fa96f8a7c 100644
--- a/test/ruby/test_file.rb
+++ b/test/ruby/test_file.rb
@@ -460,6 +460,39 @@ class TestFile < Test::Unit::TestCase
end
end
+ def test_initialize
+ Dir.mktmpdir(__method__.to_s) do |tmpdir|
+ path = File.join(tmpdir, "foo")
+
+ assert_raise(Errno::ENOENT) {File.new(path)}
+ f = File.new(path, "w")
+ f.write("FOO\n")
+ f.close
+ f = File.new(path)
+ data = f.read
+ f.close
+ assert_equal("FOO\n", data)
+
+ f = File.new(path, File::WRONLY)
+ f.write("BAR\n")
+ f.close
+ f = File.new(path, File::RDONLY)
+ data = f.read
+ f.close
+ assert_equal("BAR\n", data)
+
+ data = File.open(path) {|file|
+ File.new(file.fileno, mode: File::RDONLY, autoclose: false).read
+ }
+ assert_equal("BAR\n", data)
+
+ data = File.open(path) {|file|
+ File.new(file.fileno, File::RDONLY, autoclose: false).read
+ }
+ assert_equal("BAR\n", data)
+ end
+ end
+
def test_file_open_newline_option
Dir.mktmpdir(__method__.to_s) do |tmpdir|
path = File.join(tmpdir, "foo")