~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/rake/test_pathmap.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'test/unit'
2
 
require 'rake'
3
 
 
4
 
# ====================================================================
5
 
class Rake::TestPathMap < Test::Unit::TestCase
6
 
 
7
 
  def test_returns_self_with_no_args
8
 
    assert_equal "abc.rb", "abc.rb".pathmap
9
 
  end
10
 
 
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")
16
 
  end
17
 
 
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")
22
 
  end
23
 
 
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")
30
 
  end
31
 
 
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")
37
 
  end
38
 
 
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")
47
 
  end
48
 
 
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")
55
 
  end
56
 
 
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")
69
 
  end
70
 
 
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")
75
 
  end
76
 
 
77
 
  def test_dash_returns_empty_string
78
 
    assert_equal "", "abc.rb".pathmap("%-")
79
 
    assert_equal "abc.rb", "abc.rb".pathmap("%X%-%x")
80
 
  end
81
 
 
82
 
  def test_percent_percent_returns_percent
83
 
    assert_equal "a%b", "".pathmap("a%%b")
84
 
  end
85
 
 
86
 
  def test_undefined_percent_causes_error
87
 
    ex = assert_raise(ArgumentError) {
88
 
      "dir/abc.rb".pathmap("%z")
89
 
    }
90
 
  end
91
 
 
92
 
  def test_pattern_returns_substitutions
93
 
    assert_equal "bin/org/osb",
94
 
      "src/org/osb/Xyz.java".pathmap("%{src,bin}d")
95
 
  end
96
 
 
97
 
  def test_pattern_can_use_backreferences
98
 
    assert_equal "dir/hi/is", "dir/this/is".pathmap("%{t(hi)s,\\1}p")
99
 
  end
100
 
 
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 }
106
 
  end
107
 
 
108
 
  def test_pattern_with_no_replacement_nor_block_substitutes_empty_string
109
 
    assert_equal "bc.rb", "abc.rb".pathmap("%{a}f")
110
 
  end
111
 
 
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")
118
 
  end
119
 
 
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")
123
 
  end
124
 
 
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")
128
 
  end
129
 
 
130
 
  def test_pattern_with_invalid_operator
131
 
    ex = assert_raise(ArgumentError) do
132
 
      "abc.xyz".pathmap("%{src,bin}z")
133
 
    end
134
 
    assert_match(/unknown.*pathmap.*spec.*z/i, ex.message)
135
 
  end
136
 
 
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")
142
 
    end
143
 
  end
144
 
 
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")
157
 
  end
158
 
end
159
 
 
160
 
class Rake::TestPathMapExplode < Test::Unit::TestCase
161
 
  def setup
162
 
    String.class_eval { public :pathmap_explode }
163
 
  end
164
 
 
165
 
  def teardown
166
 
    String.class_eval { protected :pathmap_explode }
167
 
  end
168
 
 
169
 
  def test_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
183
 
    end
184
 
  end
185
 
end
186
 
 
187
 
class Rake::TestPathMapPartial < Test::Unit::TestCase
188
 
  def test_pathmap_partial
189
 
    @path = "1/2/file"
190
 
    def @path.call(n)
191
 
      pathmap_partial(n)
192
 
    end
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))
200
 
  end
201
 
end
202
 
 
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")
206
 
  end
207
 
end