-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterateInterfaces_test.go
More file actions
128 lines (123 loc) · 4.07 KB
/
iterateInterfaces_test.go
File metadata and controls
128 lines (123 loc) · 4.07 KB
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
package parser
import (
"fmt"
"github.com/mathbalduino/go-log/loggerCLI"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/packages"
"testing"
)
func TestIterateInterfaces(t *testing.T) {
fakeScopeObjects := func() (map[string]*ast.Object, *types.Info) {
typeNameIdents := []*ast.Ident{{Name: "TypeName_0"}, {}, {Name: "TypeName_1"}, {}, {Name: "TypeName_2"}}
typesInfo := &types.Info{
Defs: map[*ast.Ident]types.Object{
typeNameIdents[0]: types.NewTypeName(0, nil, typeNameIdents[0].Name, &types.Interface{}),
typeNameIdents[1]: types.NewTypeName(0, nil, typeNameIdents[1].Name, &types.Struct{}),
typeNameIdents[2]: types.NewTypeName(0, nil, typeNameIdents[2].Name, &types.Interface{}),
typeNameIdents[3]: types.NewTypeName(0, nil, typeNameIdents[3].Name, &types.Struct{}),
typeNameIdents[4]: types.NewTypeName(0, nil, typeNameIdents[4].Name, &types.Interface{}),
},
}
return map[string]*ast.Object{
"0": {Decl: &ast.TypeSpec{Name: typeNameIdents[0]}},
"1": {Decl: &ast.TypeSpec{Name: typeNameIdents[1]}},
"2": {Decl: &ast.TypeSpec{Name: typeNameIdents[2]}},
"3": {Decl: &ast.TypeSpec{Name: typeNameIdents[3]}},
"4": {Decl: &ast.TypeSpec{Name: typeNameIdents[4]}},
}, typesInfo
}
fakeTypeNames := func() *GoParser {
fileSet := token.NewFileSet()
fileSet.AddFile("a", 1, 5)
objects, typesInfo := fakeScopeObjects()
return &GoParser{
pkgs: []*packages.Package{{
Syntax: []*ast.File{{
Package: 2,
Scope: &ast.Scope{Objects: objects},
}},
TypesInfo: typesInfo,
}},
logger: loggerCLI.New(false, 0),
fileSet: fileSet,
}
}
t.Run("Should forward the optionalLogger, if provided, to the iterateTypeNames method (this test depends on the iterateTypeNames forwarding its own optionalLogger to its callback)", func(t *testing.T) {
p := fakeTypeNames()
mock := &mockLoggerCLI{}
mock.mockTrace = func(string, ...interface{}) LoggerCLI { return mock }
e := p.IterateInterfaces(func(type_ *types.TypeName, logger LoggerCLI) error {
m, isMock := logger.(*mockLoggerCLI)
if !isMock || m != mock {
t.Fatalf("The LoggerCLI given to the callback is not the expected one")
}
return nil
}, mock)
if e != nil {
t.Fatalf("Expected to be nil")
}
})
t.Run("Should return nil errors when there are no Interfaces to iterate", func(t *testing.T) {
p := fakeTypeNames()
// Remove the interfaces from the Objects map (see fakeScopeObjects above)
delete(p.pkgs[0].Syntax[0].Scope.Objects, "0")
delete(p.pkgs[0].Syntax[0].Scope.Objects, "2")
delete(p.pkgs[0].Syntax[0].Scope.Objects, "4")
e := p.IterateInterfaces(func(type_ *types.TypeName, parentLog LoggerCLI) error { return nil })
if e != nil {
t.Fatalf("Expected to be nil")
}
})
t.Run("Should return errors returned by the callback", func(t *testing.T) {
p := fakeTypeNames()
e := p.IterateInterfaces(func(type_ *types.TypeName, parentLog LoggerCLI) error {
return fmt.Errorf("error")
})
if e == nil {
t.Fatalf("Expected to be not nil")
}
})
t.Run("Skip everything that is not a Interface", func(t *testing.T) {
p := fakeTypeNames()
calls := 0
e := p.IterateInterfaces(func(type_ *types.TypeName, parentLog LoggerCLI) error {
calls += 1
return nil
})
if e != nil {
t.Fatalf("Expected to be nil")
}
if calls != 3 {
t.Fatalf("Callback was expected to be called 3 times")
}
})
t.Run("Should call the callback for every Interface that needs to be iterated", func(t *testing.T) {
p := fakeTypeNames()
callsA, callsB, callsC := 0, 0, 0
e := p.IterateInterfaces(func(type_ *types.TypeName, parentLog LoggerCLI) error {
switch type_.Name() {
// Strings from "fakeScopeObjects"
case "TypeName_0":
callsA += 1
return nil
case "TypeName_1":
callsB += 1
return nil
case "TypeName_2":
callsC += 1
return nil
default:
t.Fatalf("Unexpected Interface callback call")
return nil
}
})
if e != nil {
t.Fatalf("Expected to be nil")
}
if callsA != 1 || callsB != 1 || callsC != 1 {
t.Fatalf("Callback was expected to be called one time for every Interface")
}
})
}