Browse Source

Added tests for free

Fredric Silberberg 5 years ago
parent
commit
a144a70a61
1 changed files with 64 additions and 0 deletions
  1. 64
    0
      free/main_test.go

+ 64
- 0
free/main_test.go View File

@@ -0,0 +1,64 @@
1
+package main
2
+
3
+import (
4
+	"jsh"
5
+	"testing"
6
+	"reflect"
7
+)
8
+
9
+func testUnitError(expected jsh.Unit, t *testing.T) {
10
+	actual, error := ConvertUnit(expected.ToString())
11
+	if error != nil {
12
+		t.Error(error)
13
+	}
14
+	if actual != expected {
15
+		t.Errorf("Conversion error: expected %s, received %s", expected.ToString(), actual.ToString())
16
+	}
17
+}
18
+
19
+func TestConvertUnit(t *testing.T) {
20
+	// Good unit conversions
21
+	testUnitError(jsh.B, t)
22
+	testUnitError(jsh.KB, t)
23
+	testUnitError(jsh.MB, t)
24
+	testUnitError(jsh.GB, t)
25
+	testUnitError(jsh.TB, t)
26
+
27
+	// Testing a bad unit conversion
28
+	actual, error := ConvertUnit("pB")
29
+	if error == nil {
30
+		t.Error("Expected an error for unit pB, received value of ", actual)
31
+	}
32
+}
33
+
34
+func TestParseGoodLine(t *testing.T) {
35
+	goodLine := "MemTotal:       16370344 kB"
36
+	expectedStat := jsh.MemStat { 16370344, jsh.KB }
37
+	expectedKey := "MemTotal"
38
+	key, stat, err := ParseLine(goodLine)
39
+	if err != nil {
40
+		t.Error(err)
41
+	}
42
+	if key != expectedKey {
43
+		t.Errorf("Expected key %s, got key %s", expectedKey, key) 
44
+	}
45
+	if !reflect.DeepEqual(expectedStat, stat) {
46
+		t.Error("Expected stat ", expectedStat, " got ", stat)
47
+	}
48
+}
49
+
50
+func TestParseBadUnit(t *testing.T) {
51
+	badLine := "MemTotal:       16370344 pB"
52
+	key, stat, err := ParseLine(badLine)
53
+	if err == nil {
54
+		t.Error("Expected error, received key ", key, " and stat ", stat)
55
+	}
56
+}
57
+
58
+func TestParseBadNumber(t *testing.T) {
59
+	badLine := "MemTotal:       1637034p pB"
60
+	key, stat, err := ParseLine(badLine)
61
+	if err == nil {
62
+		t.Error("Expected error, received key ", key, " and stat ", stat)
63
+	}
64
+}

Loading…
Cancel
Save