diff --git a/cases_test.go b/cases_test.go index 1c663dd..b49bf19 100644 --- a/cases_test.go +++ b/cases_test.go @@ -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, + }, +} diff --git a/main.go b/main.go index 4f9ad4d..c842166 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/pominal.go b/pominal.go index e3ba5b5..07d6592 100644 --- a/pominal.go +++ b/pominal.go @@ -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 diff --git a/pominal_test.go b/pominal_test.go index a823822..99be7a6 100644 --- a/pominal_test.go +++ b/pominal_test.go @@ -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) + } + } +}