Skip to content

🎧 Your Personal Streaming Service

Simplify ShortDur code and tests

Deluan79a4d8f6ad31
diff --git a/log/formatters.go b/log/formatters.go
index 0e34cad7e..5cc1ca410 100644
--- a/log/formatters.go
+++ b/log/formatters.go
@@ -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")
}
diff --git a/log/formatters_test.go b/log/formatters_test.go
index 61967dc25..087459b5c 100644
--- a/log/formatters_test.go
+++ b/log/formatters_test.go
@@ -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"),
+)