pkg/units: Increased test coverage. Closes #7159.
Increased coverage: * Added test cases to size_test.go * Added coverage for duration.go Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)
This commit is contained in:
parent
fdff0c00b0
commit
23f29beb82
2 changed files with 62 additions and 13 deletions
52
units/duration_test.go
Normal file
52
units/duration_test.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package units
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHumanDuration(t *testing.T) {
|
||||||
|
// Useful duration abstractions
|
||||||
|
day := 24 * time.Hour
|
||||||
|
week := 7 * day
|
||||||
|
month := 30 * day
|
||||||
|
year := 365 * day
|
||||||
|
|
||||||
|
assertEquals(t, "Less than a second", HumanDuration(450*time.Millisecond))
|
||||||
|
assertEquals(t, "47 seconds", HumanDuration(47*time.Second))
|
||||||
|
assertEquals(t, "About a minute", HumanDuration(1*time.Minute))
|
||||||
|
assertEquals(t, "3 minutes", HumanDuration(3*time.Minute))
|
||||||
|
assertEquals(t, "35 minutes", HumanDuration(35*time.Minute))
|
||||||
|
assertEquals(t, "35 minutes", HumanDuration(35*time.Minute+40*time.Second))
|
||||||
|
assertEquals(t, "About an hour", HumanDuration(1*time.Hour))
|
||||||
|
assertEquals(t, "About an hour", HumanDuration(1*time.Hour+45*time.Minute))
|
||||||
|
assertEquals(t, "3 hours", HumanDuration(3*time.Hour))
|
||||||
|
assertEquals(t, "3 hours", HumanDuration(3*time.Hour+59*time.Minute))
|
||||||
|
assertEquals(t, "4 hours", HumanDuration(3*time.Hour+60*time.Minute))
|
||||||
|
assertEquals(t, "24 hours", HumanDuration(24*time.Hour))
|
||||||
|
assertEquals(t, "36 hours", HumanDuration(1*day+12*time.Hour))
|
||||||
|
assertEquals(t, "2 days", HumanDuration(2*day))
|
||||||
|
assertEquals(t, "7 days", HumanDuration(7*day))
|
||||||
|
assertEquals(t, "13 days", HumanDuration(13*day+5*time.Hour))
|
||||||
|
assertEquals(t, "2 weeks", HumanDuration(2*week))
|
||||||
|
assertEquals(t, "2 weeks", HumanDuration(2*week+4*day))
|
||||||
|
assertEquals(t, "3 weeks", HumanDuration(3*week))
|
||||||
|
assertEquals(t, "4 weeks", HumanDuration(4*week))
|
||||||
|
assertEquals(t, "4 weeks", HumanDuration(4*week+3*day))
|
||||||
|
assertEquals(t, "4 weeks", HumanDuration(1*month))
|
||||||
|
assertEquals(t, "6 weeks", HumanDuration(1*month+2*week))
|
||||||
|
assertEquals(t, "8 weeks", HumanDuration(2*month))
|
||||||
|
assertEquals(t, "3 months", HumanDuration(3*month+1*week))
|
||||||
|
assertEquals(t, "5 months", HumanDuration(5*month+2*week))
|
||||||
|
assertEquals(t, "13 months", HumanDuration(13*month))
|
||||||
|
assertEquals(t, "23 months", HumanDuration(23*month))
|
||||||
|
assertEquals(t, "24 months", HumanDuration(24*month))
|
||||||
|
assertEquals(t, "2.010959 years", HumanDuration(24*month+2*week))
|
||||||
|
assertEquals(t, "3.164384 years", HumanDuration(3*year+2*month))
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertEquals(t *testing.T, expected, actual interface{}) {
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("Expected '%s' but got '%s'", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,20 @@
|
||||||
package units
|
package units
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHumanSize(t *testing.T) {
|
func TestHumanSize(t *testing.T) {
|
||||||
|
assertEquals(t, "1 kB", HumanSize(1000))
|
||||||
size := strings.Trim(HumanSize(1000), " \t")
|
assertEquals(t, "1.024 kB", HumanSize(1024))
|
||||||
expect := "1 kB"
|
assertEquals(t, "1 MB", HumanSize(1000000))
|
||||||
if size != expect {
|
assertEquals(t, "1.049 MB", HumanSize(1048576))
|
||||||
t.Errorf("1000 -> expected '%s', got '%s'", expect, size)
|
assertEquals(t, "2 MB", HumanSize(2*1000*1000))
|
||||||
}
|
assertEquals(t, "3.42 GB", HumanSize(3.42*1000*1000*1000))
|
||||||
|
assertEquals(t, "5.372 TB", HumanSize(5.372*1000*1000*1000*1000))
|
||||||
size = strings.Trim(HumanSize(1024), " \t")
|
assertEquals(t, "2.22 PB", HumanSize(2.22*1000*1000*1000*1000*1000))
|
||||||
expect = "1.024 kB"
|
assertEquals(t, "2.22 EB", HumanSize(2.22*1000*1000*1000*1000*1000*1000))
|
||||||
if size != expect {
|
assertEquals(t, "7.707 EB", HumanSize(7.707*1000*1000*1000*1000*1000*1000))
|
||||||
t.Errorf("1024 -> expected '%s', got '%s'", expect, size)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromHumanSize(t *testing.T) {
|
func TestFromHumanSize(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue