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

/vendor/bundle/ruby/1.8/gems/rake-0.8.7/test/test_pathmap.rb

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