xref: /aosp_15_r20/external/golang-protobuf/internal/descfmt/desc_test.go (revision 1c12ee1efe575feb122dbf939ff15148a3b3e8f2)
1*1c12ee1eSDan Willemsen// Copyright 2018 The Go Authors. All rights reserved.
2*1c12ee1eSDan Willemsen// Use of this source code is governed by a BSD-style
3*1c12ee1eSDan Willemsen// license that can be found in the LICENSE file.
4*1c12ee1eSDan Willemsen
5*1c12ee1eSDan Willemsenpackage descfmt
6*1c12ee1eSDan Willemsen
7*1c12ee1eSDan Willemsenimport (
8*1c12ee1eSDan Willemsen	"testing"
9*1c12ee1eSDan Willemsen)
10*1c12ee1eSDan Willemsen
11*1c12ee1eSDan Willemsen// TestDescriptorAccessors tests that descriptorAccessors is up-to-date.
12*1c12ee1eSDan Willemsenfunc TestDescriptorAccessors(t *testing.T) {
13*1c12ee1eSDan Willemsen	ignore := map[string]bool{
14*1c12ee1eSDan Willemsen		"ParentFile":    true,
15*1c12ee1eSDan Willemsen		"Parent":        true,
16*1c12ee1eSDan Willemsen		"Index":         true,
17*1c12ee1eSDan Willemsen		"Syntax":        true,
18*1c12ee1eSDan Willemsen		"Name":          true,
19*1c12ee1eSDan Willemsen		"FullName":      true,
20*1c12ee1eSDan Willemsen		"IsPlaceholder": true,
21*1c12ee1eSDan Willemsen		"Options":       true,
22*1c12ee1eSDan Willemsen		"ProtoInternal": true,
23*1c12ee1eSDan Willemsen		"ProtoType":     true,
24*1c12ee1eSDan Willemsen
25*1c12ee1eSDan Willemsen		"TextName":           true, // derived from other fields
26*1c12ee1eSDan Willemsen		"HasOptionalKeyword": true, // captured by HasPresence
27*1c12ee1eSDan Willemsen		"IsSynthetic":        true, // captured by HasPresence
28*1c12ee1eSDan Willemsen
29*1c12ee1eSDan Willemsen		"SourceLocations":       true, // specific to FileDescriptor
30*1c12ee1eSDan Willemsen		"ExtensionRangeOptions": true, // specific to MessageDescriptor
31*1c12ee1eSDan Willemsen		"DefaultEnumValue":      true, // specific to FieldDescriptor
32*1c12ee1eSDan Willemsen		"MapKey":                true, // specific to FieldDescriptor
33*1c12ee1eSDan Willemsen		"MapValue":              true, // specific to FieldDescriptor
34*1c12ee1eSDan Willemsen	}
35*1c12ee1eSDan Willemsen
36*1c12ee1eSDan Willemsen	for rt, m := range descriptorAccessors {
37*1c12ee1eSDan Willemsen		got := map[string]bool{}
38*1c12ee1eSDan Willemsen		for _, s := range m {
39*1c12ee1eSDan Willemsen			got[s] = true
40*1c12ee1eSDan Willemsen		}
41*1c12ee1eSDan Willemsen		want := map[string]bool{}
42*1c12ee1eSDan Willemsen		for i := 0; i < rt.NumMethod(); i++ {
43*1c12ee1eSDan Willemsen			want[rt.Method(i).Name] = true
44*1c12ee1eSDan Willemsen		}
45*1c12ee1eSDan Willemsen
46*1c12ee1eSDan Willemsen		// Check if descriptorAccessors contains a non-existent accessor.
47*1c12ee1eSDan Willemsen		// If this test fails, remove the accessor from descriptorAccessors.
48*1c12ee1eSDan Willemsen		for s := range got {
49*1c12ee1eSDan Willemsen			if !want[s] && !ignore[s] {
50*1c12ee1eSDan Willemsen				t.Errorf("%v.%v does not exist", rt, s)
51*1c12ee1eSDan Willemsen			}
52*1c12ee1eSDan Willemsen		}
53*1c12ee1eSDan Willemsen
54*1c12ee1eSDan Willemsen		// Check if there are new protoreflect interface methods that are not
55*1c12ee1eSDan Willemsen		// handled by the formatter. If this fails, either add the method to
56*1c12ee1eSDan Willemsen		// ignore or add them to descriptorAccessors.
57*1c12ee1eSDan Willemsen		for s := range want {
58*1c12ee1eSDan Willemsen			if !got[s] && !ignore[s] {
59*1c12ee1eSDan Willemsen				t.Errorf("%v.%v is not called by formatter", rt, s)
60*1c12ee1eSDan Willemsen			}
61*1c12ee1eSDan Willemsen		}
62*1c12ee1eSDan Willemsen	}
63*1c12ee1eSDan Willemsen}
64