common_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 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 proto
  5. import (
  6. "errors"
  7. "testing"
  8. )
  9. type testErrorList []error
  10. func (e testErrorList) Error() string {
  11. return "testErrorList"
  12. }
  13. type invalidUTF8Error struct{}
  14. func (e invalidUTF8Error) Error() string { return "" }
  15. func (e invalidUTF8Error) InvalidUTF8() bool { return true }
  16. type requiredNotSetError struct{}
  17. func (e requiredNotSetError) Error() string { return "" }
  18. func (e requiredNotSetError) RequiredNotSet() bool { return true }
  19. func TestNonFatalErrors(t *testing.T) {
  20. tests := []struct {
  21. input error
  22. want errorMask
  23. }{{
  24. input: errors.New("not one of them"),
  25. }, {
  26. input: testErrorList{},
  27. }, {
  28. input: testErrorList{
  29. invalidUTF8Error{},
  30. },
  31. want: errInvalidUTF8,
  32. }, {
  33. input: testErrorList{
  34. requiredNotSetError{},
  35. },
  36. want: errRequiredNotSet,
  37. }, {
  38. input: testErrorList{
  39. invalidUTF8Error{},
  40. requiredNotSetError{},
  41. },
  42. want: errInvalidUTF8 | errRequiredNotSet,
  43. }}
  44. for _, tc := range tests {
  45. tc := tc
  46. t.Run("", func(t *testing.T) {
  47. got := nonFatalErrors(tc.input)
  48. if got != tc.want {
  49. t.Errorf("got %v, want %v", got, tc.want)
  50. }
  51. })
  52. }
  53. }