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

     1  /*
     2  Copyright 2014 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  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"sigs.k8s.io/yaml"
    26  )
    27  
    28  type TimeHolder struct {
    29  	T Time `json:"t"`
    30  }
    31  
    32  func TestTimeMarshalYAML(t *testing.T) {
    33  	cases := []struct {
    34  		input  Time
    35  		result string
    36  	}{
    37  		{Time{}, "t: null\n"},
    38  		{Date(1998, time.May, 5, 1, 5, 5, 50, time.FixedZone("test", -4*60*60)), "t: \"1998-05-05T05:05:05Z\"\n"},
    39  		{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: \"1998-05-05T05:05:05Z\"\n"},
    40  	}
    41  
    42  	for _, c := range cases {
    43  		input := TimeHolder{c.input}
    44  		result, err := yaml.Marshal(&input)
    45  		if err != nil {
    46  			t.Errorf("Failed to marshal input: '%v': %v", input, err)
    47  		}
    48  		if string(result) != c.result {
    49  			t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
    50  		}
    51  	}
    52  }
    53  
    54  func TestTimeUnmarshalYAML(t *testing.T) {
    55  	cases := []struct {
    56  		input  string
    57  		result Time
    58  	}{
    59  		{"t: null\n", Time{}},
    60  		{"t: 1998-05-05T05:05:05Z\n", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
    61  	}
    62  
    63  	for _, c := range cases {
    64  		var result TimeHolder
    65  		if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
    66  			t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
    67  		}
    68  		if result.T != c.result {
    69  			t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
    70  		}
    71  	}
    72  }
    73  
    74  func TestTimeMarshalJSON(t *testing.T) {
    75  	cases := []struct {
    76  		input  Time
    77  		result string
    78  	}{
    79  		{Time{}, "{\"t\":null}"},
    80  		{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"},
    81  		{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"},
    82  	}
    83  
    84  	for _, c := range cases {
    85  		input := TimeHolder{c.input}
    86  		result, err := json.Marshal(&input)
    87  		if err != nil {
    88  			t.Errorf("Failed to marshal input: '%v': %v", input, err)
    89  		}
    90  		if string(result) != c.result {
    91  			t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
    92  		}
    93  	}
    94  }
    95  
    96  func TestTimeUnmarshalJSON(t *testing.T) {
    97  	cases := []struct {
    98  		input  string
    99  		result Time
   100  	}{
   101  		{"{\"t\":null}", Time{}},
   102  		{"{\"t\":\"1998-05-05T05:05:05Z\"}", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
   103  	}
   104  
   105  	for _, c := range cases {
   106  		var result TimeHolder
   107  		if err := json.Unmarshal([]byte(c.input), &result); err != nil {
   108  			t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
   109  		}
   110  		if result.T != c.result {
   111  			t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
   112  		}
   113  	}
   114  }
   115  
   116  func TestTimeMarshalJSONUnmarshalYAML(t *testing.T) {
   117  	cases := []struct {
   118  		input Time
   119  	}{
   120  		{Time{}},
   121  		{Date(1998, time.May, 5, 5, 5, 5, 50, time.Local).Rfc3339Copy()},
   122  		{Date(1998, time.May, 5, 5, 5, 5, 0, time.Local).Rfc3339Copy()},
   123  	}
   124  
   125  	for i, c := range cases {
   126  		input := TimeHolder{c.input}
   127  		jsonMarshalled, err := json.Marshal(&input)
   128  		if err != nil {
   129  			t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err)
   130  		}
   131  
   132  		var result TimeHolder
   133  		err = yaml.Unmarshal(jsonMarshalled, &result)
   134  		if err != nil {
   135  			t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err)
   136  		}
   137  
   138  		iN, iO := input.T.Zone()
   139  		oN, oO := result.T.Zone()
   140  		if iN != oN || iO != oO {
   141  			t.Errorf("%d-3: Time zones differ before and after serialization %s:%d %s:%d", i, iN, iO, oN, oO)
   142  		}
   143  
   144  		if input.T.UnixNano() != result.T.UnixNano() {
   145  			t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result)
   146  		}
   147  	}
   148  }
   149  
   150  func TestTimeProto(t *testing.T) {
   151  	cases := []struct {
   152  		input Time
   153  	}{
   154  		{Time{}},
   155  		{Date(1998, time.May, 5, 1, 5, 5, 0, time.Local)},
   156  		{Date(1998, time.May, 5, 5, 5, 5, 0, time.Local)},
   157  	}
   158  
   159  	for _, c := range cases {
   160  		input := c.input
   161  		data, err := input.Marshal()
   162  		if err != nil {
   163  			t.Fatalf("Failed to marshal input: '%v': %v", input, err)
   164  		}
   165  		time := Time{}
   166  		if err := time.Unmarshal(data); err != nil {
   167  			t.Fatalf("Failed to unmarshal output: '%v': %v", input, err)
   168  		}
   169  		if !reflect.DeepEqual(input, time) {
   170  			t.Errorf("Marshal->Unmarshal is not idempotent: '%v' vs '%v'", input, time)
   171  		}
   172  	}
   173  }
   174  
   175  func TestTimeEqual(t *testing.T) {
   176  	t1 := NewTime(time.Now())
   177  	cases := []struct {
   178  		name   string
   179  		x      *Time
   180  		y      *Time
   181  		result bool
   182  	}{
   183  		{"nil =? nil", nil, nil, true},
   184  		{"!nil =? !nil", &t1, &t1, true},
   185  		{"nil =? !nil", nil, &t1, false},
   186  		{"!nil =? nil", &t1, nil, false},
   187  	}
   188  
   189  	for _, c := range cases {
   190  		t.Run(c.name, func(t *testing.T) {
   191  			result := c.x.Equal(c.y)
   192  			if result != c.result {
   193  				t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result)
   194  			}
   195  		})
   196  	}
   197  }
   198  
   199  func TestTimeBefore(t *testing.T) {
   200  	t1 := NewTime(time.Now())
   201  	cases := []struct {
   202  		name string
   203  		x    *Time
   204  		y    *Time
   205  	}{
   206  		{"nil <? nil", nil, nil},
   207  		{"!nil <? !nil", &t1, &t1},
   208  		{"nil <? !nil", nil, &t1},
   209  		{"!nil <? nil", &t1, nil},
   210  	}
   211  
   212  	for _, c := range cases {
   213  		t.Run(c.name, func(t *testing.T) {
   214  			result := c.x.Before(c.y)
   215  			if result {
   216  				t.Errorf("Failed equality test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
   217  			}
   218  		})
   219  	}
   220  }
   221  
   222  func TestTimeIsZero(t *testing.T) {
   223  	t1 := NewTime(time.Now())
   224  	cases := []struct {
   225  		name   string
   226  		x      *Time
   227  		result bool
   228  	}{
   229  		{"nil =? 0", nil, true},
   230  		{"!nil =? 0", &t1, false},
   231  	}
   232  
   233  	for _, c := range cases {
   234  		t.Run(c.name, func(t *testing.T) {
   235  			result := c.x.IsZero()
   236  			if result != c.result {
   237  				t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.x, c.result, result)
   238  			}
   239  		})
   240  	}
   241  }