| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package codes |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/json" |
| 9 |
"fmt" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestCodeString(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
code Code |
| 16 |
want string |
| 17 |
}{ |
| 18 |
{Unset, "Unset"}, |
| 19 |
{Error, "Error"}, |
| 20 |
{Ok, "Ok"}, |
| 21 |
} |
| 22 |
|
| 23 |
for _, test := range tests { |
| 24 |
if got := test.code.String(); got != test.want { |
| 25 |
t.Errorf("String of code %d %q, want %q", test.code, got, test.want) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestCodeUnmarshalJSONNull(t *testing.T) { |
| 31 |
c := new(Code) |
| 32 |
orig := c |
| 33 |
if err := c.UnmarshalJSON([]byte("null")); err != nil { |
| 34 |
t.Fatalf("Code.UnmarshalJSON(\"null\") errored: %v", err) |
| 35 |
} |
| 36 |
if orig != c { |
| 37 |
t.Error("Code.UnmarshalJSON(\"null\") should not decode a value") |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestCodeUnmarshalJSONNil(t *testing.T) { |
| 42 |
c := (*Code)(nil) |
| 43 |
if err := c.UnmarshalJSON([]byte{}); err == nil { |
| 44 |
t.Fatalf("Code(nil).UnmarshalJSON() did not error") |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestCodeUnmarshalJSON(t *testing.T) { |
| 49 |
tests := []struct { |
| 50 |
input string |
| 51 |
want Code |
| 52 |
}{ |
| 53 |
{"0", Unset}, |
| 54 |
{`"Unset"`, Unset}, |
| 55 |
{"1", Error}, |
| 56 |
{`"Error"`, Error}, |
| 57 |
{"2", Ok}, |
| 58 |
{`"Ok"`, Ok}, |
| 59 |
} |
| 60 |
for _, test := range tests { |
| 61 |
c := new(Code) |
| 62 |
*c = Code(maxCode) |
| 63 |
|
| 64 |
if err := json.Unmarshal([]byte(test.input), c); err != nil { |
| 65 |
t.Fatalf("json.Unmarshal(%q, Code) errored: %v", test.input, err) |
| 66 |
} |
| 67 |
if *c != test.want { |
| 68 |
t.Errorf("failed to unmarshal %q as %v", test.input, test.want) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestCodeUnmarshalJSONErrorInvalidData(t *testing.T) { |
| 74 |
tests := []string{ |
| 75 |
fmt.Sprintf("%d", maxCode), |
| 76 |
"Not a code", |
| 77 |
"Unset", |
| 78 |
"true", |
| 79 |
`"Not existing"`, |
| 80 |
"", |
| 81 |
} |
| 82 |
c := new(Code) |
| 83 |
for _, test := range tests { |
| 84 |
if err := json.Unmarshal([]byte(test), c); err == nil { |
| 85 |
t.Fatalf("json.Unmarshal(%q, Code) did not error", test) |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestCodeMarshalJSONNil(t *testing.T) { |
| 91 |
c := (*Code)(nil) |
| 92 |
b, err := c.MarshalJSON() |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("Code(nil).MarshalJSON() errored: %v", err) |
| 95 |
} |
| 96 |
if !bytes.Equal(b, []byte("null")) { |
| 97 |
t.Errorf("Code(nil).MarshalJSON() returned %s, want \"null\"", string(b)) |
| 98 |
} |
| 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 |
|
| 111 |
for _, test := range tests { |
| 112 |
b, err := test.code.MarshalJSON() |
| 113 |
if err != nil { |
| 114 |
t.Fatalf("Code(%s).MarshalJSON() errored: %v", test.code, err) |
| 115 |
} |
| 116 |
if !bytes.Equal(b, []byte(test.want)) { |
| 117 |
t.Errorf("Code(%s).MarshalJSON() returned %s, want %s", test.code, string(b), test.want) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func TestCodeMarshalJSONErrorInvalid(t *testing.T) { |
| 123 |
c := new(Code) |
| 124 |
*c = Code(maxCode) |
| 125 |
if b, err := c.MarshalJSON(); err == nil { |
| 126 |
t.Fatalf("Code(maxCode).MarshalJSON() did not error") |
| 127 |
} else if b != nil { |
| 128 |
t.Fatal("Code(maxCode).MarshalJSON() returned non-nil value") |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func TestRoundTripCodes(t *testing.T) { |
| 133 |
tests := []struct { |
| 134 |
input Code |
| 135 |
}{ |
| 136 |
{Unset}, |
| 137 |
{Error}, |
| 138 |
{Ok}, |
| 139 |
} |
| 140 |
for _, test := range tests { |
| 141 |
c := test.input |
| 142 |
out := new(Code) |
| 143 |
|
| 144 |
b, err := c.MarshalJSON() |
| 145 |
if err != nil { |
| 146 |
t.Fatalf("Code(%s).MarshalJSON() errored: %v", test.input, err) |
| 147 |
} |
| 148 |
|
| 149 |
if err := out.UnmarshalJSON(b); err != nil { |
| 150 |
t.Fatalf("Code.UnmarshalJSON(%q) errored: %v", c, err) |
| 151 |
} |
| 152 |
|
| 153 |
if *out != test.input { |
| 154 |
t.Errorf("failed to round trip %q, output was %v", test.input, out) |
| 155 |
} |
| 156 |
} |
| 157 |
} |