Browse code

pkg/units: Updated tests with unit constants

Also, now that I was at it, gave them a small refactor.

Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)

Francisco Carriedo authored on 2014/07/27 11:48:02
Showing 2 changed files
... ...
@@ -44,9 +44,3 @@ func TestHumanDuration(t *testing.T) {
44 44
 	assertEquals(t, "2.010959 years", HumanDuration(24*month+2*week))
45 45
 	assertEquals(t, "3.164384 years", HumanDuration(3*year+2*month))
46 46
 }
47
-
48
-func assertEquals(t *testing.T, expected, actual interface{}) {
49
-	if expected != actual {
50
-		t.Errorf("Expected '%s' but got '%s'", expected, actual)
51
-	}
52
-}
... ...
@@ -1,6 +1,9 @@
1 1
 package units
2 2
 
3 3
 import (
4
+	"reflect"
5
+	"runtime"
6
+	"strings"
4 7
 	"testing"
5 8
 )
6 9
 
... ...
@@ -9,85 +12,87 @@ func TestHumanSize(t *testing.T) {
9 9
 	assertEquals(t, "1.024 kB", HumanSize(1024))
10 10
 	assertEquals(t, "1 MB", HumanSize(1000000))
11 11
 	assertEquals(t, "1.049 MB", HumanSize(1048576))
12
-	assertEquals(t, "2 MB", HumanSize(2*1000*1000))
13
-	assertEquals(t, "3.42 GB", HumanSize(3.42*1000*1000*1000))
14
-	assertEquals(t, "5.372 TB", HumanSize(5.372*1000*1000*1000*1000))
15
-	assertEquals(t, "2.22 PB", HumanSize(2.22*1000*1000*1000*1000*1000))
16
-	assertEquals(t, "2.22 EB", HumanSize(2.22*1000*1000*1000*1000*1000*1000))
17
-	assertEquals(t, "7.707 EB", HumanSize(7.707*1000*1000*1000*1000*1000*1000))
12
+	assertEquals(t, "2 MB", HumanSize(2*MB))
13
+	assertEquals(t, "3.42 GB", HumanSize(3.42*GB))
14
+	assertEquals(t, "5.372 TB", HumanSize(5.372*TB))
15
+	assertEquals(t, "2.22 PB", HumanSize(2.22*PB))
18 16
 }
19 17
 
20 18
 func TestFromHumanSize(t *testing.T) {
21
-	assertFromHumanSize(t, "32", false, 32)
22
-	assertFromHumanSize(t, "32b", false, 32)
23
-	assertFromHumanSize(t, "32B", false, 32)
24
-	assertFromHumanSize(t, "32k", false, 32*1000)
25
-	assertFromHumanSize(t, "32K", false, 32*1000)
26
-	assertFromHumanSize(t, "32kb", false, 32*1000)
27
-	assertFromHumanSize(t, "32Kb", false, 32*1000)
28
-	assertFromHumanSize(t, "32Mb", false, 32*1000*1000)
29
-	assertFromHumanSize(t, "32Gb", false, 32*1000*1000*1000)
30
-	assertFromHumanSize(t, "32Tb", false, 32*1000*1000*1000*1000)
31
-	assertFromHumanSize(t, "8Pb", false, 8*1000*1000*1000*1000*1000)
19
+	assertSuccessEquals(t, 32, FromHumanSize, "32")
20
+	assertSuccessEquals(t, 32, FromHumanSize, "32b")
21
+	assertSuccessEquals(t, 32, FromHumanSize, "32B")
22
+	assertSuccessEquals(t, 32*KB, FromHumanSize, "32k")
23
+	assertSuccessEquals(t, 32*KB, FromHumanSize, "32K")
24
+	assertSuccessEquals(t, 32*KB, FromHumanSize, "32kb")
25
+	assertSuccessEquals(t, 32*KB, FromHumanSize, "32Kb")
26
+	assertSuccessEquals(t, 32*MB, FromHumanSize, "32Mb")
27
+	assertSuccessEquals(t, 32*GB, FromHumanSize, "32Gb")
28
+	assertSuccessEquals(t, 32*TB, FromHumanSize, "32Tb")
29
+	assertSuccessEquals(t, 32*PB, FromHumanSize, "32Pb")
32 30
 
33
-	assertFromHumanSize(t, "", true, -1)
34
-	assertFromHumanSize(t, "hello", true, -1)
35
-	assertFromHumanSize(t, "-32", true, -1)
36
-	assertFromHumanSize(t, " 32 ", true, -1)
37
-	assertFromHumanSize(t, "32 mb", true, -1)
38
-	assertFromHumanSize(t, "32m b", true, -1)
39
-	assertFromHumanSize(t, "32bm", true, -1)
31
+	assertError(t, FromHumanSize, "")
32
+	assertError(t, FromHumanSize, "hello")
33
+	assertError(t, FromHumanSize, "-32")
34
+	assertError(t, FromHumanSize, "32.3")
35
+	assertError(t, FromHumanSize, " 32 ")
36
+	assertError(t, FromHumanSize, "32.3Kb")
37
+	assertError(t, FromHumanSize, "32 mb")
38
+	assertError(t, FromHumanSize, "32m b")
39
+	assertError(t, FromHumanSize, "32bm")
40 40
 }
