-
-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathdirname_spec.rb
More file actions
170 lines (146 loc) · 6.17 KB
/
dirname_spec.rb
File metadata and controls
170 lines (146 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require_relative '../../spec_helper'
describe "File.dirname" do
it "returns all the components of filename except the last one" do
File.dirname('/home/jason').should == '/home'
File.dirname('/home/jason/poot.txt').should == '/home/jason'
File.dirname('poot.txt').should == '.'
File.dirname('/holy///schnikies//w00t.bin').should == '/holy///schnikies'
File.dirname('').should == '.'
File.dirname('/').should == '/'
File.dirname('/foo/foo').should == '/foo'
end
context "when level is passed" do
it "returns all the components of filename except the last parts by the level" do
File.dirname('/home/jason', 2).should == '/'
File.dirname('/home/jason/poot.txt', 2).should == '/home'
end
it "returns the same String if the level is 0" do
File.dirname('poot.txt', 0).should == 'poot.txt'
File.dirname('/', 0).should == '/'
end
it "raises ArgumentError if the level is negative" do
-> {
File.dirname('/home/jason', -1)
}.should raise_error(ArgumentError, "negative level: -1")
end
it "returns '/' when level exceeds the number of segments in the path" do
File.dirname("/home/jason", 100).should == '/'
end
it "calls #to_int if passed not numeric value" do
object = Object.new
def object.to_int; 2; end
File.dirname("/a/b/c/d", object).should == '/a/b'
end
end
it "returns a String" do
File.dirname("foo").should be_kind_of(String)
end
it "does not modify its argument" do
x = "/usr/bin"
File.dirname(x)
x.should == "/usr/bin"
end
it "ignores a trailing /" do
File.dirname("/foo/bar/").should == "/foo"
end
it "returns the return all the components of filename except the last one (unix format)" do
File.dirname("foo").should =="."
File.dirname("/foo").should =="/"
File.dirname("/foo/bar").should =="/foo"
File.dirname("/foo/bar.txt").should =="/foo"
File.dirname("/foo/bar/baz").should =="/foo/bar"
end
it "returns all the components of filename except the last one (edge cases on all platforms)" do
File.dirname("").should == "."
File.dirname(".").should == "."
File.dirname("./").should == "."
File.dirname("./b/./").should == "./b"
File.dirname("..").should == "."
File.dirname("../").should == "."
File.dirname("/").should == "/"
File.dirname("/.").should == "/"
File.dirname("/foo/").should == "/"
File.dirname("/foo/.").should == "/foo"
File.dirname("/foo/./").should == "/foo"
File.dirname("/foo/../.").should == "/foo/.."
File.dirname("foo/../").should == "foo"
end
it "rejects strings encoded with non ASCII-compatible encodings" do
Encoding.list.reject(&:ascii_compatible?).reject(&:dummy?).each do |enc|
path = "/foo/bar".encode(enc)
-> {
File.dirname(path)
}.should raise_error(Encoding::CompatibilityError)
end
end
it "works with all ASCII-compatible encodings" do
Encoding.list.select(&:ascii_compatible?).each do |enc|
File.dirname("/foo/bar".encode(enc)).should == "/foo".encode(enc)
end
end
it "handles Shift JIS 0x5C (\\) as second byte of a multi-byte sequence" do
# dir/fileソname.txt
path = "dir/file\x83\x5cname.txt".b.force_encoding(Encoding::SHIFT_JIS)
path.valid_encoding?.should be_true
File.dirname(path).should == "dir"
end
platform_is_not :windows do
it "ignores repeated leading / (edge cases on non-windows)" do
File.dirname("/////foo/bar/").should == "/foo"
end
it "returns all the components of filename except the last one (edge cases on non-windows)" do
File.dirname('/////').should == '/'
File.dirname("//foo//").should == "/"
File.dirname('foo\bar').should == '.'
File.dirname('/foo\bar').should == '/'
File.dirname('foo/bar\baz').should == 'foo'
end
end
platform_is :windows do
it "returns all the components of filename except the last one (edge cases on windows)" do
File.dirname("//foo").should == "//foo"
File.dirname("//foo//").should == "//foo"
File.dirname('/////').should == '//'
end
it "handles Shift JIS 0x5C (\\) as second byte of a multi-byte sequence (windows)" do
# dir\fileソname.txt
path = "dir\\file\x83\x5cname.txt".b.force_encoding(Encoding::SHIFT_JIS)
path.valid_encoding?.should be_true
File.dirname(path).should == "dir"
end
end
it "accepts an object that has a #to_path method" do
File.dirname(mock_to_path("/")).should == "/"
end
it "raises a TypeError if not passed a String type" do
-> { File.dirname(nil) }.should raise_error(TypeError, "no implicit conversion of nil into String")
-> { File.dirname(0) }.should raise_error(TypeError, "no implicit conversion of Integer into String")
-> { File.dirname(true) }.should raise_error(TypeError, "no implicit conversion of true into String")
-> { File.dirname(false) }.should raise_error(TypeError, "no implicit conversion of false into String")
end
# Windows specific tests
platform_is :windows do
it "returns the return all the components of filename except the last one (Windows format)" do
File.dirname("C:\\foo\\bar\\baz.txt").should =="C:\\foo\\bar"
File.dirname("C:\\foo\\bar").should =="C:\\foo"
File.dirname("C:\\foo\\bar\\").should == "C:\\foo"
File.dirname("C:\\foo").should == "C:\\"
File.dirname("C:\\").should =="C:\\"
end
it "returns the return all the components of filename except the last one (windows unc)" do
File.dirname("\\\\foo\\bar\\baz.txt").should == "\\\\foo\\bar"
File.dirname("\\\\foo\\bar\\baz").should == "\\\\foo\\bar"
File.dirname("\\\\foo").should =="\\\\foo"
File.dirname("\\\\foo\\bar").should =="\\\\foo\\bar"
File.dirname("\\\\\\foo\\bar").should =="\\\\foo\\bar"
File.dirname("\\\\\\foo").should =="\\\\foo"
end
it "returns the return all the components of filename except the last one (forward_slash)" do
File.dirname("C:/").should == "C:/"
File.dirname("C:/foo").should == "C:/"
File.dirname("C:/foo/bar").should == "C:/foo"
File.dirname("C:/foo/bar/").should == "C:/foo"
File.dirname("C:/foo/bar//").should == "C:/foo"
end
end
end