-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrcutil_test.go
125 lines (114 loc) · 2.93 KB
/
srcutil_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package srcutil
import (
"log"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
)
func tmust(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
func teq(t *testing.T, exp, got interface{}) {
if !reflect.DeepEqual(exp, got) {
t.Errorf("DeepEqual failed:\n exp: %#v\n got: %#v", exp, got)
}
}
type testPackage struct {
Name string
Path string
ImportPath string
Names []string
PkgNames []string
PkgTests []string
Types map[string]bool
Funcs map[string]bool
Vars map[string]bool
Consts map[string]bool
DocFuncs map[string]bool
DocVars map[string]bool
DocMethods map[string]bool
}
func newTestPackage() *testPackage {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
tPkg := &testPackage{
Name: "tpkg",
Path: filepath.Join(cwd, "testdata"),
ImportPath: "github.com/cstockton/go-srcutil/testdata",
Names: []string{
"tpkg.go", "tpkg_private.go", "tpkg_test.go"},
PkgNames: []string{
"tpkg.go", "tpkg_private.go"},
PkgTests: []string{
"tpkg_example_test.go", "tpkg_test.go"},
Types: map[string]bool{
"PublicStruct": true, "PublicStructUnexported": true,
"privateStruct": true, "privateStructExported": true},
Funcs: map[string]bool{
"funcOne": true, "funcTwo": true, "funcThree": true, "StringFunc": true,
"NiladicFunc": true, "NiladicVoidFunc": true},
Vars: map[string]bool{
"variableOne": true, "variableTwo": true, "variableThree": true,
"VariableOne": true, "VariableTwo": true, "VariableThree": true},
Consts: map[string]bool{
"constantOne": true, "constantTwo": true, "constantThree": true,
"ConstantOne": true, "ConstantTwo": true, "ConstantThree": true},
}
return tPkg
}
func (p *testPackage) paths(s []string) []string {
paths := make([]string, len(s))
for i, name := range s {
paths[i] = filepath.Join(p.Path, name)
}
sort.Strings(paths)
return paths
}
var tPkg = newTestPackage()
func TestContext(t *testing.T) {
cwd, err := os.Getwd()
tmust(t, err)
t.Run("Creation", func(t *testing.T) {
t.Run("FromWorkDir", func(t *testing.T) {
ctx := FromWorkDir()
teq(t, cwd, ctx.SourceDir)
})
t.Run("FromStandard", func(t *testing.T) {
ctx := FromStandard()
teq(t, ``, ctx.GOPATH)
teq(t, ctx.GOROOT+"/src", ctx.SourceDir)
})
t.Run("FromDir", func(t *testing.T) {
ctx := FromDir(`.`)
teq(t, `.`, ctx.SourceDir)
})
})
}
func TestImport(t *testing.T) {
ctx := FromWorkDir()
t.Run("FromGOPATH", func(t *testing.T) {
pkg, err := ctx.Import(tPkg.ImportPath)
tmust(t, err)
teq(t, tPkg.Name, pkg.Name)
})
t.Run("FromStandardLibrary", func(t *testing.T) {
pkg, err := ctx.Import("reflect")
tmust(t, err)
teq(t, "reflect", pkg.Name)
})
t.Run("Failure", func(t *testing.T) {
pkg, err := ctx.Import("thislibrarydoesntexist")
if err == nil {
t.Errorf("expected error for non-existent import")
}
if pkg != nil {
t.Errorf("expected nil pkg for non-existent import")
}
})
}