pominal/pominal_test.go
Dan Anglin 19eb9b20be
feat: support configuring Pominal with config file
This commit adds a feature to load Pominal
configuration from a JSON configuration file.

A new data type called PominalConfig was created for
the purpose of configuring Pominal.
A new factory function called newPominalConfig was
created which takes the path to the JSON configuration
file and the flag overrides and generates the new
PominalConfig value.

Here, the JSON config is parsed to create the initial
configuration and any flag overrides can override
the corresponding fields in the PominalConfig object.
Currently only the sessions times and the maximum work
sessions per Pominal cycle have flag overrides.

Additionally users can now configure custom notification
messages for each session type from the configuration file.
There are currently no flag overrides for these.

This commit resolves dananglin/Pominal#1
2020-02-13 12:42:55 +00:00

107 lines
3.5 KiB
Go

/*
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 <http://www.gnu.org/licenses/>.
*/
package main
import (
"reflect"
"testing"
"time"
"github.com/rivo/tview"
)
// TestNewPominal validates the factory function for generating new
// Pominal values from PominalConfig 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, err := newPominal(p.config)
if err != nil {
t.Fatalf("%s\tError while creating the Pominal value, %s", failure, err.Error())
}
if !reflect.DeepEqual(res, p.expectedPominal) {
t.Errorf("%s\tUnexpected Pominal value 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) {
config, err := newPominalConfig("", "1m", "1m", "1m", 2)
if err != nil {
t.Fatalf("%s\tError while creating the PominalConfig value, %s", failure, err.Error())
}
pominal, err := newPominal(config)
if err != nil {
t.Fatalf("%s\tError while creating the Pominal value, %s", failure, err.Error())
}
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)
}
}
}