desc_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. package descfmt
  5. import (
  6. "testing"
  7. )
  8. // TestDescriptorAccessors tests that descriptorAccessors is up-to-date.
  9. func TestDescriptorAccessors(t *testing.T) {
  10. ignore := map[string]bool{
  11. "ParentFile": true,
  12. "Parent": true,
  13. "Index": true,
  14. "Syntax": true,
  15. "Name": true,
  16. "FullName": true,
  17. "IsPlaceholder": true,
  18. "Options": true,
  19. "ProtoInternal": true,
  20. "ProtoType": true,
  21. "SourceLocations": true, // specific to FileDescriptor
  22. "ExtensionRangeOptions": true, // specific to MessageDescriptor
  23. "DefaultEnumValue": true, // specific to FieldDescriptor
  24. "MapKey": true, // specific to FieldDescriptor
  25. "MapValue": true, // specific to FieldDescriptor
  26. }
  27. for rt, m := range descriptorAccessors {
  28. got := map[string]bool{}
  29. for _, s := range m {
  30. got[s] = true
  31. }
  32. want := map[string]bool{}
  33. for i := 0; i < rt.NumMethod(); i++ {
  34. want[rt.Method(i).Name] = true
  35. }
  36. // Check if descriptorAccessors contains a non-existent accessor.
  37. // If this test fails, remove the accessor from descriptorAccessors.
  38. for s := range got {
  39. if !want[s] && !ignore[s] {
  40. t.Errorf("%v.%v does not exist", rt, s)
  41. }
  42. }
  43. // Check if there are new protoreflect interface methods that are not
  44. // handled by the formatter. If this fails, either add the method to
  45. // ignore or add them to descriptorAccessors.
  46. for s := range want {
  47. if !got[s] && !ignore[s] {
  48. t.Errorf("%v.%v is not called by formatter", rt, s)
  49. }
  50. }
  51. }
  52. }