/* Pominal Copyright (C) 2020 Daniel Anglin This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "reflect" "testing" "time" "github.com/rivo/tview" ) const success = "\u2713" 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 the creation of a new Pominal instance.") { for _, p := range newPominalTestCases { testFunc := func(t *testing.T) { t.Log(p.description) res := NewPominal( p.workSession, p.shortBreakSession, p.longBreakSession, p.maxWorkSessions, ) if !reflect.DeepEqual(res, p.expectedPominal) { t.Errorf("%s\tUnexpected Pominal created. Expected %v, received %v.", failure, p.expectedPominal, res) } else { t.Logf("%s\tTest passed.", success) } } t.Run(p.name, testFunc) } } } // 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() 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) } } }