proto_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 protoreflect
  5. import "testing"
  6. func TestNameIsValid(t *testing.T) {
  7. tests := []struct {
  8. in Name
  9. want bool
  10. }{
  11. {"", false},
  12. {"a", true},
  13. {".", false},
  14. {"_", true}, // odd, but permitted by protoc
  15. {".foo", false},
  16. {"foo.", false},
  17. {"foo", true},
  18. {"one1_two2_three3", true},
  19. {"1one", false},
  20. }
  21. for _, tt := range tests {
  22. if got := tt.in.IsValid(); got != tt.want {
  23. t.Errorf("Name(%q).IsValid() = %v, want %v", tt.in, got, tt.want)
  24. }
  25. }
  26. }
  27. func TestFullNameIsValid(t *testing.T) {
  28. tests := []struct {
  29. in FullName
  30. want bool
  31. }{
  32. {"", false},
  33. {"a", true},
  34. {"a.b", true},
  35. {"a.b.c", true},
  36. {".", false},
  37. {"_._._", true}, // odd, but permitted by protoc
  38. {".foo", false},
  39. {"foo.", false},
  40. {"foo", true},
  41. {"one1_two2_three3", true},
  42. {"one1.two2.three3", true},
  43. {".one1.two2.three3", false},
  44. {"one1.two2.three3.", false},
  45. {"foo.1one", false},
  46. }
  47. for _, tt := range tests {
  48. if got := tt.in.IsValid(); got != tt.want {
  49. t.Errorf("Name(%q).IsValid() = %v, want %v", tt.in, got, tt.want)
  50. }
  51. }
  52. }
  53. func TestNameAppend(t *testing.T) {
  54. tests := []FullName{
  55. "",
  56. "a",
  57. "a.b",
  58. "a.b.c",
  59. "one1.two2.three3",
  60. }
  61. for _, tt := range tests {
  62. if got := tt.Parent().Append(tt.Name()); got != tt {
  63. t.Errorf("FullName.Parent().Append(FullName.Name()) = %q, want %q", got, tt)
  64. }
  65. }
  66. }