From 0904b1354660001a61f8aefc42c98bf103674528 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 18 Sep 2014 12:48:24 -0400 Subject: [PATCH 1/3] Moved units to be with themselves in common, updated convertUnit and parseLine to be public for testing --- common.go | 38 +++++++++++++++++++++++++++----------- free/main.go | 8 ++++---- 2 files changed, 31 insertions(+), 15 deletions(-) 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..cc8b9e5 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 @@ -30,7 +30,7 @@ func convertUnit(stringUnit string) (jsh.Unit, error) { } } -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) } From a144a70a614057dfb647312fe7cdd73b6ac61073 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 18 Sep 2014 12:58:25 -0400 Subject: [PATCH 2/3] Added tests for free --- free/main_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 free/main_test.go diff --git a/free/main_test.go b/free/main_test.go new file mode 100644 index 0000000..2714907 --- /dev/null +++ b/free/main_test.go @@ -0,0 +1,64 @@ +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) + + // 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) + } +} From 72c638432f2e7eb4d706ffa03e2559ceabd0c984 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 18 Sep 2014 13:03:44 -0400 Subject: [PATCH 3/3] Added a test for the empty unit, moved bad unit test into a separate test function --- free/main.go | 2 +- free/main_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/free/main.go b/free/main.go index cc8b9e5..d1c8a43 100644 --- a/free/main.go +++ b/free/main.go @@ -23,7 +23,7 @@ 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)) diff --git a/free/main_test.go b/free/main_test.go index 2714907..c60e170 100644 --- a/free/main_test.go +++ b/free/main_test.go @@ -24,11 +24,23 @@ func TestConvertUnit(t *testing.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) {