github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/time/time_test.go (about) 1 /* 2 Copyright The Helm 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 time 18 19 import ( 20 "encoding/json" 21 "testing" 22 "time" 23 ) 24 25 var ( 26 testingTime, _ = Parse(time.RFC3339, "1977-09-02T22:04:05Z") 27 testingTimeString = `"1977-09-02T22:04:05Z"` 28 ) 29 30 func TestNonZeroValueMarshal(t *testing.T) { 31 res, err := json.Marshal(testingTime) 32 if err != nil { 33 t.Fatal(err) 34 } 35 if testingTimeString != string(res) { 36 t.Errorf("expected a marshaled value of %s, got %s", testingTimeString, res) 37 } 38 } 39 40 func TestZeroValueMarshal(t *testing.T) { 41 res, err := json.Marshal(Time{}) 42 if err != nil { 43 t.Fatal(err) 44 } 45 if string(res) != emptyString { 46 t.Errorf("expected zero value to marshal to empty string, got %s", res) 47 } 48 } 49 50 func TestNonZeroValueUnmarshal(t *testing.T) { 51 var myTime Time 52 err := json.Unmarshal([]byte(testingTimeString), &myTime) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if !myTime.Equal(testingTime) { 57 t.Errorf("expected time to be equal to %v, got %v", testingTime, myTime) 58 } 59 } 60 61 func TestEmptyStringUnmarshal(t *testing.T) { 62 var myTime Time 63 err := json.Unmarshal([]byte(emptyString), &myTime) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if !myTime.IsZero() { 68 t.Errorf("expected time to be equal to zero value, got %v", myTime) 69 } 70 } 71 72 func TestZeroValueUnmarshal(t *testing.T) { 73 // This test ensures that we can unmarshal any time value that was output 74 // with the current go default value of "0001-01-01T00:00:00Z" 75 var myTime Time 76 err := json.Unmarshal([]byte(`"0001-01-01T00:00:00Z"`), &myTime) 77 if err != nil { 78 t.Fatal(err) 79 } 80 if !myTime.IsZero() { 81 t.Errorf("expected time to be equal to zero value, got %v", myTime) 82 } 83 }