Browse Source

Merge branch 'feature/free-testing' into 'master'

Feature/free testing

Moved functions around in common to keep functions related to each type together. Added in some tests for free, since I said we should do that and I hadn't.

See merge request !2
Fredric Silberberg 6 years ago
parent
commit
f11cbeaf22
3 changed files with 108 additions and 16 deletions
  1. 27
    11
      common.go
  2. 5
    5
      free/main.go
  3. 76
    0
      free/main_test.go

+ 27
- 11
common.go View File

@@ -12,17 +12,6 @@ type JshFrame struct {
12 12
 	StdErr interface{}
13 13
 }
14 14
 
15
-// Size prefixes as integers, using binary representation
16
-type Unit int
17
-
18
-const (
19
-	B  Unit = 2 ^ 0
20
-	KB Unit = 2 ^ 10
21
-	MB Unit = 2 ^ 20
22
-	GB Unit = 2 ^ 30
23
-	TB Unit = 2 ^ 40
24
-)
25
-
26 15
 // Converts a JshFrame into a JSON string
27 16
 func (j JshFrame) ToJson() *string {
28 17
 	jsonOut, err := json.Marshal(j)
@@ -54,3 +43,30 @@ func OutputFrames(queue chan *JshFrame, done chan bool) {
54 43
 		}
55 44
 	}
56 45
 }
46
+
47
+// Size prefixes as integers, using binary representation
48
+type Unit int
49
+
50
+const (
51
+	B  Unit = 2 ^ 0
52
+	KB Unit = 2 ^ 10
53
+	MB Unit = 2 ^ 20
54
+	GB Unit = 2 ^ 30
55
+	TB Unit = 2 ^ 40
56
+)
57
+
58
+func (u Unit) ToString() string {
59
+	if u == B {
60
+		return "B"
61
+	} else if u == KB {
62
+		return "kB"
63
+	} else if u == MB {
64
+		return "mB"
65
+	} else if u == GB {
66
+		return "gB"
67
+	} else if u == TB {
68
+		return "tB"
69
+	} else {
70
+		return fmt.Sprintf("Unknown type, values is %d", u)
71
+	}
72
+}

+ 5
- 5
free/main.go View File

@@ -11,7 +11,7 @@ import (
11 11
 	"strconv"
12 12
 )
13 13
 
14
-func convertUnit(stringUnit string) (jsh.Unit, error) {
14
+func ConvertUnit(stringUnit string) (jsh.Unit, error) {
15 15
 	switch stringUnit {
16 16
 	case "B":
17 17
 		return jsh.B, nil
@@ -23,14 +23,14 @@ func convertUnit(stringUnit string) (jsh.Unit, error) {
23 23
 		return jsh.GB, nil
24 24
 	case "tB":
25 25
 		return jsh.TB, nil
26
-	case "":
26
+	case "": // On some systems (Arch, for example) bytes are just the empty string
27 27
 		return jsh.B, nil
28 28
 	default:
29 29
 		return 0, errors.New(fmt.Sprintf("Unknown unit: %s\n", stringUnit))
30 30
 	}
31 31
 }
32 32
 
33
-func parseLine(line string) (string, jsh.MemStat, error) {
33
+func ParseLine(line string) (string, jsh.MemStat, error) {
34 34
 	// Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
35 35
 	// more whitespace, and possibly the unit
36 36
 	lineRegex := regexp.MustCompile("(?P<key>^[\\w()]+):\\s+(?P<val>\\d+)(\\s+)?(?P<unit>\\w+)?")
@@ -48,7 +48,7 @@ func parseLine(line string) (string, jsh.MemStat, error) {
48 48
 	if err != nil {
49 49
 		return "", jsh.MemStat{}, err
50 50
 	}
51
-	unit, err := convertUnit(matchedVals["unit"])
51
+	unit, err := ConvertUnit(matchedVals["unit"])
52 52
 	if err != nil {
53 53
 		return "", jsh.MemStat{}, err
54 54
 	}
@@ -67,7 +67,7 @@ func parseMemInfo() jsh.JshFrame {
67 67
 	errors := []error{}
68 68
 	// Read in each line of the meminfo file, and place it in the map
69 69
 	for scanner.Scan() {
70
-		key, val, err := parseLine(scanner.Text())
70
+		key, val, err := ParseLine(scanner.Text())
71 71
 		if err != nil {
72 72
 			errors = append(errors, err)
73 73
 		}

+ 76
- 0
free/main_test.go View File

@@ -0,0 +1,76 @@
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
+	// Test the empty unit, because this exists on some systems
28
+	actual, error := ConvertUnit("")
29
+	if error != nil {
30
+		t.Error(error)
31
+	}
32
+	if actual != jsh.B {
33
+		t.Errorf("Conversion error: expected %s, received %s", jsh.B.ToString(), actual.ToString())
34
+	}
35
+}
36
+
37
+func TestConvertBadUnit(t *testing.T) {
38
+	// Testing a bad unit conversion
39
+	actual, error := ConvertUnit("pB")
40
+	if error == nil {
41
+		t.Error("Expected an error for unit pB, received value of ", actual)
42
+	}
43
+
44
+}
45
+
46
+func TestParseGoodLine(t *testing.T) {
47
+	goodLine := "MemTotal:       16370344 kB"
48
+	expectedStat := jsh.MemStat { 16370344, jsh.KB }
49
+	expectedKey := "MemTotal"
50
+	key, stat, err := ParseLine(goodLine)
51
+	if err != nil {
52
+		t.Error(err)
53
+	}
54
+	if key != expectedKey {
55
+		t.Errorf("Expected key %s, got key %s", expectedKey, key) 
56
+	}
57
+	if !reflect.DeepEqual(expectedStat, stat) {
58
+		t.Error("Expected stat ", expectedStat, " got ", stat)
59
+	}
60
+}
61
+
62
+func TestParseBadUnit(t *testing.T) {
63
+	badLine := "MemTotal:       16370344 pB"
64
+	key, stat, err := ParseLine(badLine)
65
+	if err == nil {
66
+		t.Error("Expected error, received key ", key, " and stat ", stat)
67
+	}
68
+}
69
+
70
+func TestParseBadNumber(t *testing.T) {
71
+	badLine := "MemTotal:       1637034p pB"
72
+	key, stat, err := ParseLine(badLine)
73
+	if err == nil {
74
+		t.Error("Expected error, received key ", key, " and stat ", stat)
75
+	}
76
+}

Loading…
Cancel
Save