PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/jruby-1.1.6RC1/lib/ruby/gems/1.8/gems/rake-0.8.3/test/test_pathmap.rb

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