PageRenderTime 308ms CodeModel.GetById 277ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/test/test_rake_path_map.rb

http://github.com/agross/netopenspace
Ruby | 157 lines | 133 code | 24 blank | 0 comment | 2 complexity | 4753f96768e74c5fb4c85982a7197e07 MD5 | raw file
  1require File.expand_path('../helper', __FILE__)
  2
  3class TestRakePathMap < Rake::TestCase
  4
  5  def test_returns_self_with_no_args
  6    assert_equal "abc.rb", "abc.rb".pathmap
  7  end
  8
  9  def test_s_returns_file_separator
 10    sep = File::ALT_SEPARATOR || File::SEPARATOR
 11    assert_equal sep, "abc.rb".pathmap("%s")
 12    assert_equal sep, "".pathmap("%s")
 13    assert_equal "a#{sep}b", "a/b".pathmap("%d%s%f")
 14  end
 15
 16  def test_f_returns_basename
 17    assert_equal "abc.rb", "abc.rb".pathmap("%f")
 18    assert_equal "abc.rb", "this/is/a/dir/abc.rb".pathmap("%f")
 19    assert_equal "abc.rb", "/this/is/a/dir/abc.rb".pathmap("%f")
 20  end
 21
 22  def test_n_returns_basename_without_extension
 23    assert_equal "abc", "abc.rb".pathmap("%n")
 24    assert_equal "abc", "abc".pathmap("%n")
 25    assert_equal "abc", "this/is/a/dir/abc.rb".pathmap("%n")
 26    assert_equal "abc", "/this/is/a/dir/abc.rb".pathmap("%n")
 27    assert_equal "abc", "/this/is/a/dir/abc".pathmap("%n")
 28  end
 29
 30  def test_d_returns_dirname
 31    assert_equal ".", "abc.rb".pathmap("%d")
 32    assert_equal "/", "/abc".pathmap("%d")
 33    assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%d")
 34    assert_equal "/this/is/a/dir", "/this/is/a/dir/abc.rb".pathmap("%d")
 35  end
 36
 37  def test_9d_returns_partial_dirname
 38    assert_equal "this/is", "this/is/a/dir/abc.rb".pathmap("%2d")
 39    assert_equal "this", "this/is/a/dir/abc.rb".pathmap("%1d")
 40    assert_equal ".", "this/is/a/dir/abc.rb".pathmap("%0d")
 41    assert_equal "dir", "this/is/a/dir/abc.rb".pathmap("%-1d")
 42    assert_equal "a/dir", "this/is/a/dir/abc.rb".pathmap("%-2d")
 43    assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%100d")
 44    assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%-100d")
 45  end
 46
 47  def test_x_returns_extension
 48    assert_equal "", "abc".pathmap("%x")
 49    assert_equal ".rb", "abc.rb".pathmap("%x")
 50    assert_equal ".rb", "abc.xyz.rb".pathmap("%x")
 51    assert_equal "", ".depends".pathmap("%x")
 52    assert_equal "", "dir/.depends".pathmap("%x")
 53  end
 54
 55  def test_X_returns_everything_but_extension
 56    assert_equal "abc", "abc".pathmap("%X")
 57    assert_equal "abc", "abc.rb".pathmap("%X")
 58    assert_equal "abc.xyz", "abc.xyz.rb".pathmap("%X")
 59    assert_equal "ab.xyz", "ab.xyz.rb".pathmap("%X")
 60    assert_equal "a.xyz", "a.xyz.rb".pathmap("%X")
 61    assert_equal "abc", "abc.rb".pathmap("%X")
 62    assert_equal "ab", "ab.rb".pathmap("%X")
 63    assert_equal "a", "a.rb".pathmap("%X")
 64    assert_equal ".depends", ".depends".pathmap("%X")
 65    assert_equal "a/dir/.depends", "a/dir/.depends".pathmap("%X")
 66    assert_equal "/.depends", "/.depends".pathmap("%X")
 67  end
 68
 69  def test_p_returns_entire_pathname
 70    assert_equal "abc.rb", "abc.rb".pathmap("%p")
 71    assert_equal "this/is/a/dir/abc.rb", "this/is/a/dir/abc.rb".pathmap("%p")
 72    assert_equal "/this/is/a/dir/abc.rb", "/this/is/a/dir/abc.rb".pathmap("%p")
 73  end
 74
 75  def test_dash_returns_empty_string
 76    assert_equal "", "abc.rb".pathmap("%-")
 77    assert_equal "abc.rb", "abc.rb".pathmap("%X%-%x")
 78  end
 79
 80  def test_percent_percent_returns_percent
 81    assert_equal "a%b", "".pathmap("a%%b")
 82  end
 83
 84  def test_undefined_percent_causes_error
 85    assert_raises(ArgumentError) {
 86      "dir/abc.rb".pathmap("%z")
 87    }
 88  end
 89
 90  def test_pattern_returns_substitutions
 91    assert_equal "bin/org/osb",
 92      "src/org/osb/Xyz.java".pathmap("%{src,bin}d")
 93  end
 94
 95  def test_pattern_can_use_backreferences
 96    assert_equal "dir/hi/is", "dir/this/is".pathmap("%{t(hi)s,\\1}p")
 97  end
 98
 99  def test_pattern_with_star_replacement_string_uses_block
