summaryrefslogtreecommitdiffstats
path: root/src/cmd/go/internal/str/str_test.go
blob: 158fe65dc16edfc7e8f9136fcdb9b279bcc1ab74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package str

import (
	"os"
	"runtime"
	"testing"
)

var foldDupTests = []struct {
	list   []string
	f1, f2 string
}{
	{StringList("math/rand", "math/big"), "", ""},
	{StringList("math", "strings"), "", ""},
	{StringList("strings"), "", ""},
	{StringList("strings", "strings"), "strings", "strings"},
	{StringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"},
}

func TestFoldDup(t *testing.T) {
	for _, tt := range foldDupTests {
		f1, f2 := FoldDup(tt.list)
		if f1 != tt.f1 || f2 != tt.f2 {
			t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2)
		}
	}
}

type trimFilePathPrefixTest struct {
	s, prefix, want string
}

func TestTrimFilePathPrefixSlash(t *testing.T) {
	if os.PathSeparator != '/' {
		t.Skipf("test requires slash-separated file paths")
	}
	tests := []trimFilePathPrefixTest{
		{"/foo", "", "foo"},
		{"/foo", "/", "foo"},
		{"/foo", "/foo", ""},
		{"/foo/bar", "/foo", "bar"},
		{"/foo/bar", "/foo/", "bar"},
		// if prefix is not s's prefix, return s
		{"/foo", "/bar", "/foo"},
		{"/foo", "/foo/bar", "/foo"},
	}

	for _, tt := range tests {
		if got := TrimFilePathPrefix(tt.s, tt.prefix); got != tt.want {
			t.Errorf("TrimFilePathPrefix(%q, %q) = %q, want %q", tt.s, tt.prefix, got, tt.want)
		}
	}
}

func TestTrimFilePathPrefixWindows(t *testing.T) {
	if runtime.GOOS != "windows" {
		t.Skipf("test requires Windows file paths")
	}
	tests := []trimFilePathPrefixTest{
		{`C:\foo`, `C:`, `foo`},
		{`C:\foo`, `C:\`, `foo`},
		{`C:\foo`, `C:\foo`, ``},
		{`C:\foo\bar`, `C:\foo`, `bar`},
		{`C:\foo\bar`, `C:\foo\`, `bar`},
		// if prefix is not s's prefix, return s
		{`C:\foo`, `C:\bar`, `C:\foo`},
		{`C:\foo`, `C:\foo\bar`, `C:\foo`},
		// if volumes are different, return s
		{`C:\foo`, ``, `C:\foo`},
		{`C:\foo`, `\foo`, `C:\foo`},
		{`C:\foo`, `D:\foo`, `C:\foo`},

		//UNC path
		{`\\host\share\foo`, `\\host\share`, `foo`},
		{`\\host\share\foo`, `\\host\share\`, `foo`},
		{`\\host\share\foo`, `\\host\share\foo`, ``},
		{`\\host\share\foo\bar`, `\\host\share\foo`, `bar`},
		{`\\host\share\foo\bar`, `\\host\share\foo\`, `bar`},
		// if prefix is not s's prefix, return s
		{`\\host\share\foo`, `\\host\share\bar`, `\\host\share\foo`},
		{`\\host\share\foo`, `\\host\share\foo\bar`, `\\host\share\foo`},
		// if either host or share name is different, return s
		{`\\host\share\foo`, ``, `\\host\share\foo`},
		{`\\host\share\foo`, `\foo`, `\\host\share\foo`},
		{`\\host\share\foo`, `\\host\other\`, `\\host\share\foo`},
		{`\\host\share\foo`, `\\other\share\`, `\\host\share\foo`},
		{`\\host\share\foo`, `\\host\`, `\\host\share\foo`},
		{`\\host\share\foo`, `\share\`, `\\host\share\foo`},
	}

	for _, tt := range tests {
		if got := TrimFilePathPrefix(tt.s, tt.prefix); got != tt.want {
			t.Errorf("TrimFilePathPrefix(%q, %q) = %q, want %q", tt.s, tt.prefix, got, tt.want)
		}
	}
}