test: added test suite for validating Pominal

This commit is contained in:
Dan Anglin 2020-01-20 09:13:47 +00:00
parent 4c38bfb828
commit 1d79a4c7e6
No known key found for this signature in database
GPG key ID: 7AC2B18EC1D09F27
4 changed files with 107 additions and 9 deletions

View file

@ -18,7 +18,7 @@
package main
var pominalTestCases = []struct {
var newPominalTestCases = []struct {
name string
description string
workSession float64
@ -104,3 +104,47 @@ var pominalTestCases = []struct {
},
},
}
var sessionsTestCases = []struct {
name string
description string
expectedWorkSession int
expectedCycle int
expectedLabel string
}{
{
name: "Test session 1",
description: "When the first work session has started.",
expectedWorkSession: 1,
expectedCycle: 1,
expectedLabel: workTimerLabel,
},
{
name: "Test session 2",
description: "When the first work session has finished and the short break session has started.",
expectedWorkSession: 1,
expectedCycle: 1,
expectedLabel: shortBreakTimerLabel,
},
{
name: "Test session 3",
description: "When the short break session has finished and the second work session has started.",
expectedWorkSession: 2,
expectedCycle: 1,
expectedLabel: workTimerLabel,
},
{
name: "Test session 4",
description: "When the second work session has finished and the long break has started because the maximum number of work sessions is reached.",
expectedWorkSession: 2,
expectedCycle: 1,
expectedLabel: longBreakTimerLabel,
},
{
name: "Test session 5",
description: "When the long break session has finished and the new work session has started for the next Pominal cycle.",
expectedWorkSession: 1,
expectedCycle: 2,
expectedLabel: workTimerLabel,
},
}

View file

@ -27,12 +27,6 @@ import (
"github.com/rivo/tview"
)
const (
workTimerLabel string = "Work"
shortBreakTimerLabel string = "Short break"
longBreakTimerLabel string = "Long break"
)
var (
workTime string
shortBreakTime string

View file

@ -24,6 +24,12 @@ import (
"github.com/rivo/tview"
)
const (
workTimerLabel string = "Work"
shortBreakTimerLabel string = "Short break"
longBreakTimerLabel string = "Long break"
)
type Pominal struct {
// workSession is a counter for work sessions.
workSession int

View file

@ -21,6 +21,9 @@ package main
import (
"reflect"
"testing"
"time"
"github.com/rivo/tview"
)
const success = "\u2713"
@ -29,9 +32,9 @@ const failure = "\u2717"
// TestNewPominal validates the factory function for generating new
// Pominal values
func TestNewPominal(t *testing.T) {
t.Log("Given the need to test creating a new value of type Pominal.")
t.Log("Given the need to test the creation of a new Pominal instance.")
{
for _, p := range pominalTestCases {
for _, p := range newPominalTestCases {
testFunc := func(t *testing.T) {
t.Log(p.description)
res := NewPominal(
@ -50,3 +53,54 @@ func TestNewPominal(t *testing.T) {
}
}
}
// TestRun validates the Pominal runtime. This test creates a new
// Pominal instance and executes the runtime in a separate goroutine.
// To ease testing the work, short break and long break timers are equal.
// Before the sub-tests are run there is a second delay to allow the
// runtime to initialise. The test then checks that the correct
// session label, work session number and pominal cycle number
// are set for each session. This test runs for exactly one
// pominal cycle.
func TestRun(t *testing.T) {
pominal := NewPominal(60, 60, 60, 2)
defer pominal.Stop()
// create the most basic text view for the info and timer
// views since we are not testing the user interface.
infoUI := tview.NewTextView()
timerUI := tview.NewTextView()
// TODO: Remove when support for disabling desktop notifications is in place.
initNotifier()
go pominal.Run(infoUI, timerUI)
time.Sleep(1 * time.Second)
t.Log("Given the need to test the Pominal runtime execution.")
{
for _, s := range sessionsTestCases {
testFunc := func(t *testing.T) {
t.Log(s.description)
if pominal.workSession != s.expectedWorkSession {
t.Errorf("%s\tUnexpected work session integer returned: expected %d, got %d", failure, s.expectedWorkSession, pominal.workSession)
} else {
t.Logf("%s\tExpected work session integer returned.", success)
}
if pominal.cycle != s.expectedCycle {
t.Errorf("%s\tUnexpected pominal cycle integer returned: expected %d, got %d", failure, s.expectedCycle, pominal.cycle)
} else {
t.Logf("%s\tExpected pominal cycle integer returned.", success)
}
if pominal.label != s.expectedLabel {
t.Errorf("%s\tUnexpected session label returned: expected %s, got %s", failure, s.expectedLabel, pominal.label)
} else {
t.Logf("%s\tExpected session label returned.", success)
}
}
t.Run(s.name, testFunc)
time.Sleep(65 * time.Second)
}
}
}