100    assert_equal "src/ORG/osb",
101      "src/org/osb/Xyz.java".pathmap("%{/org,*}d") { |d| d.upcase }
102    assert_equal "Xyz.java",
103      "src/org/osb/Xyz.java".pathmap("%{.*,*}f") { |f| f.capitalize }
104  end
105
106  def test_pattern_with_no_replacement_nor_block_substitutes_empty_string
107    assert_equal "bc.rb", "abc.rb".pathmap("%{a}f")
108  end
109
110  def test_pattern_works_with_certain_valid_operators
111    assert_equal "dir/xbc.rb", "dir/abc.rb".pathmap("%{a,x}p")
112    assert_equal "d1r", "dir/abc.rb".pathmap("%{i,1}d")
113    assert_equal "xbc.rb", "dir/abc.rb".pathmap("%{a,x}f")
114    assert_equal ".Rb", "dir/abc.rb".pathmap("%{r,R}x")
115    assert_equal "xbc", "dir/abc.rb".pathmap("%{a,x}n")
116  end
117
118  def test_multiple_patterns
119    assert_equal "this/is/b/directory/abc.rb",
120      "this/is/a/dir/abc.rb".pathmap("%{a,b;dir,\\0ectory}p")
121  end
122
123  def test_partial_directory_selection_works_with_patterns
124    assert_equal "this/is/a/long",
125      "this/is/a/really/long/path/ok.rb".pathmap("%{/really/,/}5d")
126  end
127
128  def test_pattern_with_invalid_operator
129    ex = assert_raises(ArgumentError) do
130      "abc.xyz".pathmap("%{src,bin}z")
131    end
132    assert_match(/unknown.*pathmap.*spec.*z/i, ex.message)
133  end
134
135  def test_works_with_windows_separators
136    if File::ALT_SEPARATOR
137      assert_equal "abc", 'dir\abc.rb'.pathmap("%n")
138      assert_equal 'this\is\a\dir',
139        'this\is\a\dir\abc.rb'.pathmap("%d")
140    end
141  end
142
143  def test_complex_patterns
144    sep = "".pathmap("%s")
145    assert_equal "dir/abc.rb", "dir/abc.rb".pathmap("%d/%n%x")
146    assert_equal "./abc.rb", "abc.rb".pathmap("%d/%n%x")
147    assert_equal "Your file extension is '.rb'",
148      "dir/abc.rb".pathmap("Your file extension is '%x'")
149    assert_equal "bin/org/onstepback/proj/A.class",
150      "src/org/onstepback/proj/A.java".pathmap("%{src,bin}d/%n.class")
151    assert_equal "src_work/bin/org/onstepback/proj/A.class",
152      "src_work/src/org/onstepback/proj/A.java".pathmap('%{\bsrc\b,bin}X.class')
153    assert_equal ".depends.bak", ".depends".pathmap("%X.bak")
154    assert_equal "d#{sep}a/b/c#{sep}file.txt", "a/b/c/d/file.txt".pathmap("%-1d%s%3d%s%f")
155  end
156end
157