github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/group_version_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	gojson "encoding/json"
    21  	"reflect"
    22  	"testing"
    23  
    24  	utiljson "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/json"
    25  )
    26  
    27  type GroupVersionHolder struct {
    28  	GV GroupVersion `json:"val"`
    29  }
    30  
    31  func TestGroupVersionUnmarshalJSON(t *testing.T) {
    32  	cases := []struct {
    33  		input  []byte
    34  		expect GroupVersion
    35  	}{
    36  		{[]byte(`{"val": "v1"}`), GroupVersion{"", "v1"}},
    37  		{[]byte(`{"val": "apps/v1"}`), GroupVersion{"apps", "v1"}},
    38  	}
    39  
    40  	for _, c := range cases {
    41  		var result GroupVersionHolder
    42  		// test golang lib's JSON codec
    43  		if err := gojson.Unmarshal([]byte(c.input), &result); err != nil {
    44  			t.Errorf("JSON codec failed to unmarshal input '%v': %v", c.input, err)
    45  		}
    46  		if !reflect.DeepEqual(result.GV, c.expect) {
    47  			t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
    48  		}
    49  		// test the utiljson codec
    50  		if err := utiljson.Unmarshal(c.input, &result); err != nil {
    51  			t.Errorf("util/json codec failed to unmarshal input '%v': %v", c.input, err)
    52  		}
    53  		if !reflect.DeepEqual(result.GV, c.expect) {
    54  			t.Errorf("util/json codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
    55  		}
    56  	}
    57  }
    58  
    59  func TestGroupVersionMarshalJSON(t *testing.T) {
    60  	cases := []struct {
    61  		input  GroupVersion
    62  		expect []byte
    63  	}{
    64  		{GroupVersion{"", "v1"}, []byte(`{"val":"v1"}`)},
    65  		{GroupVersion{"apps", "v1"}, []byte(`{"val":"apps/v1"}`)},
    66  	}
    67  
    68  	for _, c := range cases {
    69  		input := GroupVersionHolder{c.input}
    70  		result, err := gojson.Marshal(&input)
    71  		if err != nil {
    72  			t.Errorf("Failed to marshal input '%v': %v", input, err)
    73  		}
    74  		if !reflect.DeepEqual(result, c.expect) {
    75  			t.Errorf("Failed to marshal input '%+v': expected: %s, got: %s", input, c.expect, result)
    76  		}
    77  	}
    78  }