codes_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package codes
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "testing"
  20. )
  21. func TestCodeString(t *testing.T) {
  22. tests := []struct {
  23. code Code
  24. want string
  25. }{
  26. {Unset, "Unset"},
  27. {Error, "Error"},
  28. {Ok, "Ok"},
  29. }
  30. for _, test := range tests {
  31. if got := test.code.String(); got != test.want {
  32. t.Errorf("String of code %d %q, want %q", test.code, got, test.want)
  33. }
  34. }
  35. }
  36. func TestCodeUnmarshalJSONNull(t *testing.T) {
  37. c := new(Code)
  38. orig := c
  39. if err := c.UnmarshalJSON([]byte("null")); err != nil {
  40. t.Fatalf("Code.UnmarshalJSON(\"null\") errored: %v", err)
  41. }
  42. if orig != c {
  43. t.Error("Code.UnmarshalJSON(\"null\") should not decode a value")
  44. }
  45. }
  46. func TestCodeUnmarshalJSONNil(t *testing.T) {
  47. c := (*Code)(nil)
  48. if err := c.UnmarshalJSON([]byte{}); err == nil {
  49. t.Fatalf("Code(nil).UnmarshalJSON() did not error")
  50. }
  51. }
  52. func TestCodeUnmarshalJSON(t *testing.T) {
  53. tests := []struct {
  54. input string
  55. want Code
  56. }{
  57. {"0", Unset},
  58. {`"Unset"`, Unset},
  59. {"1", Error},
  60. {`"Error"`, Error},
  61. {"2", Ok},
  62. {`"Ok"`, Ok},
  63. }
  64. for _, test := range tests {
  65. c := new(Code)
  66. *c = Code(maxCode)
  67. if err := json.Unmarshal([]byte(test.input), c); err != nil {
  68. t.Fatalf("json.Unmarshal(%q, Code) errored: %v", test.input, err)
  69. }
  70. if *c != test.want {
  71. t.Errorf("failed to unmarshal %q as %v", test.input, test.want)
  72. }
  73. }
  74. }
  75. func TestCodeUnmarshalJSONErrorInvalidData(t *testing.T) {
  76. tests := []string{
  77. fmt.Sprintf("%d", maxCode),
  78. "Not a code",
  79. "Unset",
  80. "true",
  81. `"Not existing"`,
  82. "",
  83. }
  84. c := new(Code)
  85. for _, test := range tests {
  86. if err := json.Unmarshal([]byte(test), c); err == nil {
  87. t.Fatalf("json.Unmarshal(%q, Code) did not error", test)
  88. }
  89. }
  90. }
  91. func TestCodeMarshalJSONNil(t *testing.T) {
  92. c := (*Code)(nil)
  93. b, err := c.MarshalJSON()
  94. if err != nil {
  95. t.Fatalf("Code(nil).MarshalJSON() errored: %v", err)
  96. }
  97. if !bytes.Equal(b, []byte("null")) {
  98. t.Errorf("Code(nil).MarshalJSON() returned %s, want \"null\"", string(b))
  99. }
  100. }
  101. func TestCodeMarshalJSON(t *testing.T) {
  102. tests := []struct {
  103. code Code
  104. want string
  105. }{
  106. {Unset, `"Unset"`},
  107. {Error, `"Error"`},
  108. {Ok, `"Ok"`},
  109. }
  110. for _, test := range tests {
  111. b, err := test.code.MarshalJSON()
  112. if err != nil {
  113. t.Fatalf("Code(%s).MarshalJSON() errored: %v", test.code, err)
  114. }
  115. if !bytes.Equal(b, []byte(test.want)) {
  116. t.Errorf("Code(%s).MarshalJSON() returned %s, want %s", test.code, string(b), test.want)
  117. }
  118. }
  119. }
  120. func TestCodeMarshalJSONErrorInvalid(t *testing.T) {
  121. c := new(Code)
  122. *c = Code(maxCode)
  123. if b, err := c.MarshalJSON(); err == nil {
  124. t.Fatalf("Code(maxCode).MarshalJSON() did not error")
  125. } else if b != nil {
  126. t.Fatal("Code(maxCode).MarshalJSON() returned non-nil value")
  127. }
  128. }
  129. func TestRoundTripCodes(t *testing.T) {
  130. tests := []struct {
  131. input Code
  132. }{
  133. {Unset},
  134. {Error},
  135. {Ok},
  136. }
  137. for _, test := range tests {
  138. c := test.input
  139. out := new(Code)
  140. b, err := c.MarshalJSON()
  141. if err != nil {
  142. t.Fatalf("Code(%s).MarshalJSON() errored: %v", test.input, err)
  143. }
  144. if err := out.UnmarshalJSON(b); err != nil {
  145. t.Fatalf("Code.UnmarshalJSON(%q) errored: %v", c, err)
  146. }
  147. if *out != test.input {
  148. t.Errorf("failed to round trip %q, output was %v", test.input, out)
  149. }
  150. }
  151. }