PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/externals/ruby1.9/rake/test_pathmap.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 207 lines | 178 code | 28 blank | 1 comment | 3 complexity | 0b6bc0b9425c1a9fbb225b1273f2b481 MD5 | raw file
Possible License(s): GPL-3.0, JSON
  1. require 'test/unit'
  2. require 'rake'
  3. # ====================================================================
  4. class Rake::TestPathMap < Test::Unit::TestCase
  5. def test_returns_self_with_no_args
  6. assert_equal "abc.rb", "abc.rb".pathmap
  7. end
  8. def test_s_returns_file_separator
  9. sep = File::ALT_SEPARATOR || File::SEPARATOR
  10. assert_equal sep, "abc.rb".pathmap("%s")
  11. assert_equal sep, "".pathmap("%s")
  12. assert_equal "a#{sep}b", "a/b".pathmap("%d%s%f")
  13. end
  14. def test_f_returns_basename
  15. assert_equal "abc.rb", "abc.rb".pathmap("%f")
  16. assert_equal "abc.rb", "this/is/a/dir/abc.rb".pathmap("%f")
  17. assert_equal "abc.rb", "/this/is/a/dir/abc.rb".pathmap("%f")
  18. end
  19. def test_n_returns_basename_without_extension
  20. assert_equal "abc", "abc.rb".pathmap("%n")
  21. assert_equal "abc", "abc".pathmap("%n")
  22. assert_equal "abc", "this/is/a/dir/abc.rb".pathmap("%n")
  23. assert_equal "abc", "/this/is/a/dir/abc.rb".pathmap("%n")
  24. assert_equal "abc", "/this/is/a/dir/abc".pathmap("%n")
  25. end
  26. def test_d_returns_dirname
  27. assert_equal ".", "abc.rb".pathmap("%d")
  28. assert_equal "/", "/abc".pathmap("%d")
  29. assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%d")
  30. assert_equal "/this/is/a/dir", "/this/is/a/dir/abc.rb".pathmap("%d")
  31. end
  32. def test_9d_returns_partial_dirname
  33. assert_equal "this/is", "this/is/a/dir/abc.rb".pathmap("%2d")
  34. assert_equal "this", "this/is/a/dir/abc.rb".pathmap("%1d")
  35. assert_equal ".", "this/is/a/dir/abc.rb".pathmap("%0d")
  36. assert_equal "dir", "this/is/a/dir/abc.rb".pathmap("%-1d")
  37. assert_equal "a/dir", "this/is/a/dir/abc.rb".pathmap("%-2d")
  38. assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%100d")
  39. assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%-100d")
  40. end
  41. def test_x_returns_extension
  42. assert_equal "", "abc".pathmap("%x")
  43. assert_equal ".rb", "abc.rb".pathmap("%x")
  44. assert_equal ".rb", "abc.xyz.rb".pathmap("%x")
  45. assert_equal "", ".depends".pathmap("%x")
  46. assert_equal "", "dir/.depends".pathmap("%x")
  47. end
  48. def test_X_returns_everything_but_extension
  49. assert_equal "abc", "abc".pathmap("%X")
  50. assert_equal "abc", "abc.rb".pathmap("%X")
  51. assert_equal "abc.xyz", "abc.xyz.rb".pathmap("%X")
  52. assert_equal "ab.xyz", "ab.xyz.rb".pathmap("%X")
  53. assert_equal "a.xyz", "a.xyz.rb".pathmap("%X")
  54. assert_equal "abc", "abc.rb".pathmap("%X")
  55. assert_equal "ab", "ab.rb".pathmap("%X")
  56. assert_equal "a", "a.rb".pathmap("%X")
  57. assert_equal ".depends", ".depends".pathmap("%X")
  58. assert_equal "a/dir/.depends", "a/dir/.depends".pathmap("%X")
  59. assert_equal "/.depends", "/.depends".pathmap("%X")
  60. end
  61. def test_p_returns_entire_pathname
  62. assert_equal "abc.rb", "abc.rb".pathmap("%p")
  63. assert_equal "this/is/a/dir/abc.rb", "this/is/a/dir/abc.rb".pathmap("%p")
  64. assert_equal "/this/is/a/dir/abc.rb", "/this/is/a/dir/abc.rb".pathmap("%p")
  65. end
  66. def test_dash_returns_empty_string
  67. assert_equal "", "abc.rb".pathmap("%-")
  68. assert_equal "abc.rb", "abc.rb".pathmap("%X%-%x")
  69. end
  70. def test_percent_percent_returns_percent
  71. assert_equal "a%b", "".pathmap("a%%b")
  72. end
  73. def test_undefined_percent_causes_error
  74. ex = assert_raise(ArgumentError) {
  75. "dir/abc.rb".pathmap("%z")
  76. }
  77. end
  78. def test_pattern_returns_substitutions
  79. assert_equal "bin/org/osb",
  80. "src/org/osb/Xyz.java".pathmap("%{src,bin}d")
  81. end
  82. def test_pattern_can_use_backreferences
  83. assert_equal "dir/hi/is", "dir/this/is".pathmap("%{t(hi)s,\\1}p")
  84. end
  85. def test_pattern_with_star_replacement_string_uses_block
  86. assert_equal "src/ORG/osb",
  87. "src/org/osb/Xyz.java".pathmap("%{/org,*}d") { |d| d.upcase }
  88. assert_equal "Xyz.java",
  89. "src/org/osb/Xyz.java".pathmap("%{.*,*}f") { |f| f.capitalize }
  90. end
  91. def test_pattern_with_no_replacement_nor_block_substitutes_empty_string
  92. assert_equal "bc.rb", "abc.rb".pathmap("%{a}f")
  93. end
  94. def test_pattern_works_with_certain_valid_operators
  95. assert_equal "dir/xbc.rb", "dir/abc.rb".pathmap("%{a,x}p")
  96. assert_equal "d1r", "dir/abc.rb".pathmap("%{i,1}d")
  97. assert_equal "xbc.rb", "dir/abc.rb".pathmap("%{a,x}f")
  98. assert_equal ".Rb", "dir/abc.rb".pathmap("%{r,R}x")
  99. assert_equal "xbc", "dir/abc.rb".pathmap("%{a,x}n")
  100. end
  101. def test_multiple_patterns
  102. assert_equal "this/is/b/directory/abc.rb",
  103. "this/is/a/dir/abc.rb".pathmap("%{a,b;dir,\\0ectory}p")
  104. end
  105. def test_partial_directory_selection_works_with_patterns
  106. assert_equal "this/is/a/long",
  107. "this/is/a/really/long/path/ok.rb".pathmap("%{/really/,/}5d")
  108. end
  109. def test_pattern_with_invalid_operator
  110. ex = assert_raise(ArgumentError) do
  111. "abc.xyz".pathmap("%{src,bin}z")
  112. end
  113. assert_match(/unknown.*pathmap.*spec.*z/i, ex.message)
  114. end
  115. def test_works_with_windows_separators
  116. if File::ALT_SEPARATOR
  117. assert_equal "abc", 'dir\abc.rb'.pathmap("%n")
  118. assert_equal 'this\is\a\dir',
  119. 'this\is\a\dir\abc.rb'.pathmap("%d")
  120. end
  121. end
  122. def test_complex_patterns
  123. sep = "".pathmap("%s")
  124. assert_equal "dir/abc.rb", "dir/abc.rb".pathmap("%d/%n%x")
  125. assert_equal "./abc.rb", "abc.rb".pathmap("%d/%n%x")
  126. assert_equal "Your file extension is '.rb'",
  127. "dir/abc.rb".pathmap("Your file extension is '%x'")
  128. assert_equal "bin/org/onstepback/proj/A.class",
  129. "src/org/onstepback/proj/A.java".pathmap("%{src,bin}d/%n.class")
  130. assert_equal "src_work/bin/org/onstepback/proj/A.class",
  131. "src_work/src/org/onstepback/proj/A.java".pathmap('%{\bsrc\b,bin}X.class')
  132. assert_equal ".depends.bak", ".depends".pathmap("%X.bak")
  133. assert_equal "d#{sep}a/b/c#{sep}file.txt", "a/b/c/d/file.txt".pathmap("%-1d%s%3d%s%f")
  134. end
  135. end
  136. class Rake::TestPathMapExplode < Test::Unit::TestCase
  137. def setup
  138. String.class_eval { public :pathmap_explode }
  139. end
  140. def teardown
  141. String.class_eval { protected :pathmap_explode }
  142. end
  143. def test_explode
  144. assert_equal ['a'], 'a'.pathmap_explode
  145. assert_equal ['a', 'b'], 'a/b'.pathmap_explode
  146. assert_equal ['a', 'b', 'c'], 'a/b/c'.pathmap_explode
  147. assert_equal ['/', 'a'], '/a'.pathmap_explode
  148. assert_equal ['/', 'a', 'b'], '/a/b'.pathmap_explode
  149. assert_equal ['/', 'a', 'b', 'c'], '/a/b/c'.pathmap_explode
  150. if File::ALT_SEPARATOR
  151. assert_equal ['c:.', 'a'], 'c:a'.pathmap_explode
  152. assert_equal ['c:.', 'a', 'b'], 'c:a/b'.pathmap_explode
  153. assert_equal ['c:.', 'a', 'b', 'c'], 'c:a/b/c'.pathmap_explode
  154. assert_equal ['c:/', 'a'], 'c:/a'.pathmap_explode
  155. assert_equal ['c:/', 'a', 'b'], 'c:/a/b'.pathmap_explode
  156. assert_equal ['c:/', 'a', 'b', 'c'], 'c:/a/b/c'.pathmap_explode
  157. end
  158. end
  159. end
  160. class Rake::TestPathMapPartial < Test::Unit::TestCase
  161. def test_pathmap_partial
  162. @path = "1/2/file"
  163. def @path.call(n)
  164. pathmap_partial(n)
  165. end
  166. assert_equal("1", @path.call(1))
  167. assert_equal("1/2", @path.call(2))
  168. assert_equal("1/2", @path.call(3))
  169. assert_equal(".", @path.call(0))
  170. assert_equal("2", @path.call(-1))
  171. assert_equal("1/2", @path.call(-2))
  172. assert_equal("1/2", @path.call(-3))
  173. end
  174. end
  175. class Rake::TestFileListPathMap < Test::Unit::TestCase
  176. def test_file_list_supports_pathmap
  177. assert_equal ['a', 'b'], FileList['dir/a.rb', 'dir/b.rb'].pathmap("%n")
  178. end
  179. end