summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2025-08-27 19:31:28 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2025-08-27 20:10:52 +0900
commit08af07b297c0daf3393024f6e03768074d99d9da (patch)
tree4d427b4394a684ccd4de5fb49591ab53088a1859
parent5257e1298c4dc4e854eaa0a9fe5e6dc5c1495c91 (diff)
Add more `File.path` tests
-rw-r--r--test/ruby/test_file_exhaustive.rb19
1 files changed, 15 insertions, 4 deletions
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index b20b597256..222578be26 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -197,12 +197,23 @@ class TestFileExhaustive < Test::Unit::TestCase
[regular_file, utf8_file].each do |file|
assert_equal(file, File.open(file) {|f| f.path})
assert_equal(file, File.path(file))
- o = Object.new
- class << o; self; end.class_eval do
- define_method(:to_path) { file }
- end
+ o = Struct.new(:to_path).new(file)
+ assert_equal(file, File.path(o))
+ o = Struct.new(:to_str).new(file)
assert_equal(file, File.path(o))
end
+
+ conv_error = ->(method, msg = "converting with #{method}") {
+ o = Struct.new(method).new(42)
+ assert_raise(TypeError, msg) {File.path(o)}
+ o = Struct.new(method).new("abc".encode(Encoding::UTF_32BE))
+ assert_raise(Encoding::CompatibilityError, msg) {File.path(o)}
+ o = Struct.new(method).new("\0")
+ assert_raise(ArgumentError, msg) {File.path(o)}
+ }
+
+ conv_error[:to_path]
+ conv_error[:to_str]
end
def assert_integer(n)