Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
path_test.go249 linesDownload Raw Back to path
1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5package path_test6 7import (8	"path"9	. "path"10	"runtime"11	"testing"12)13 14type PathTest struct {15	path, result string16}17 18var cleantests = []PathTest{19	// Already clean20	{"", "."},21	{"abc", "abc"},22	{"abc/def", "abc/def"},23	{"a/b/c", "a/b/c"},24	{".", "."},25	{"..", ".."},26	{"../..", "../.."},27	{"../../abc", "../../abc"},28	{"/abc", "/abc"},29	{"/", "/"},30 31	// Remove trailing slash32	{"abc/", "abc"},33	{"abc/def/", "abc/def"},34	{"a/b/c/", "a/b/c"},35	{"./", "."},36	{"../", ".."},37	{"../../", "../.."},38	{"/abc/", "/abc"},39 40	// Remove doubled slash41	{"abc//def//ghi", "abc/def/ghi"},42	{"//abc", "/abc"},43	{"///abc", "/abc"},44	{"//abc//", "/abc"},45	{"abc//", "abc"},46 47	// Remove . elements48	{"abc/./def", "abc/def"},49	{"/./abc/def", "/abc/def"},50	{"abc/.", "abc"},51 52	// Remove .. elements53	{"abc/def/ghi/../jkl", "abc/def/jkl"},54	{"abc/def/../ghi/../jkl", "abc/jkl"},55	{"abc/def/..", "abc"},56	{"abc/def/../..", "."},57	{"/abc/def/../..", "/"},58	{"abc/def/../../..", ".."},59	{"/abc/def/../../..", "/"},60	{"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},61 62	// Combinations63	{"abc/./../def", "def"},64	{"abc//./../def", "def"},65	{"abc/../../././../def", "../../def"},66}67 68func TestClean(t *testing.T) {69	for _, test := range cleantests {70		if s := Clean(test.path); s != test.result {71			t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)72		}73		if s := Clean(test.result); s != test.result {74			t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)75		}76	}77}78 79func TestCleanMallocs(t *testing.T) {80	if testing.Short() {81		t.Skip("skipping malloc count in short mode")82	}83	if runtime.GOMAXPROCS(0) > 1 {84		t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")85		return86	}87 88	for _, test := range cleantests {89		allocs := testing.AllocsPerRun(100, func() { Clean(test.result) })90		if allocs > 0 {91			t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)92		}93	}94}95 96type SplitTest struct {97	path, dir, file string98}99 100var splittests = []SplitTest{101	{"a/b", "a/", "b"},102	{"a/b/", "a/b/", ""},103	{"a/", "a/", ""},104	{"a", "", "a"},105	{"/", "/", ""},106}107 108func TestSplit(t *testing.T) {109	for _, test := range splittests {110		if d, f := Split(test.path); d != test.dir || f != test.file {111			t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)112		}113	}114}115 116type JoinTest struct {117	elem []string118	path string119}120 121var jointests = []JoinTest{122	// zero parameters123	{[]string{}, ""},124 125	// one parameter126	{[]string{""}, ""},127	{[]string{"a"}, "a"},128 129	// two parameters130	{[]string{"a", "b"}, "a/b"},131	{[]string{"a", ""}, "a"},132	{[]string{"", "b"}, "b"},133	{[]string{"/", "a"}, "/a"},134	{[]string{"/", ""}, "/"},135	{[]string{"a/", "b"}, "a/b"},136	{[]string{"a/", ""}, "a"},137	{[]string{"", ""}, ""},138}139 140func TestJoin(t *testing.T) {141	for _, test := range jointests {142		if p := Join(test.elem...); p != test.path {143			t.Errorf("Join(%q) = %q, want %q", test.elem, p, test.path)144		}145	}146}147 148type ExtTest struct {149	path, ext string150}151 152var exttests = []ExtTest{153	{"path.go", ".go"},154	{"path.pb.go", ".go"},155	{"a.dir/b", ""},156	{"a.dir/b.go", ".go"},157	{"a.dir/", ""},158}159 160func TestExt(t *testing.T) {161	for _, test := range exttests {162		if x := Ext(test.path); x != test.ext {163			t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)164		}165	}166}167 168var basetests = []PathTest{169	// Already clean170	{"", "."},171	{".", "."},172	{"/.", "."},173	{"/", "/"},174	{"////", "/"},175	{"x/", "x"},176	{"abc", "abc"},177	{"abc/def", "def"},178	{"a/b/.x", ".x"},179	{"a/b/c.", "c."},180	{"a/b/c.x", "c.x"},181}182 183func TestBase(t *testing.T) {184	for _, test := range basetests {185		if s := Base(test.path); s != test.result {186			t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)187		}188	}189}190 191var dirtests = []PathTest{192	{"", "."},193	{".", "."},194	{"/.", "/"},195	{"/", "/"},196	{"////", "/"},197	{"/foo", "/"},198	{"x/", "x"},199	{"abc", "."},200	{"abc/def", "abc"},201	{"abc////def", "abc"},202	{"a/b/.x", "a/b"},203	{"a/b/c.", "a/b"},204	{"a/b/c.x", "a/b"},205}206 207func TestDir(t *testing.T) {208	for _, test := range dirtests {209		if s := Dir(test.path); s != test.result {210			t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)211		}212	}213}214 215type IsAbsTest struct {216	path  string217	isAbs bool218}219 220var isAbsTests = []IsAbsTest{221	{"", false},222	{"/", true},223	{"/usr/bin/gcc", true},224	{"..", false},225	{"/a/../bb", true},226	{".", false},227	{"./", false},228	{"lala", false},229}230 231func TestIsAbs(t *testing.T) {232	for _, test := range isAbsTests {233		if r := IsAbs(test.path); r != test.isAbs {234			t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)235		}236	}237}238 239func BenchmarkJoin(b *testing.B) {240	b.ReportAllocs()241	parts := []string{"one", "two", "three", "four"}242	s := parts[0]243	for b.Loop() {244		parts[0] = s245		s = path.Join(parts...)246		s = s[:3]247	}248}249 
codekingpro/portable-devtools · Team Ai