/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

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