Browse Source

Clean up the tests

Oz Tiram 5 years ago
parent
commit
74944e1cb3
1 changed files with 72 additions and 61 deletions
  1. 72 61
      gupta_test.go

+ 72 - 61
gupta_test.go

@@ -8,9 +8,10 @@ import (
 func TestHello(t *testing.T) {
 	got := Hello()
 	want := fmt.Sprintf("Welcome to gupta version %s", version)
+	name := "welcome string"
 
 	if got != want {
-		t.Errorf("got %q want %q", got, want)
+		t.Errorf("got %s %s want %s", name, got, want)
 	}
 }
 
@@ -31,72 +32,82 @@ func TestRunGupta(t *testing.T) {
 	// it to json ...
 }
 
-func TestGuptaHasReport(t *testing.T) {
-	// test we can get some report struct from gupta
-	report := GuptaReport{
-		cpuLoad:     0.2,
-		cpuUsage:    0.2,
-		memoryUsage: 70.0,
-	}
-
-	got := report.GetCPULoad()
-	want := "0.20"
-
-	if got != want {
-		t.Errorf("got %v want %v", got, want)
-	}
-
-}
-
-func TestGetCPUUsage(t *testing.T) {
-	cpustat := CPUStat{}
-	cpustat.ReadInfo([]string{
-		"4705",
-		"356",
-		"584",
-		"3699",
-		"23",
-		"23",
-		"0",
-		"0",
-		"0",
-		"0"})
-	if cpustat.user != 4705 {
-		t.Errorf("Got cpustat.user %v want %v", cpustat.user, 4705)
-	}
-}
-
-func TestNewCPUUsage(t *testing.T) {
-	//               0    1    2     3     4     5      6     7       8     9     10
-	//               cpu user nice system idle iowait  irq  softirq steal guest guest_nice
-	procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
-	cpustat := NewCPUStat(procstatLine)
+func TestCPUStat(t *testing.T) {
 
-	if cpustat.user != 4705 {
-		t.Errorf("got %v want %v", cpustat.user, 4705)
-	}
-}
+	t.Run("Test that gupta hast Report", func(t *testing.T) {
+		// test we can get some report struct from gupta
+		report := GuptaReport{
+			cpuLoad:     0.2,
+			cpuUsage:    0.2,
+			memoryUsage: 70.0,
+		}
 
-func TestTotalCPUTime(t *testing.T) {
-	procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
-	cpustat := NewCPUStat(procstatLine)
+		got := report.GetCPULoad()
+		want := "0.20"
+		name := "CPULoad"
 
-	got := cpustat.TotalTime()
-	want := cpustat.user + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
-		cpustat.irq + cpustat.softirq + cpustat.steal
+		if got != want {
+			t.Errorf("got %s %s want %s", name, got, want)
+		}
 
-	if got != want {
-		t.Errorf("got total time %v want %v", got, want)
-	}
+	})
+
+	t.Run("Test the CPUStat can read info", func(t *testing.T) {
+		cpustat := CPUStat{}
+		cpustat.ReadInfo([]string{
+			"4705",
+			"356",
+			"584",
+			"3699",
+			"23",
+			"23",
+			"0",
+			"0",
+			"0",
+			"0"})
+		got := cpustat.user
+		want := uint64(4705)
+		name := "cpustat.user"
+		assert(t, name, got, want)
+	})
+
+	t.Run("Test the NewCPUStat can create an instance", func(t *testing.T) {
+		//               0    1    2     3     4     5      6     7       8     9     10
+		//               cpu user nice system idle iowait  irq  softirq steal guest guest_nice
+		procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
+		cpustat := NewCPUStat(procstatLine)
+
+		got := cpustat.user
+		want := uint64(4705)
+		name := "cpustat.user"
+		assert(t, name, got, want)
+	})
+
+	t.Run("Test that Total CPUTime calculates properly", func(t *testing.T) {
+		procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
+		cpustat := NewCPUStat(procstatLine)
+
+		got := cpustat.TotalTime()
+		want := cpustat.user + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
+			cpustat.irq + cpustat.softirq + cpustat.steal
+		name := "total time"
+		assert(t, name, got, want)
+	})
+
+	t.Run("Test that IdleTime calculates properly", func(t *testing.T) {
+		procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
+		cpustat := NewCPUStat(procstatLine)
+
+		got := cpustat.IdleTime()
+		want := uint64(3722)
+		name := "idle time"
+		assert(t, name, got, want)
+
+	})
 }
 
-func TestIdleTime(t *testing.T) {
-	procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
-	cpustat := NewCPUStat(procstatLine)
-
-	got := cpustat.IdleTime()
-	want := uint64(3722)
+func assert(t *testing.T, name string, got, want uint64) {
 	if got != want {
-		t.Errorf("got idle time %v want %v", got, want)
+		t.Errorf("got %s %v want %v", name, got, want)
 	}
 }