diff --git a/common.go b/common.go index 964addb..c2cb6ad 100644 --- a/common.go +++ b/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) + } +} diff --git a/free/main.go b/free/main.go index 0849aef..d1c8a43 100644 --- a/free/main.go +++ b/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^[\\w()]+):\\s+(?P\\d+)(\\s+)?(?P\\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) } diff --git a/free/main_test.go b/free/main_test.go new file mode 100644 index 0000000..c60e170 --- /dev/null +++ b/free/main_test.go @@ -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) + } +}