Debian Codegraph @

parent_test.go

raw symbols SCIP
1 // Copyright (c) 2018 Ernest Micklei
2 //
3 // # MIT License
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 package proto
24
25 import (
26 "fmt"
27 "testing"
28 )
29
30 type parentChecker struct {
31 errors []error
32 }
33
34 func checkParent(v Visitee, t *testing.T) {
35 pc := new(parentChecker)
36 v.Accept(pc)
37 if len(pc.errors) == 0 {
38 return
39 }
40 for _, each := range pc.errors {
41 t.Error(each)
42 }
43 }
44
45 func (pc *parentChecker) checkAll(list []Visitee, parent Visitee) {
46 for _, each := range list {
47 if _, ok := each.(*Comment); ok {
48 continue
49 }
50 if got, want := getParent(each), parent; got != want {
51 pc.errors = append(pc.errors, fmt.Errorf("%T has wrong parent set, got %v want %v", each, got, want))
52 }
53 each.Accept(pc)
54 }
55 }
56 func (pc *parentChecker) check(astType, astName string, parent Visitee) {
57 if parent == nil {
58 pc.errors = append(pc.errors, fmt.Errorf("%s %s has no parent set", astType, astName))
59 }
60 }
61
62 func (pc *parentChecker) VisitProto(p *Proto) {
63 pc.checkAll(p.Elements, p)
64 }
65 func (pc *parentChecker) VisitMessage(m *Message) {
66 pc.check("Message", m.Name, m.Parent)
67 pc.checkAll(m.Elements, m)
68 }
69 func (pc *parentChecker) VisitService(v *Service) {
70 pc.check("Service", v.Name, v.Parent)
71 pc.checkAll(v.Elements, v)
72 }
73 func (pc *parentChecker) VisitSyntax(s *Syntax) {
74 pc.check("Syntax", s.Value, s.Parent)
75 }
76 func (pc *parentChecker) VisitPackage(p *Package) {
77 pc.check("Package", p.Name, p.Parent)
78 }
79 func (pc *parentChecker) VisitOption(o *Option) {
80 pc.check("Option", o.Name, o.Parent)
81 }
82 func (pc *parentChecker) VisitImport(i *Import) {
83 pc.check("Import", i.Filename, i.Parent)
84 }
85 func (pc *parentChecker) VisitNormalField(i *NormalField) {
86 pc.check("NormalField", i.Name, i.Parent)
87 }
88 func (pc *parentChecker) VisitEnumField(i *EnumField) {
89 pc.check("EnumField", i.Name, i.Parent)
90 }
91 func (pc *parentChecker) VisitEnum(e *Enum) {
92 pc.check("Enum", e.Name, e.Parent)
93 pc.checkAll(e.Elements, e)
94 }
95 func (pc *parentChecker) VisitComment(e *Comment) {}
96 func (pc *parentChecker) VisitOneof(o *Oneof) {
97 pc.check("Oneof", o.Name, o.Parent)
98 pc.checkAll(o.Elements, o)
99 }
100 func (pc *parentChecker) VisitOneofField(o *OneOfField) {
101 pc.check("OneOfField", o.Name, o.Parent)
102 }
103 func (pc *parentChecker) VisitReserved(r *Reserved) {
104 pc.check("Reserved", "", r.Parent)
105 }
106 func (pc *parentChecker) VisitRPC(r *RPC) {
107 pc.check("RPC", r.Name, r.Parent)
108 //pc.checkAll(r.Options, r)
109 for _, each := range r.Options {
110 pc.check("Option", each.Name, r)
111 }
112 }
113 func (pc *parentChecker) VisitMapField(f *MapField) {
114 pc.check("MapField", f.Name, f.Parent)
115 }
116
117 // proto2
118 func (pc *parentChecker) VisitGroup(g *Group) {
119 pc.check("Group", g.Name, g.Parent)
120 pc.checkAll(g.Elements, g)
121 }
122 func (pc *parentChecker) VisitExtensions(e *Extensions) {
123 pc.check("Extensions", "", e.Parent)
124 }
125
126 // edition (proto3+)
127 func (pc *parentChecker) VisitEdition(e *Edition) {
128 pc.check("Edition", "", e.Parent)
129 }