4
# ====================================================================
5
class Rake::TestPathMap < Test::Unit::TestCase
7
def test_returns_self_with_no_args
8
assert_equal "abc.rb", "abc.rb".pathmap
11
def test_s_returns_file_separator
12
sep = File::ALT_SEPARATOR || File::SEPARATOR
13
assert_equal sep, "abc.rb".pathmap("%s")
14
assert_equal sep, "".pathmap("%s")
15
assert_equal "a#{sep}b", "a/b".pathmap("%d%s%f")
18
def test_f_returns_basename
19
assert_equal "abc.rb", "abc.rb".pathmap("%f")
20
assert_equal "abc.rb", "this/is/a/dir/abc.rb".pathmap("%f")
21
assert_equal "abc.rb", "/this/is/a/dir/abc.rb".pathmap("%f")
24
def test_n_returns_basename_without_extension
25
assert_equal "abc", "abc.rb".pathmap("%n")
26
assert_equal "abc", "abc".pathmap("%n")
27
assert_equal "abc", "this/is/a/dir/abc.rb".pathmap("%n")
28
assert_equal "abc", "/this/is/a/dir/abc.rb".pathmap("%n")
29
assert_equal "abc", "/this/is/a/dir/abc".pathmap("%n")
32
def test_d_returns_dirname
33
assert_equal ".", "abc.rb".pathmap("%d")
34
assert_equal "/", "/abc".pathmap("%d")
35
assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%d")
36
assert_equal "/this/is/a/dir", "/this/is/a/dir/abc.rb".pathmap("%d")
39
def test_9d_returns_partial_dirname
40
assert_equal "this/is", "this/is/a/dir/abc.rb".pathmap("%2d")
41
assert_equal "this", "this/is/a/dir/abc.rb".pathmap("%1d")
42
assert_equal ".", "this/is/a/dir/abc.rb".pathmap("%0d")
43
assert_equal "dir", "this/is/a/dir/abc.rb".pathmap("%-1d")
44
assert_equal "a/dir", "this/is/a/dir/abc.rb".pathmap("%-2d")
45
assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%100d")
46
assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%-100d")
49
def test_x_returns_extension
50
assert_equal "", "abc".pathmap("%x")
51
assert_equal ".rb", "abc.rb".pathmap("%x")
52
assert_equal ".rb", "abc.xyz.rb".pathmap("%x")
53
assert_equal "", ".depends".pathmap("%x")
54
assert_equal "", "dir/.depends".pathmap("%x")
57
def test_X_returns_everything_but_extension
58
assert_equal "abc", "abc".pathmap("%X")
59
assert_equal "abc", "abc.rb".pathmap("%X")
60
assert_equal "abc.xyz", "abc.xyz.rb".pathmap("%X")
61
assert_equal "ab.xyz", "ab.xyz.rb".pathmap("%X")
62
assert_equal "a.xyz", "a.xyz.rb".pathmap("%X")
63
assert_equal "abc", "abc.rb".pathmap("%X")
64
assert_equal "ab", "ab.rb".pathmap("%X")
65
assert_equal "a", "a.rb".pathmap("%X")
66
assert_equal ".depends", ".depends".pathmap("%X")
67
assert_equal "a/dir/.depends", "a/dir/.depends".pathmap("%X")
68
assert_equal "/.depends", "/.depends".pathmap("%X")
71
def test_p_returns_entire_pathname
72
assert_equal "abc.rb", "abc.rb".pathmap("%p")
73
assert_equal "this/is/a/dir/abc.rb", "this/is/a/dir/abc.rb".pathmap("%p")
74
assert_equal "/this/is/a/dir/abc.rb", "/this/is/a/dir/abc.rb".pathmap("%p")
77
def test_dash_returns_empty_string
78
assert_equal "", "abc.rb".pathmap("%-")
79
assert_equal "abc.rb", "abc.rb".pathmap("%X%-%x")
82
def test_percent_percent_returns_percent
83
assert_equal "a%b", "".pathmap("a%%b")
86
def test_undefined_percent_causes_error
87
ex = assert_raise(ArgumentError) {
88
"dir/abc.rb".pathmap("%z")
92
def test_pattern_returns_substitutions
93
assert_equal "bin/org/osb",
94
"src/org/osb/Xyz.java".pathmap("%{src,bin}d")
97
def test_pattern_can_use_backreferences
98
assert_equal "dir/hi/is", "dir/this/is".pathmap("%{t(hi)s,\\1}p")
101
def test_pattern_with_star_replacement_string_uses_block
102
assert_equal "src/ORG/osb",
103
"src/org/osb/Xyz.java".pathmap("%{/org,*}d") { |d| d.upcase }
104
assert_equal "Xyz.java",
105
"src/org/osb/Xyz.java".pathmap("%{.*,*}f") { |f| f.capitalize }
108
def test_pattern_with_no_replacement_nor_block_substitutes_empty_string
109
assert_equal "bc.rb", "abc.rb".pathmap("%{a}f")
112
def test_pattern_works_with_certain_valid_operators
113
assert_equal "dir/xbc.rb", "dir/abc.rb".pathmap("%{a,x}p")
114
assert_equal "d1r", "dir/abc.rb".pathmap("%{i,1}d")
115
assert_equal "xbc.rb", "dir/abc.rb".pathmap("%{a,x}f")
116
assert_equal ".Rb", "dir/abc.rb".pathmap("%{r,R}x")
117
assert_equal "xbc", "dir/abc.rb".pathmap("%{a,x}n")
120
def test_multiple_patterns
121
assert_equal "this/is/b/directory/abc.rb",
122
"this/is/a/dir/abc.rb".pathmap("%{a,b;dir,\\0ectory}p")
125
def test_partial_directory_selection_works_with_patterns
126
assert_equal "this/is/a/long",
127
"this/is/a/really/long/path/ok.rb".pathmap("%{/really/,/}5d")
130
def test_pattern_with_invalid_operator
131
ex = assert_raise(ArgumentError) do
132
"abc.xyz".pathmap("%{src,bin}z")
134
assert_match(/unknown.*pathmap.*spec.*z/i, ex.message)
137
def test_works_with_windows_separators
138
if File::ALT_SEPARATOR
139
assert_equal "abc", 'dir\abc.rb'.pathmap("%n")
140
assert_equal 'this\is\a\dir',
141
'this\is\a\dir\abc.rb'.pathmap("%d")
145
def test_complex_patterns
146
sep = "".pathmap("%s")
147
assert_equal "dir/abc.rb", "dir/abc.rb".pathmap("%d/%n%x")
148
assert_equal "./abc.rb", "abc.rb".pathmap("%d/%n%x")
149
assert_equal "Your file extension is '.rb'",
150
"dir/abc.rb".pathmap("Your file extension is '%x'")
151
assert_equal "bin/org/onstepback/proj/A.class",
152
"src/org/onstepback/proj/A.java".pathmap("%{src,bin}d/%n.class")
153
assert_equal "src_work/bin/org/onstepback/proj/A.class",
154
"src_work/src/org/onstepback/proj/A.java".pathmap('%{\bsrc\b,bin}X.class')
155
assert_equal ".depends.bak", ".depends".pathmap("%X.bak")
156
assert_equal "d#{sep}a/b/c#{sep}file.txt", "a/b/c/d/file.txt".pathmap("%-1d%s%3d%s%f")
160
class Rake::TestPathMapExplode < Test::Unit::TestCase
162
String.class_eval { public :pathmap_explode }
166
String.class_eval { protected :pathmap_explode }
170
assert_equal ['a'], 'a'.pathmap_explode
171
assert_equal ['a', 'b'], 'a/b'.pathmap_explode
172
assert_equal ['a', 'b', 'c'], 'a/b/c'.pathmap_explode
173
assert_equal ['/', 'a'], '/a'.pathmap_explode
174
assert_equal ['/', 'a', 'b'], '/a/b'.pathmap_explode
175
assert_equal ['/', 'a', 'b', 'c'], '/a/b/c'.pathmap_explode
176
if File::ALT_SEPARATOR
177
assert_equal ['c:.', 'a'], 'c:a'.pathmap_explode
178
assert_equal ['c:.', 'a', 'b'], 'c:a/b'.pathmap_explode
179
assert_equal ['c:.', 'a', 'b', 'c'], 'c:a/b/c'.pathmap_explode
180
assert_equal ['c:/', 'a'], 'c:/a'.pathmap_explode
181
assert_equal ['c:/', 'a', 'b'], 'c:/a/b'.pathmap_explode
182
assert_equal ['c:/', 'a', 'b', 'c'], 'c:/a/b/c'.pathmap_explode
187
class Rake::TestPathMapPartial < Test::Unit::TestCase
188
def test_pathmap_partial
193
assert_equal("1", @path.call(1))
194
assert_equal("1/2", @path.call(2))
195
assert_equal("1/2", @path.call(3))
196
assert_equal(".", @path.call(0))
197
assert_equal("2", @path.call(-1))
198
assert_equal("1/2", @path.call(-2))
199
assert_equal("1/2", @path.call(-3))
203
class Rake::TestFileListPathMap < Test::Unit::TestCase
204
def test_file_list_supports_pathmap
205
assert_equal ['a', 'b'], FileList['dir/a.rb', 'dir/b.rb'].pathmap("%n")