-
Notifications
You must be signed in to change notification settings - Fork 13
/
deps_test.go
143 lines (121 loc) · 4.45 KB
/
deps_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"golang.org/x/tools/go/loader"
"testing"
)
func TestExportedFuncs(t *testing.T) {
var result *Result
var src string
src = "test/exported.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 2)
checkSelector(src, t, result, "xsample.var.Sample", 1, 0, 0, 0, 0)
checkSelector(src, t, result, "xsample.func.SampleFunc", 1, 6, 14, 0, 2)
src = "test/exported2.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 3)
checkSelector(src, t, result, "xsample.func.SampleFunc", 1, 6, 14, 0, 2)
checkSelector(src, t, result, "xsample.(Foo).method.Bar", 1, 3, 3, 0, 0)
checkSelector(src, t, result, "xsample.type.Foo", 1, 0, 0, 0, 0)
src = "test/pkg_renamed.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 3)
checkSelector(src, t, result, "xsample.func.SampleFunc", 1, 6, 14, 0, 2)
checkSelector(src, t, result, "xsample.(Foo).method.Bar", 1, 3, 3, 0, 0)
checkSelector(src, t, result, "xsample.type.Foo", 1, 0, 0, 0, 0)
src = "test/pkg_dot.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 3)
checkSelector(src, t, result, "xsample.func.SampleFunc", 1, 6, 14, 0, 2)
checkSelector(src, t, result, "xsample.(Foo).method.Bar", 1, 3, 3, 0, 0)
checkSelector(src, t, result, "xsample.type.Foo", 1, 0, 0, 0, 0)
}
func TestRecursion(t *testing.T) {
var result *Result
var src string
src = "test/recursion.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 2)
checkSelector(src, t, result, "bar.func.Bar", 1, 4, 4, 0, 0)
checkSelector(src, t, result, "foo.func.Foo", 1, 4, 8, 1, 0)
}
func TestConsts(t *testing.T) {
var result *Result
var src string
src = "test/const.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 1)
checkSelector(src, t, result, "foo.const.FooConst", 1, 0, 0, 0, 0)
}
func TestVars(t *testing.T) {
var result *Result
var src string
src = "test/var.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 1)
checkSelector(src, t, result, "foo.var.FooVar", 1, 0, 0, 0, 0)
}
func TestInterface(t *testing.T) {
var result *Result
var src string
src = "test/interface.go"
result = getResult(t, false, "test", src)
checkCount(src, t, result, 2)
checkSelector(src, t, result, "foo.(Fooer).method.Foo", 1, 0, 0, 0, 0)
checkSelector(src, t, result, "foo.interface.Fooer", 1, 0, 0, 0, 0)
}
func TestInternal(t *testing.T) {
var result *Result
var src string
src = "test/recursion.go"
result = getResult(t, false, "github.com/divan/depscheck/test", src)
checkCount(src, t, result, 0)
result = getResult(t, true, "github.com/divan/depscheck/test", src)
checkCount(src, t, result, 2)
src = "test/pkg_renamed.go"
result = getResult(t, false, "github.com/divan/depscheck/test", src)
checkCount(src, t, result, 0)
result = getResult(t, true, "github.com/divan/depscheck/test", src)
checkCount(src, t, result, 3)
src = "test/external.go"
result = getResult(t, false, "github.com/divan/depscheck/test", src)
checkCount(src, t, result, 1)
result = getResult(t, true, "github.com/divan/depscheck/test", src)
checkCount(src, t, result, 2)
}
func getResult(t *testing.T, isInternal bool, name string, sources ...string) *Result {
var conf loader.Config
conf.CreateFromFilenames(name, sources...)
p, err := conf.Load()
if err != nil {
t.Fatal(err)
}
w := NewWalker(p, false, isInternal)
return w.TopWalk()
}
func checkCount(src string, t *testing.T, r *Result, want int) {
if have := len(r.Counter); have != want {
t.Fatalf("%s: expected to have %d selectors, but have %d", src, want, have)
}
}
func checkSelector(src string, t *testing.T, r *Result, fn string, count, loc, loccum, depth, depthint int) {
sel, ok := r.Selectors[fn]
if !ok {
t.Fatalf("%s: expected to see func '%s' in result, but could not", src, fn)
}
if r.Counter[fn] != count {
t.Fatalf("%s: expected to func '%s' to have Count %d , but got %d", src, fn, count, r.Counter[fn])
}
if sel.LOC != loc {
t.Fatalf("%s: expected to func '%s' to have %d LOC, but got %d", src, fn, loc, sel.LOC)
}
if sel.LOCCum() != loccum {
t.Fatalf("%s: expected to func '%s' to have %d Cumulative LOC, but got %d", src, fn, loccum, sel.LOCCum())
}
if sel.Depth() != depth {
t.Fatalf("%s: expected to func '%s' to have Depth %d, but got %d", src, fn, depth, sel.Depth())
}
if sel.DepthInternal() != depthint {
t.Fatalf("%s: expected to func '%s' to have %d Depth Internal, but got %d", src, fn, depthint, sel.DepthInternal())
}
}