github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/duration/duration_test.go (about) 1 /* 2 Copyright 2018 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 duration 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 func TestHumanDuration(t *testing.T) { 25 tests := []struct { 26 d time.Duration 27 want string 28 }{ 29 {d: time.Second, want: "1s"}, 30 {d: 70 * time.Second, want: "70s"}, 31 {d: 190 * time.Second, want: "3m10s"}, 32 {d: 70 * time.Minute, want: "70m"}, 33 {d: 47 * time.Hour, want: "47h"}, 34 {d: 49 * time.Hour, want: "2d1h"}, 35 {d: (8*24 + 2) * time.Hour, want: "8d"}, 36 {d: (367 * 24) * time.Hour, want: "367d"}, 37 {d: (365*2*24 + 25) * time.Hour, want: "2y1d"}, 38 {d: (365*8*24 + 2) * time.Hour, want: "8y"}, 39 } 40 for _, tt := range tests { 41 t.Run(tt.d.String(), func(t *testing.T) { 42 if got := HumanDuration(tt.d); got != tt.want { 43 t.Errorf("HumanDuration() = %v, want %v", got, tt.want) 44 } 45 }) 46 } 47 } 48 49 func TestHumanDurationBoundaries(t *testing.T) { 50 tests := []struct { 51 d time.Duration 52 want string 53 }{ 54 {d: -2 * time.Second, want: "<invalid>"}, 55 {d: -2*time.Second + 1, want: "0s"}, 56 {d: 0, want: "0s"}, 57 {d: time.Second - time.Millisecond, want: "0s"}, 58 {d: 2*time.Minute - time.Millisecond, want: "119s"}, 59 {d: 2 * time.Minute, want: "2m"}, 60 {d: 2*time.Minute + time.Second, want: "2m1s"}, 61 {d: 10*time.Minute - time.Millisecond, want: "9m59s"}, 62 {d: 10 * time.Minute, want: "10m"}, 63 {d: 10*time.Minute + time.Second, want: "10m"}, 64 {d: 3*time.Hour - time.Millisecond, want: "179m"}, 65 {d: 3 * time.Hour, want: "3h"}, 66 {d: 3*time.Hour + time.Minute, want: "3h1m"}, 67 {d: 8*time.Hour - time.Millisecond, want: "7h59m"}, 68 {d: 8 * time.Hour, want: "8h"}, 69 {d: 8*time.Hour + 59*time.Minute, want: "8h"}, 70 {d: 2*24*time.Hour - time.Millisecond, want: "47h"}, 71 {d: 2 * 24 * time.Hour, want: "2d"}, 72 {d: 2*24*time.Hour + time.Hour, want: "2d1h"}, 73 {d: 8*24*time.Hour - time.Millisecond, want: "7d23h"}, 74 {d: 8 * 24 * time.Hour, want: "8d"}, 75 {d: 8*24*time.Hour + 23*time.Hour, want: "8d"}, 76 {d: 2*365*24*time.Hour - time.Millisecond, want: "729d"}, 77 {d: 2 * 365 * 24 * time.Hour, want: "2y"}, 78 {d: 2*365*24*time.Hour + 23*time.Hour, want: "2y"}, 79 {d: 2*365*24*time.Hour + 23*time.Hour + 59*time.Minute, want: "2y"}, 80 {d: 2*365*24*time.Hour + 24*time.Hour - time.Millisecond, want: "2y"}, 81 {d: 2*365*24*time.Hour + 24*time.Hour, want: "2y1d"}, 82 {d: 3 * 365 * 24 * time.Hour, want: "3y"}, 83 {d: 4 * 365 * 24 * time.Hour, want: "4y"}, 84 {d: 5 * 365 * 24 * time.Hour, want: "5y"}, 85 {d: 6 * 365 * 24 * time.Hour, want: "6y"}, 86 {d: 7 * 365 * 24 * time.Hour, want: "7y"}, 87 {d: 8*365*24*time.Hour - time.Millisecond, want: "7y364d"}, 88 {d: 8 * 365 * 24 * time.Hour, want: "8y"}, 89 {d: 8*365*24*time.Hour + 364*24*time.Hour, want: "8y"}, 90 {d: 9 * 365 * 24 * time.Hour, want: "9y"}, 91 } 92 for _, tt := range tests { 93 t.Run(tt.d.String(), func(t *testing.T) { 94 if got := HumanDuration(tt.d); got != tt.want { 95 t.Errorf("HumanDuration() = %v, want %v", got, tt.want) 96 } 97 }) 98 } 99 } 100 101 func TestShortHumanDurationBoundaries(t *testing.T) { 102 tests := []struct { 103 d time.Duration 104 want string 105 }{ 106 {d: -2 * time.Second, want: "<invalid>"}, 107 {d: -2*time.Second + 1, want: "0s"}, 108 {d: 0, want: "0s"}, 109 {d: time.Second - time.Millisecond, want: "0s"}, 110 {d: time.Second, want: "1s"}, 111 {d: 2*time.Second - time.Millisecond, want: "1s"}, 112 {d: time.Minute - time.Millisecond, want: "59s"}, 113 {d: time.Minute, want: "1m"}, 114 {d: 2*time.Minute - time.Millisecond, want: "1m"}, 115 {d: time.Hour - time.Millisecond, want: "59m"}, 116 {d: time.Hour, want: "1h"}, 117 {d: 2*time.Hour - time.Millisecond, want: "1h"}, 118 {d: 24*time.Hour - time.Millisecond, want: "23h"}, 119 {d: 24 * time.Hour, want: "1d"}, 120 {d: 2*24*time.Hour - time.Millisecond, want: "1d"}, 121 {d: 365*24*time.Hour - time.Millisecond, want: "364d"}, 122 {d: 365 * 24 * time.Hour, want: "1y"}, 123 {d: 2*365*24*time.Hour - time.Millisecond, want: "1y"}, 124 {d: 2 * 365 * 24 * time.Hour, want: "2y"}, 125 } 126 for _, tt := range tests { 127 t.Run(tt.d.String(), func(t *testing.T) { 128 if got := ShortHumanDuration(tt.d); got != tt.want { 129 t.Errorf("ShortHumanDuration() = %v, want %v", got, tt.want) 130 } 131 }) 132 } 133 }