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
This commit is contained in:
commit
f11cbeaf22
38
common.go
38
common.go
@ -12,17 +12,6 @@ type JshFrame struct {
|
||||
StdErr interface{}
|
||||
}
|
||||
|
||||
// Size prefixes as integers, using binary representation
|
||||
type Unit int
|
||||
|
||||
const (
|
||||
B Unit = 2 ^ 0
|
||||
KB Unit = 2 ^ 10
|
||||
MB Unit = 2 ^ 20
|
||||
GB Unit = 2 ^ 30
|
||||
TB Unit = 2 ^ 40
|
||||
)
|
||||
|
||||
// Converts a JshFrame into a JSON string
|
||||
func (j JshFrame) ToJson() *string {
|
||||
jsonOut, err := json.Marshal(j)
|
||||
@ -54,3 +43,30 @@ func OutputFrames(queue chan *JshFrame, done chan bool) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Size prefixes as integers, using binary representation
|
||||
type Unit int
|
||||
|
||||
const (
|
||||
B Unit = 2 ^ 0
|
||||
KB Unit = 2 ^ 10
|
||||
MB Unit = 2 ^ 20
|
||||
GB Unit = 2 ^ 30
|
||||
TB Unit = 2 ^ 40
|
||||
)
|
||||
|
||||
func (u Unit) ToString() string {
|
||||
if u == B {
|
||||
return "B"
|
||||
} else if u == KB {
|
||||
return "kB"
|
||||
} else if u == MB {
|
||||
return "mB"
|
||||
} else if u == GB {
|
||||
return "gB"
|
||||
} else if u == TB {
|
||||
return "tB"
|
||||
} else {
|
||||
return fmt.Sprintf("Unknown type, values is %d", u)
|
||||
}
|
||||
}
|
||||
|
10
free/main.go
10
free/main.go
@ -11,7 +11,7 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func convertUnit(stringUnit string) (jsh.Unit, error) {
|
||||
func ConvertUnit(stringUnit string) (jsh.Unit, error) {
|
||||
switch stringUnit {
|
||||
case "B":
|
||||
return jsh.B, nil
|
||||
@ -23,14 +23,14 @@ func convertUnit(stringUnit string) (jsh.Unit, error) {
|
||||
return jsh.GB, nil
|
||||
case "tB":
|
||||
return jsh.TB, nil
|
||||
case "":
|
||||
case "": // On some systems (Arch, for example) bytes are just the empty string
|
||||
return jsh.B, nil
|
||||
default:
|
||||
return 0, errors.New(fmt.Sprintf("Unknown unit: %s\n", stringUnit))
|
||||
}
|
||||
}
|
||||
|
||||
func parseLine(line string) (string, jsh.MemStat, error) {
|
||||
func ParseLine(line string) (string, jsh.MemStat, error) {
|
||||
// Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
|
||||
// more whitespace, and possibly the unit
|
||||
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) {
|
||||
if err != nil {
|
||||
return "", jsh.MemStat{}, err
|
||||
}
|
||||
unit, err := convertUnit(matchedVals["unit"])
|
||||
unit, err := ConvertUnit(matchedVals["unit"])
|
||||
if err != nil {
|
||||
return "", jsh.MemStat{}, err
|
||||
}
|
||||
@ -67,7 +67,7 @@ func parseMemInfo() jsh.JshFrame {
|
||||
errors := []error{}
|
||||
// Read in each line of the meminfo file, and place it in the map
|
||||
for scanner.Scan() {
|
||||
key, val, err := parseLine(scanner.Text())
|
||||
key, val, err := ParseLine(scanner.Text())
|
||||
if err != nil {
|
||||
errors = append(errors, err)
|
||||
}
|
||||
|
76
free/main_test.go
Normal file
76
free/main_test.go
Normal file
@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"jsh"
|
||||
"testing"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func testUnitError(expected jsh.Unit, t *testing.T) {
|
||||
actual, error := ConvertUnit(expected.ToString())
|
||||
if error != nil {
|
||||
t.Error(error)
|
||||
}
|
||||
if actual != expected {
|
||||
t.Errorf("Conversion error: expected %s, received %s", expected.ToString(), actual.ToString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertUnit(t *testing.T) {
|
||||
// Good unit conversions
|
||||
testUnitError(jsh.B, t)
|
||||
testUnitError(jsh.KB, t)
|
||||
testUnitError(jsh.MB, t)
|
||||
testUnitError(jsh.GB, t)
|
||||
testUnitError(jsh.TB, t)
|
||||
|
||||
// Test the empty unit, because this exists on some systems
|
||||
actual, error := ConvertUnit("")
|
||||
if error != nil {
|
||||
t.Error(error)
|
||||
}
|
||||
if actual != jsh.B {
|
||||
t.Errorf("Conversion error: expected %s, received %s", jsh.B.ToString(), actual.ToString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertBadUnit(t *testing.T) {
|
||||
// Testing a bad unit conversion
|
||||
actual, error := ConvertUnit("pB")
|
||||
if error == nil {
|
||||
t.Error("Expected an error for unit pB, received value of ", actual)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestParseGoodLine(t *testing.T) {
|
||||
goodLine := "MemTotal: 16370344 kB"
|
||||
expectedStat := jsh.MemStat { 16370344, jsh.KB }
|
||||
expectedKey := "MemTotal"
|
||||
key, stat, err := ParseLine(goodLine)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if key != expectedKey {
|
||||
t.Errorf("Expected key %s, got key %s", expectedKey, key)
|
||||
}
|
||||
if !reflect.DeepEqual(expectedStat, stat) {
|
||||
t.Error("Expected stat ", expectedStat, " got ", stat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBadUnit(t *testing.T) {
|
||||
badLine := "MemTotal: 16370344 pB"
|
||||
key, stat, err := ParseLine(badLine)
|
||||
if err == nil {
|
||||
t.Error("Expected error, received key ", key, " and stat ", stat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBadNumber(t *testing.T) {
|
||||
badLine := "MemTotal: 1637034p pB"
|
||||
key, stat, err := ParseLine(badLine)
|
||||
if err == nil {
|
||||
t.Error("Expected error, received key ", key, " and stat ", stat)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user