github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/timeparams_test.go (about) 1 /* 2 * Copyright 2018-2022 The NATS Authors 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 16 package cmd 17 18 import ( 19 "testing" 20 "time" 21 ) 22 23 func TestParseExpiry(t *testing.T) { 24 type testd struct { 25 input string 26 output int64 27 isError bool 28 } 29 tests := []testd{ 30 {"", 0, false}, 31 {"0", 0, false}, 32 {"19-1-6", 0, true}, 33 {"2019-1-6", 0, true}, 34 {"2019-01-6", 0, true}, 35 {"2019-01-06", time.Date(2019, 1, 6, 0, 0, 0, 0, time.UTC).Unix(), false}, 36 {"1m", time.Now().Unix() + 60, false}, 37 {"32m", time.Now().Unix() + 60*32, false}, 38 {"1h", time.Now().Unix() + 60*60, false}, 39 {"3h", time.Now().Unix() + 60*60*3, false}, 40 {"1d", time.Now().AddDate(0, 0, 1).Unix(), false}, 41 {"3d", time.Now().AddDate(0, 0, 3).Unix(), false}, 42 {"1w", time.Now().AddDate(0, 0, 7).Unix(), false}, 43 {"3w", time.Now().AddDate(0, 0, 7*3).Unix(), false}, 44 {"2M", time.Now().AddDate(0, 2, 0).Unix(), false}, 45 {"2y", time.Now().AddDate(2, 0, 0).Unix(), false}, 46 {"2.5d", 0, true}, 47 } 48 for _, d := range tests { 49 v, err := ParseExpiry(d.input) 50 if err != nil && !d.isError { 51 t.Errorf("%s didn't expect error: %v", d.input, err) 52 continue 53 } 54 if err == nil && d.isError { 55 t.Errorf("expected error from %s", d.input) 56 continue 57 } 58 if v != d.output { 59 t.Errorf("%s expected %d but got %d", d.input, d.output, v) 60 } 61 } 62 }