codes_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package codes
  19. import (
  20. "encoding/json"
  21. "reflect"
  22. "testing"
  23. cpb "google.golang.org/genproto/googleapis/rpc/code"
  24. "google.golang.org/grpc/internal/grpctest"
  25. )
  26. type s struct {
  27. grpctest.Tester
  28. }
  29. func Test(t *testing.T) {
  30. grpctest.RunSubTests(t, s{})
  31. }
  32. func (s) TestUnmarshalJSON(t *testing.T) {
  33. for s, v := range cpb.Code_value {
  34. want := Code(v)
  35. var got Code
  36. if err := got.UnmarshalJSON([]byte(`"` + s + `"`)); err != nil || got != want {
  37. t.Errorf("got.UnmarshalJSON(%q) = %v; want <nil>. got=%v; want %v", s, err, got, want)
  38. }
  39. }
  40. }
  41. func (s) TestJSONUnmarshal(t *testing.T) {
  42. var got []Code
  43. want := []Code{OK, NotFound, Internal, Canceled}
  44. in := `["OK", "NOT_FOUND", "INTERNAL", "CANCELLED"]`
  45. err := json.Unmarshal([]byte(in), &got)
  46. if err != nil || !reflect.DeepEqual(got, want) {
  47. t.Fatalf("json.Unmarshal(%q, &got) = %v; want <nil>. got=%v; want %v", in, err, got, want)
  48. }
  49. }
  50. func (s) TestUnmarshalJSON_NilReceiver(t *testing.T) {
  51. var got *Code
  52. in := OK.String()
  53. if err := got.UnmarshalJSON([]byte(in)); err == nil {
  54. t.Errorf("got.UnmarshalJSON(%q) = nil; want <non-nil>. got=%v", in, got)
  55. }
  56. }
  57. func (s) TestUnmarshalJSON_UnknownInput(t *testing.T) {
  58. var got Code
  59. for _, in := range [][]byte{[]byte(""), []byte("xxx"), []byte("Code(17)"), nil} {
  60. if err := got.UnmarshalJSON([]byte(in)); err == nil {
  61. t.Errorf("got.UnmarshalJSON(%q) = nil; want <non-nil>. got=%v", in, got)
  62. }
  63. }
  64. }
  65. func (s) TestUnmarshalJSON_MarshalUnmarshal(t *testing.T) {
  66. for i := 0; i < _maxCode; i++ {
  67. var cUnMarshaled Code
  68. c := Code(i)
  69. cJSON, err := json.Marshal(c)
  70. if err != nil {
  71. t.Errorf("marshalling %q failed: %v", c, err)
  72. }
  73. if err := json.Unmarshal(cJSON, &cUnMarshaled); err != nil {
  74. t.Errorf("unmarshalling code failed: %s", err)
  75. }
  76. if c != cUnMarshaled {
  77. t.Errorf("code is %q after marshalling/unmarshalling, expected %q", cUnMarshaled, c)
  78. }
  79. }
  80. }