-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegenutil_test.go
93 lines (88 loc) · 2.02 KB
/
codegenutil_test.go
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
package codegenutil
import (
"testing"
)
func TestIdentifierRegexp(t *testing.T) {
tests := []struct {
id string
want bool
}{
{"helloWorld123", true},
{"_helloWorld123", true},
{"a", true},
{"_ó3", true},
{"b_b", true},
{"A_b", true},
{"A b", false},
{"A-b", false},
}
for _, tt := range tests {
if got, want := identifierRegexp.MatchString(tt.id), tt.want; got != want {
t.Errorf("%q got is regexp = %v, want = %v", tt.id, got, want)
}
}
}
func TestAssumedPackageName(t *testing.T) {
tests := []struct {
importPath string
want *Package
}{
{
importPath: "go.lang/x/tools",
want: &Package{importPath: "go.lang/x/tools", name: "tools"},
},
{
importPath: "go.lang/x/tools/v2",
want: &Package{importPath: "go.lang/x/tools/v2", name: "tools"},
},
{
importPath: "go.lang/x/go-tools/v2",
want: &Package{importPath: "go.lang/x/go-tools/v2", name: "tools"},
},
}
for _, tt := range tests {
t.Run(tt.importPath, func(t *testing.T) {
if got := AssumedPackageName(tt.importPath); !pkgEqual(got, tt.want) {
t.Errorf("AssumedPackageName() = %q, want %v", got, tt.want)
}
})
}
}
func pkgEqual(a, b *Package) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return a.importPath == b.importPath && a.name == b.name
}
func TestSymbol_GoCode(t *testing.T) {
type example struct {
name string
sym *Symbol
imports *FileImports
want string
}
tests := []example{
{
name: "simple1",
sym: AssumedPackageName("abc/xyz").Symbol("Foo"),
imports: NewFileImports(AssumedPackageName("bar")),
want: "xyz.Foo",
},
{
name: "symbol in package of file",
sym: AssumedPackageName("abc/xyz").Symbol("Foo"),
imports: NewFileImports(AssumedPackageName("abc/xyz")),
want: "Foo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.sym.GoCode(tt.imports); got != tt.want {
t.Errorf("%v.GoCode() = %q, want %q", tt.sym, got, tt.want)
}
})
}
}