Added tests for free
This commit is contained in:
parent
0904b13546
commit
a144a70a61
64
free/main_test.go
Normal file
64
free/main_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user