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