mirror of
https://github.com/ruby/ruby.git
synced 2026-01-26 04:07:58 +00:00
Add error case tests for File.path
- for non-String argument - for NUL-contained argument - for ASCII-incompatible argument
This commit is contained in:
parent
aae8592650
commit
f4f5f0a009
Notes:
git
2025-12-06 11:01:55 +00:00
@ -37,4 +37,45 @@ describe "File.path" do
|
||||
path.should_receive(:to_path).and_return("abc")
|
||||
File.path(path).should == "abc"
|
||||
end
|
||||
|
||||
it "raises TypeError when #to_path result is not a string" do
|
||||
path = mock("path")
|
||||
path.should_receive(:to_path).and_return(nil)
|
||||
-> { File.path(path) }.should raise_error TypeError
|
||||
|
||||
path = mock("path")
|
||||
path.should_receive(:to_path).and_return(42)
|
||||
-> { File.path(path) }.should raise_error TypeError
|
||||
end
|
||||
|
||||
it "raises ArgumentError for string argument contains NUL character" do
|
||||
-> { File.path("\0") }.should raise_error ArgumentError
|
||||
-> { File.path("a\0") }.should raise_error ArgumentError
|
||||
-> { File.path("a\0c") }.should raise_error ArgumentError
|
||||
end
|
||||
|
||||
it "raises ArgumentError when #to_path result contains NUL character" do
|
||||
path = mock("path")
|
||||
path.should_receive(:to_path).and_return("\0")
|
||||
-> { File.path(path) }.should raise_error ArgumentError
|
||||
|
||||
path = mock("path")
|
||||
path.should_receive(:to_path).and_return("a\0")
|
||||
-> { File.path(path) }.should raise_error ArgumentError
|
||||
|
||||
path = mock("path")
|
||||
path.should_receive(:to_path).and_return("a\0c")
|
||||
-> { File.path(path) }.should raise_error ArgumentError
|
||||
end
|
||||
|
||||
it "raises Encoding::CompatibilityError for ASCII-incompatible string argument" do
|
||||
path = "abc".encode(Encoding::UTF_32BE)
|
||||
-> { File.path(path) }.should raise_error Encoding::CompatibilityError
|
||||
end
|
||||
|
||||
it "raises Encoding::CompatibilityError when #to_path result is ASCII-incompatible" do
|
||||
path = mock("path")
|
||||
path.should_receive(:to_path).and_return("abc".encode(Encoding::UTF_32BE))
|
||||
-> { File.path(path) }.should raise_error Encoding::CompatibilityError
|
||||
end
|
||||
end
|
||||
|
||||
@ -204,12 +204,21 @@ class TestFileExhaustive < Test::Unit::TestCase
|
||||
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)}
|
||||
test = ->(&new) do
|
||||
o = new.(42)
|
||||
assert_raise(TypeError, msg) {File.path(o)}
|
||||
|
||||
o = new.("abc".encode(Encoding::UTF_32BE))
|
||||
assert_raise(Encoding::CompatibilityError, msg) {File.path(o)}
|
||||
|
||||
["\0", "a\0", "a\0c"].each do |path|
|
||||
o = new.(path)
|
||||
assert_raise(ArgumentError, msg) {File.path(o)}
|
||||
end
|
||||
end
|
||||
|
||||
test.call(&:itself)
|
||||
test.call(&Struct.new(method).method(:new))
|
||||
}
|
||||
|
||||
conv_error[:to_path]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user