This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
forked from emicklei/proto
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parent_test.go
124 lines (118 loc) · 3.71 KB
/
parent_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
// Copyright (c) 2018 Ernest Micklei
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package proto
import (
"fmt"
"testing"
)
type parentChecker struct {
errors []error
}
func checkParent(v Visitee, t *testing.T) {
pc := new(parentChecker)
v.Accept(pc)
if len(pc.errors) == 0 {
return
}
for _, each := range pc.errors {
t.Error(each)
}
}
func (pc *parentChecker) checkAll(list []Visitee, parent Visitee) {
for _, each := range list {
if _, ok := each.(*Comment); ok {
continue
}
if got, want := getParent(each), parent; got != want {
pc.errors = append(pc.errors, fmt.Errorf("%T has wrong parent set, got %v want %v", each, got, want))
}
each.Accept(pc)
}
}
func (pc *parentChecker) check(astType, astName string, parent Visitee) {
if parent == nil {
pc.errors = append(pc.errors, fmt.Errorf("%s %s has no parent set", astType, astName))
}
}
func (pc *parentChecker) VisitProto(p *Proto) {
pc.checkAll(p.Elements, p)
}
func (pc *parentChecker) VisitMessage(m *Message) {
pc.check("Message", m.Name, m.Parent)
pc.checkAll(m.Elements, m)
}
func (pc *parentChecker) VisitService(v *Service) {
pc.check("Service", v.Name, v.Parent)
pc.checkAll(v.Elements, v)
}
func (pc *parentChecker) VisitSyntax(s *Syntax) {
pc.check("Syntax", s.Value, s.Parent)
}
func (pc *parentChecker) VisitPackage(p *Package) {
pc.check("Package", p.Name, p.Parent)
}
func (pc *parentChecker) VisitOption(o *Option) {
pc.check("Option", o.Name, o.Parent)
}
func (pc *parentChecker) VisitImport(i *Import) {
pc.check("Import", i.Filename, i.Parent)
}
func (pc *parentChecker) VisitNormalField(i *NormalField) {
pc.check("NormalField", i.Name, i.Parent)
}
func (pc *parentChecker) VisitEnumField(i *EnumField) {
pc.check("EnumField", i.Name, i.Parent)
}
func (pc *parentChecker) VisitEnum(e *Enum) {
pc.check("Enum", e.Name, e.Parent)
pc.checkAll(e.Elements, e)
}
func (pc *parentChecker) VisitComment(e *Comment) {}
func (pc *parentChecker) VisitOneof(o *Oneof) {
pc.check("Oneof", o.Name, o.Parent)
pc.checkAll(o.Elements, o)
}
func (pc *parentChecker) VisitOneofField(o *OneOfField) {
pc.check("OneOfField", o.Name, o.Parent)
}
func (pc *parentChecker) VisitReserved(r *Reserved) {
pc.check("Reserved", "", r.Parent)
}
func (pc *parentChecker) VisitRPC(r *RPC) {
pc.check("RPC", r.Name, r.Parent)
//pc.checkAll(r.Options, r)
for _, each := range r.Options {
pc.check("Option", each.Name, r)
}
}
func (pc *parentChecker) VisitMapField(f *MapField) {
pc.check("MapField", f.Name, f.Parent)
}
// proto2
func (pc *parentChecker) VisitGroup(g *Group) {
pc.check("Group", g.Name, g.Parent)
pc.checkAll(g.Elements, g)
}
func (pc *parentChecker) VisitExtensions(e *Extensions) {
pc.check("Extensions", "", e.Parent)
}