...

Source file src/go.opentelemetry.io/otel/codes/codes_test.go

Documentation: go.opentelemetry.io/otel/codes

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

View as plain text