41 41
 
42
-func assertFromHumanSize(t *testing.T, size string, expectError bool, expectedBytes int64) {
43
-	actualBytes, err := FromHumanSize(size)
44
-	if (err != nil) && !expectError {
45
-		t.Errorf("Unexpected error parsing '%s': %s", size, err)
46
-	}
47
-	if (err == nil) && expectError {
48
-		t.Errorf("Expected to get an error parsing '%s', but got none (bytes=%d)", size, actualBytes)
49
-	}
50
-	if actualBytes != expectedBytes {
51
-		t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes)
42
+func TestRAMInBytes(t *testing.T) {
43
+	assertSuccessEquals(t, 32, RAMInBytes, "32")
44
+	assertSuccessEquals(t, 32, RAMInBytes, "32b")
45
+	assertSuccessEquals(t, 32, RAMInBytes, "32B")
46
+	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32k")
47
+	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K")
48
+	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb")
49
+	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb")
50
+	assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb")
51
+	assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb")
52
+	assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb")
53
+	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32Pb")
54
+	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32PB")
55
+	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32P")
56
+
57
+	assertError(t, RAMInBytes, "")
58
+	assertError(t, RAMInBytes, "hello")
59
+	assertError(t, RAMInBytes, "-32")
60
+	assertError(t, RAMInBytes, "32.3")
61
+	assertError(t, RAMInBytes, " 32 ")
62
+	assertError(t, RAMInBytes, "32.3Kb")
63
+	assertError(t, RAMInBytes, "32 mb")
64
+	assertError(t, RAMInBytes, "32m b")
65
+	assertError(t, RAMInBytes, "32bm")
66
+}
67
+
68
+func assertEquals(t *testing.T, expected, actual interface{}) {
69
+	if expected != actual {
70
+		t.Errorf("Expected '%v' but got '%v'", expected, actual)
52 71
 	}
53 72
 }
54 73
 
55
-func TestRAMInBytes(t *testing.T) {
56
-	assertRAMInBytes(t, "32", false, 32)
57
-	assertRAMInBytes(t, "32b", false, 32)
58
-	assertRAMInBytes(t, "32B", false, 32)
59
-	assertRAMInBytes(t, "32k", false, 32*1024)
60
-	assertRAMInBytes(t, "32K", false, 32*1024)
61
-	assertRAMInBytes(t, "32kb", false, 32*1024)
62
-	assertRAMInBytes(t, "32Kb", false, 32*1024)
63
-	assertRAMInBytes(t, "32Mb", false, 32*1024*1024)
64
-	assertRAMInBytes(t, "32MB", false, 32*1024*1024)
65
-	assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024)
66
-	assertRAMInBytes(t, "32G", false, 32*1024*1024*1024)
67
-	assertRAMInBytes(t, "32GB", false, 32*1024*1024*1024)
68
-	assertRAMInBytes(t, "32Tb", false, 32*1024*1024*1024*1024)
69
-	assertRAMInBytes(t, "8Pb", false, 8*1024*1024*1024*1024*1024)
70
-	assertRAMInBytes(t, "8PB", false, 8*1024*1024*1024*1024*1024)
71
-	assertRAMInBytes(t, "8P", false, 8*1024*1024*1024*1024*1024)
74
+// func that maps to the parse function signatures as testing abstraction
75
+type parseFn func(string) (int64, error)
72 76
 
73
-	assertRAMInBytes(t, "", true, -1)
74
-	assertRAMInBytes(t, "hello", true, -1)
75
-	assertRAMInBytes(t, "-32", true, -1)
76
-	assertRAMInBytes(t, " 32 ", true, -1)
77
-	assertRAMInBytes(t, "32 mb", true, -1)
78
-	assertRAMInBytes(t, "32m b", true, -1)
79
-	assertRAMInBytes(t, "32bm", true, -1)
77
+// Define 'String()' for pretty-print
78
+func (fn parseFn) String() string {
79
+	fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
80
+	return fnName[strings.LastIndex(fnName, ".")+1:]
80 81
 }
81 82
 
82
-func assertRAMInBytes(t *testing.T, size string, expectError bool, expectedBytes int64) {
83
-	actualBytes, err := RAMInBytes(size)
84
-	if (err != nil) && !expectError {
85
-		t.Errorf("Unexpected error parsing '%s': %s", size, err)
86
-	}
87
-	if (err == nil) && expectError {
88
-		t.Errorf("Expected to get an error parsing '%s', but got none (bytes=%d)", size, actualBytes)
83
+func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
84
+	res, err := fn(arg)
85
+	if err != nil || res != expected {
86
+		t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
89 87
 	}
90
-	if actualBytes != expectedBytes {
91
-		t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes)
88
+}
89
+
90
+func assertError(t *testing.T, fn parseFn, arg string) {
91
+	res, err := fn(arg)
92
+	if err == nil && res != -1 {
93
+		t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
92 94
 	}
93 95
 }