go.temporal.io/server@v1.23.0/common/primitives/timestamp/parse_duration_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2021 Temporal Technologies Inc. All rights reserved. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 package timestamp 24 25 import ( 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/suite" 30 ) 31 32 type ParseDurationSuite struct { 33 suite.Suite 34 } 35 36 func TestParseDurationSuite(t *testing.T) { 37 suite.Run(t, new(ParseDurationSuite)) 38 } 39 40 func (s *ParseDurationSuite) TestParseDuration() { 41 for _, c := range []struct { 42 input string 43 expected time.Duration // -1 means error 44 }{ 45 {"1h", time.Hour}, 46 {"3m30s", 3*time.Minute + 30*time.Second}, 47 {"1d", 24 * time.Hour}, 48 {"3d", 3 * 24 * time.Hour}, 49 {"5d6h15m", 5*24*time.Hour + 6*time.Hour + 15*time.Minute}, 50 {"5.25d15m", 5*24*time.Hour + 6*time.Hour + 15*time.Minute}, 51 {".5d", 12 * time.Hour}, 52 {"-10d12.25h", -(10*24*time.Hour + 12*time.Hour + 15*time.Minute)}, 53 {"3m2h1d", 3*time.Minute + 2*time.Hour + 1*24*time.Hour}, 54 {"8m7h6d5d4h3m", 8*time.Minute + 7*time.Hour + 6*24*time.Hour + 5*24*time.Hour + 4*time.Hour + 3*time.Minute}, 55 {"7", -1}, // error 56 {"", -1}, // error 57 {"10000000h", -1}, // error out of bounds 58 } { 59 got, err := ParseDuration(c.input) 60 if c.expected == -1 { 61 s.Error(err) 62 } else { 63 s.Equal(c.expected, got) 64 } 65 } 66 } 67 68 func (s *ParseDurationSuite) TestParseDurationDefaultDays() { 69 for _, c := range []struct { 70 input string 71 expected time.Duration // -1 means error 72 }{ 73 {"3m30s", 3*time.Minute + 30*time.Second}, 74 {"7", 7 * 24 * time.Hour}, 75 {"7.5", 7*24*time.Hour + 12*time.Hour}, 76 {".75", 18 * time.Hour}, 77 {"2.75", 2*24*time.Hour + 18*time.Hour}, 78 {"", -1}, // error 79 } { 80 got, err := ParseDurationDefaultDays(c.input) 81 if c.expected == -1 { 82 s.Error(err) 83 } else { 84 s.Equal(c.expected, got) 85 } 86 } 87 } 88 89 func (s *ParseDurationSuite) TestParseDurationDefaultSeconds() { 90 for _, c := range []struct { 91 input string 92 expected time.Duration // -1 means error 93 }{ 94 {"3m30s", 3*time.Minute + 30*time.Second}, 95 {"7", 7 * time.Second}, 96 {"", -1}, // error 97 } { 98 got, err := ParseDurationDefaultSeconds(c.input) 99 if c.expected == -1 { 100 s.Error(err) 101 } else { 102 s.Equal(c.expected, got) 103 } 104 } 105 } 106 107 func (s *ParseDurationSuite) TestParseHHMMSSDuration() { 108 for _, c := range []struct { 109 input string 110 expected time.Duration // -1 means error 111 }{ 112 {"1:00:00", 1 * time.Hour}, 113 {"123:05:10", 123*time.Hour + 5*time.Minute + 10*time.Second}, 114 {"00:05:10", 5*time.Minute + 10*time.Second}, 115 {"-12:05:10", -1}, 116 {"12:61:10", -1}, 117 {"12:05:61", -1}, 118 {"", -1}, 119 } { 120 got, err := ParseHHMMSSDuration(c.input) 121 if c.expected == -1 { 122 s.Error(err) 123 } else { 124 s.Equal(c.expected, got) 125 } 126 } 127 }