aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/objabi/path_test.go
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2023-09-15 13:49:56 -0400
committerMichael Pratt <mpratt@google.com>2023-10-13 13:56:25 +0000
commit696fb5ead03a4b84e11f2d195bc91838fdd029b2 (patch)
treee9755fdf7ed3c5ccdeb5823ce2d2bcd523c3bbad /src/cmd/internal/objabi/path_test.go
parentd1ef967306b401474aafb2a0d351e972bdfeb54c (diff)
downloadgo-696fb5ead03a4b84e11f2d195bc91838fdd029b2.tar.xz
cmd/internal/objabi: add inverse of PathToPrefix
Add PrefixToPath, which can be used to convert a package path in a symbol name back to the original package path. For #61577. Change-Id: Ifbe8c852a7f41ff9b81ad48b92a26a0e1b046777 Reviewed-on: https://go-review.googlesource.com/c/go/+/529557 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd/internal/objabi/path_test.go')
-rw-r--r--src/cmd/internal/objabi/path_test.go43
1 files changed, 36 insertions, 7 deletions
diff --git a/src/cmd/internal/objabi/path_test.go b/src/cmd/internal/objabi/path_test.go
index 78b94a3266..934db3dfa0 100644
--- a/src/cmd/internal/objabi/path_test.go
+++ b/src/cmd/internal/objabi/path_test.go
@@ -11,11 +11,11 @@ import (
"testing"
)
-func TestPathToPrefix(t *testing.T) {
- tests := []struct {
- Path string
- Expected string
- }{{"foo/bar/v1", "foo/bar/v1"},
+var escapeTests = []struct {
+ Path string
+ Escaped string
+ }{
+ {"foo/bar/v1", "foo/bar/v1"},
{"foo/bar/v.1", "foo/bar/v%2e1"},
{"f.o.o/b.a.r/v1", "f.o.o/b.a.r/v1"},
{"f.o.o/b.a.r/v.1", "f.o.o/b.a.r/v%2e1"},
@@ -30,9 +30,38 @@ func TestPathToPrefix(t *testing.T) {
{"%foo%bar", "%25foo%25bar"},
{"\x01\x00\x7F☺", "%01%00%7f%e2%98%ba"},
}
+
+func TestPathToPrefix(t *testing.T) {
+ for _, tc := range escapeTests {
+ if got := PathToPrefix(tc.Path); got != tc.Escaped {
+ t.Errorf("expected PathToPrefix(%s) = %s, got %s", tc.Path, tc.Escaped, got)
+ }
+ }
+}
+
+func TestPrefixToPath(t *testing.T) {
+ for _, tc := range escapeTests {
+ got, err := PrefixToPath(tc.Escaped)
+ if err != nil {
+ t.Errorf("expected PrefixToPath(%s) err = nil, got %v", tc.Escaped, err)
+ }
+ if got != tc.Path {
+ t.Errorf("expected PrefixToPath(%s) = %s, got %s", tc.Escaped, tc.Path, got)
+ }
+ }
+}
+
+func TestPrefixToPathError(t *testing.T) {
+ tests := []string{
+ "foo%",
+ "foo%1",
+ "foo%%12",
+ "foo%1g",
+ }
for _, tc := range tests {
- if got := PathToPrefix(tc.Path); got != tc.Expected {
- t.Errorf("expected PathToPrefix(%s) = %s, got %s", tc.Path, tc.Expected, got)
+ _, err := PrefixToPath(tc)
+ if err == nil {
+ t.Errorf("expected PrefixToPath(%s) err != nil, got nil", tc)
}
}
}