1
2
3
4
5 package pkgpath
6
7 import (
8 "os"
9 "testing"
10 )
11
12 const testEnvName = "GO_PKGPATH_TEST_COMPILER"
13
14
15
16
17 func init() {
18 switch os.Getenv(testEnvName) {
19 case "":
20 return
21 case "v1":
22 os.Stdout.WriteString(`.string "go.l__ufer.Run"`)
23 os.Exit(0)
24 case "v2":
25 os.Stdout.WriteString(`.string "go.l..u00e4ufer.Run"`)
26 os.Exit(0)
27 case "v3":
28 os.Stdout.WriteString(`.string "go_0l_u00e4ufer.Run"`)
29 os.Exit(0)
30 case "error":
31 os.Stdout.WriteString(`unknown string`)
32 os.Exit(0)
33 }
34 }
35
36 func TestToSymbolFunc(t *testing.T) {
37 const input = "pä世🜃"
38 tests := []struct {
39 env string
40 fail bool
41 mangled string
42 }{
43 {
44 env: "v1",
45 mangled: "p___",
46 },
47 {
48 env: "v2",
49 mangled: "p..u00e4..u4e16..U0001f703",
50 },
51 {
52 env: "v3",
53 mangled: "p_u00e4_u4e16_U0001f703",
54 },
55 {
56 env: "error",
57 fail: true,
58 },
59 }
60
61 cmd := os.Args[0]
62 tmpdir := t.TempDir()
63
64 defer os.Unsetenv(testEnvName)
65
66 for _, test := range tests {
67 t.Run(test.env, func(t *testing.T) {
68 os.Setenv(testEnvName, test.env)
69
70 fn, err := ToSymbolFunc(cmd, tmpdir)
71 if err != nil {
72 if !test.fail {
73 t.Errorf("ToSymbolFunc(%q, %q): unexpected error %v", cmd, tmpdir, err)
74 }
75 } else if test.fail {
76 t.Errorf("ToSymbolFunc(%q, %q) succeeded but expected to fail", cmd, tmpdir)
77 } else if got, want := fn(input), test.mangled; got != want {
78 t.Errorf("ToSymbolFunc(%q, %q)(%q) = %q, want %q", cmd, tmpdir, input, got, want)
79 }
80 })
81 }
82 }
83
84 var symbolTests = []struct {
85 input, v1, v2, v3 string
86 }{
87 {
88 "",
89 "",
90 "",
91 "",
92 },
93 {
94 "bytes",
95 "bytes",
96 "bytes",
97 "bytes",
98 },
99 {
100 "net/http",
101 "net_http",
102 "net..z2fhttp",
103 "net_1http",
104 },
105 {
106 "golang.org/x/net/http",
107 "golang_org_x_net_http",
108 "golang.x2eorg..z2fx..z2fnet..z2fhttp",
109 "golang_0org_1x_1net_1http",
110 },
111 {
112 "pä世.🜃",
113 "p____",
114 "p..u00e4..u4e16.x2e..U0001f703",
115 "p_u00e4_u4e16_0_U0001f703",
116 },
117 }
118
119 func TestV1(t *testing.T) {
120 for _, test := range symbolTests {
121 if got, want := toSymbolV1(test.input), test.v1; got != want {
122 t.Errorf("toSymbolV1(%q) = %q, want %q", test.input, got, want)
123 }
124 }
125 }
126
127 func TestV2(t *testing.T) {
128 for _, test := range symbolTests {
129 if got, want := toSymbolV2(test.input), test.v2; got != want {
130 t.Errorf("toSymbolV2(%q) = %q, want %q", test.input, got, want)
131 }
132 }
133 }
134
135 func TestV3(t *testing.T) {
136 for _, test := range symbolTests {
137 if got, want := toSymbolV3(test.input), test.v3; got != want {
138 t.Errorf("toSymbolV3(%q) = %q, want %q", test.input, got, want)
139 }
140 }
141 }
142
View as plain text