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