Simplify ShortDur code and tests

This commit is contained in:
Deluan 2024-02-02 21:07:27 -05:00
parent 61257f89d2
commit 79a4d8f6ad
2 changed files with 19 additions and 33 deletions

View File

@ -19,11 +19,6 @@ func ShortDur(d time.Duration) string {
default:
s = d.String()
}
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}
return s
s = strings.TrimSuffix(s, "0s")
return strings.TrimSuffix(s, "0m")
}

View File

@ -7,29 +7,20 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("ShortDur", func() {
It("formats microseconds", func() {
Expect(ShortDur(9 * time.Microsecond)).To(Equal("9µs"))
Expect(ShortDur(2 * time.Microsecond)).To(Equal("2µs"))
})
It("rounds milliseconds", func() {
Expect(ShortDur(5*time.Millisecond + 10*time.Microsecond)).To(Equal("5ms"))
Expect(ShortDur(5*time.Millisecond + 240*time.Microsecond)).To(Equal("5.2ms"))
})
It("rounds seconds", func() {
Expect(ShortDur(time.Second + 263*time.Millisecond)).To(Equal("1.26s"))
})
It("removes 0 secs", func() {
Expect(ShortDur(4 * time.Minute)).To(Equal("4m"))
})
It("rounds to seconds", func() {
Expect(ShortDur(4*time.Minute + 3*time.Second)).To(Equal("4m3s"))
})
It("removes 0 minutes", func() {
Expect(ShortDur(4 * time.Hour)).To(Equal("4h"))
})
It("round big durations to the minute", func() {
Expect(ShortDur(4*time.Hour + 2*time.Minute + 5*time.Second + 200*time.Millisecond)).
To(Equal("4h2m"))
})
})
var _ = DescribeTable("ShortDur",
func(d time.Duration, expected string) {
Expect(ShortDur(d)).To(Equal(expected))
},
Entry("1ns", 1*time.Nanosecond, "1ns"),
Entry("9µs", 9*time.Microsecond, "9µs"),
Entry("2ms", 2*time.Millisecond, "2ms"),
Entry("5ms", 5*time.Millisecond, "5ms"),
Entry("5.2ms", 5*time.Millisecond+240*time.Microsecond, "5.2ms"),
Entry("1s", 1*time.Second, "1s"),
Entry("1.26s", 1*time.Second+263*time.Millisecond, "1.26s"),
Entry("4m", 4*time.Minute, "4m"),
Entry("4m3s", 4*time.Minute+3*time.Second, "4m3s"),
Entry("4h", 4*time.Hour, "4h"),
Entry("4h", 4*time.Hour+2*time.Second, "4h"),
Entry("4h2m", 4*time.Hour+2*time.Minute+5*time.Second+200*time.Millisecond, "4h2m"),
)