123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package path
- import (
- "runtime"
- "testing"
- )
- type PathTest struct {
- path, result string
- }
- var cleantests = []PathTest{
-
- {"", "."},
- {"abc", "abc"},
- {"abc/def", "abc/def"},
- {"a/b/c", "a/b/c"},
- {".", "."},
- {"..", ".."},
- {"../..", "../.."},
- {"../../abc", "../../abc"},
- {"/abc", "/abc"},
- {"/", "/"},
-
- {"abc/", "abc"},
- {"abc/def/", "abc/def"},
- {"a/b/c/", "a/b/c"},
- {"./", "."},
- {"../", ".."},
- {"../../", "../.."},
- {"/abc/", "/abc"},
-
- {"abc//def//ghi", "abc/def/ghi"},
- {"//abc", "/abc"},
- {"///abc", "/abc"},
- {"//abc//", "/abc"},
- {"abc//", "abc"},
-
- {"abc/./def", "abc/def"},
- {"/./abc/def", "/abc/def"},
- {"abc/.", "abc"},
-
- {"abc/def/ghi/../jkl", "abc/def/jkl"},
- {"abc/def/../ghi/../jkl", "abc/jkl"},
- {"abc/def/..", "abc"},
- {"abc/def/../..", "."},
- {"/abc/def/../..", "/"},
- {"abc/def/../../..", ".."},
- {"/abc/def/../../..", "/"},
- {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
-
- {"abc/./../def", "def"},
- {"abc//./../def", "def"},
- {"abc/../../././../def", "../../def"},
- }
- func TestClean(t *testing.T) {
- for _, test := range cleantests {
- if s := Clean(test.path); s != test.result {
- t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
- }
- if s := Clean(test.result); s != test.result {
- t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
- }
- }
- }
- func TestCleanMallocs(t *testing.T) {
- if testing.Short() {
- t.Skip("skipping malloc count in short mode")
- }
- if runtime.GOMAXPROCS(0) > 1 {
- t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
- return
- }
- t.Log("Skipping AllocsPerRun for gccgo")
- return
- for _, test := range cleantests {
- allocs := testing.AllocsPerRun(100, func() { Clean(test.result) })
- if allocs > 0 {
- t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
- }
- }
- }
- type SplitTest struct {
- path, dir, file string
- }
- var splittests = []SplitTest{
- {"a/b", "a/", "b"},
- {"a/b/", "a/b/", ""},
- {"a/", "a/", ""},
- {"a", "", "a"},
- {"/", "/", ""},
- }
- func TestSplit(t *testing.T) {
- for _, test := range splittests {
- if d, f := Split(test.path); d != test.dir || f != test.file {
- t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
- }
- }
- }
- type JoinTest struct {
- elem []string
- path string
- }
- var jointests = []JoinTest{
-
- {[]string{}, ""},
-
- {[]string{""}, ""},
- {[]string{"a"}, "a"},
-
- {[]string{"a", "b"}, "a/b"},
- {[]string{"a", ""}, "a"},
- {[]string{"", "b"}, "b"},
- {[]string{"/", "a"}, "/a"},
- {[]string{"/", ""}, "/"},
- {[]string{"a/", "b"}, "a/b"},
- {[]string{"a/", ""}, "a"},
- {[]string{"", ""}, ""},
- }
- func join(elem []string, args ...string) string {
- args = elem
- return Join(args...)
- }
- func TestJoin(t *testing.T) {
- for _, test := range jointests {
- if p := join(test.elem); p != test.path {
- t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
- }
- }
- }
- type ExtTest struct {
- path, ext string
- }
- var exttests = []ExtTest{
- {"path.go", ".go"},
- {"path.pb.go", ".go"},
- {"a.dir/b", ""},
- {"a.dir/b.go", ".go"},
- {"a.dir/", ""},
- }
- func TestExt(t *testing.T) {
- for _, test := range exttests {
- if x := Ext(test.path); x != test.ext {
- t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
- }
- }
- }
- var basetests = []PathTest{
-
- {"", "."},
- {".", "."},
- {"/.", "."},
- {"/", "/"},
- {"////", "/"},
- {"x/", "x"},
- {"abc", "abc"},
- {"abc/def", "def"},
- {"a/b/.x", ".x"},
- {"a/b/c.", "c."},
- {"a/b/c.x", "c.x"},
- }
- func TestBase(t *testing.T) {
- for _, test := range basetests {
- if s := Base(test.path); s != test.result {
- t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
- }
- }
- }
- var dirtests = []PathTest{
- {"", "."},
- {".", "."},
- {"/.", "/"},
- {"/", "/"},
- {"////", "/"},
- {"/foo", "/"},
- {"x/", "x"},
- {"abc", "."},
- {"abc/def", "abc"},
- {"abc////def", "abc"},
- {"a/b/.x", "a/b"},
- {"a/b/c.", "a/b"},
- {"a/b/c.x", "a/b"},
- }
- func TestDir(t *testing.T) {
- for _, test := range dirtests {
- if s := Dir(test.path); s != test.result {
- t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
- }
- }
- }
- type IsAbsTest struct {
- path string
- isAbs bool
- }
- var isAbsTests = []IsAbsTest{
- {"", false},
- {"/", true},
- {"/usr/bin/gcc", true},
- {"..", false},
- {"/a/../bb", true},
- {".", false},
- {"./", false},
- {"lala", false},
- }
- func TestIsAbs(t *testing.T) {
- for _, test := range isAbsTests {
- if r := IsAbs(test.path); r != test.isAbs {
- t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)
- }
- }
- }
|