path_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package path
  5. import (
  6. "runtime"
  7. "testing"
  8. )
  9. type PathTest struct {
  10. path, result string
  11. }
  12. var cleantests = []PathTest{
  13. // Already clean
  14. {"", "."},
  15. {"abc", "abc"},
  16. {"abc/def", "abc/def"},
  17. {"a/b/c", "a/b/c"},
  18. {".", "."},
  19. {"..", ".."},
  20. {"../..", "../.."},
  21. {"../../abc", "../../abc"},
  22. {"/abc", "/abc"},
  23. {"/", "/"},
  24. // Remove trailing slash
  25. {"abc/", "abc"},
  26. {"abc/def/", "abc/def"},
  27. {"a/b/c/", "a/b/c"},
  28. {"./", "."},
  29. {"../", ".."},
  30. {"../../", "../.."},
  31. {"/abc/", "/abc"},
  32. // Remove doubled slash
  33. {"abc//def//ghi", "abc/def/ghi"},
  34. {"//abc", "/abc"},
  35. {"///abc", "/abc"},
  36. {"//abc//", "/abc"},
  37. {"abc//", "abc"},
  38. // Remove . elements
  39. {"abc/./def", "abc/def"},
  40. {"/./abc/def", "/abc/def"},
  41. {"abc/.", "abc"},
  42. // Remove .. elements
  43. {"abc/def/ghi/../jkl", "abc/def/jkl"},
  44. {"abc/def/../ghi/../jkl", "abc/jkl"},
  45. {"abc/def/..", "abc"},
  46. {"abc/def/../..", "."},
  47. {"/abc/def/../..", "/"},
  48. {"abc/def/../../..", ".."},
  49. {"/abc/def/../../..", "/"},
  50. {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
  51. // Combinations
  52. {"abc/./../def", "def"},
  53. {"abc//./../def", "def"},
  54. {"abc/../../././../def", "../../def"},
  55. }
  56. func TestClean(t *testing.T) {
  57. for _, test := range cleantests {
  58. if s := Clean(test.path); s != test.result {
  59. t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
  60. }
  61. if s := Clean(test.result); s != test.result {
  62. t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
  63. }
  64. }
  65. }
  66. func TestCleanMallocs(t *testing.T) {
  67. if testing.Short() {
  68. t.Skip("skipping malloc count in short mode")
  69. }
  70. if runtime.GOMAXPROCS(0) > 1 {
  71. t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
  72. return
  73. }
  74. t.Log("Skipping AllocsPerRun for gccgo")
  75. return
  76. for _, test := range cleantests {
  77. allocs := testing.AllocsPerRun(100, func() { Clean(test.result) })
  78. if allocs > 0 {
  79. t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
  80. }
  81. }
  82. }
  83. type SplitTest struct {
  84. path, dir, file string
  85. }
  86. var splittests = []SplitTest{
  87. {"a/b", "a/", "b"},
  88. {"a/b/", "a/b/", ""},
  89. {"a/", "a/", ""},
  90. {"a", "", "a"},
  91. {"/", "/", ""},
  92. }
  93. func TestSplit(t *testing.T) {
  94. for _, test := range splittests {
  95. if d, f := Split(test.path); d != test.dir || f != test.file {
  96. t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
  97. }
  98. }
  99. }
  100. type JoinTest struct {
  101. elem []string
  102. path string
  103. }
  104. var jointests = []JoinTest{
  105. // zero parameters
  106. {[]string{}, ""},
  107. // one parameter
  108. {[]string{""}, ""},
  109. {[]string{"a"}, "a"},
  110. // two parameters
  111. {[]string{"a", "b"}, "a/b"},
  112. {[]string{"a", ""}, "a"},
  113. {[]string{"", "b"}, "b"},
  114. {[]string{"/", "a"}, "/a"},
  115. {[]string{"/", ""}, "/"},
  116. {[]string{"a/", "b"}, "a/b"},
  117. {[]string{"a/", ""}, "a"},
  118. {[]string{"", ""}, ""},
  119. }
  120. // join takes a []string and passes it to Join.
  121. func join(elem []string, args ...string) string {
  122. args = elem
  123. return Join(args...)
  124. }
  125. func TestJoin(t *testing.T) {
  126. for _, test := range jointests {
  127. if p := join(test.elem); p != test.path {
  128. t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
  129. }
  130. }
  131. }
  132. type ExtTest struct {
  133. path, ext string
  134. }
  135. var exttests = []ExtTest{
  136. {"path.go", ".go"},
  137. {"path.pb.go", ".go"},
  138. {"a.dir/b", ""},
  139. {"a.dir/b.go", ".go"},
  140. {"a.dir/", ""},
  141. }
  142. func TestExt(t *testing.T) {
  143. for _, test := range exttests {
  144. if x := Ext(test.path); x != test.ext {
  145. t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
  146. }
  147. }
  148. }
  149. var basetests = []PathTest{
  150. // Already clean
  151. {"", "."},
  152. {".", "."},
  153. {"/.", "."},
  154. {"/", "/"},
  155. {"////", "/"},
  156. {"x/", "x"},
  157. {"abc", "abc"},
  158. {"abc/def", "def"},
  159. {"a/b/.x", ".x"},
  160. {"a/b/c.", "c."},
  161. {"a/b/c.x", "c.x"},
  162. }
  163. func TestBase(t *testing.T) {
  164. for _, test := range basetests {
  165. if s := Base(test.path); s != test.result {
  166. t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
  167. }
  168. }
  169. }
  170. var dirtests = []PathTest{
  171. {"", "."},
  172. {".", "."},
  173. {"/.", "/"},
  174. {"/", "/"},
  175. {"////", "/"},
  176. {"/foo", "/"},
  177. {"x/", "x"},
  178. {"abc", "."},
  179. {"abc/def", "abc"},
  180. {"abc////def", "abc"},
  181. {"a/b/.x", "a/b"},
  182. {"a/b/c.", "a/b"},
  183. {"a/b/c.x", "a/b"},
  184. }
  185. func TestDir(t *testing.T) {
  186. for _, test := range dirtests {
  187. if s := Dir(test.path); s != test.result {
  188. t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
  189. }
  190. }
  191. }
  192. type IsAbsTest struct {
  193. path string
  194. isAbs bool
  195. }
  196. var isAbsTests = []IsAbsTest{
  197. {"", false},
  198. {"/", true},
  199. {"/usr/bin/gcc", true},
  200. {"..", false},
  201. {"/a/../bb", true},
  202. {".", false},
  203. {"./", false},
  204. {"lala", false},
  205. }
  206. func TestIsAbs(t *testing.T) {
  207. for _, test := range isAbsTests {
  208. if r := IsAbs(test.path); r != test.isAbs {
  209. t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)
  210. }
  211. }
  212. }