From 425f23e70fe6eb013b51dd0328707b223d77aa84 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 23 Sep 2019 09:05:19 +0100 Subject: [PATCH] fix: set minimum maxWorkSessions for a cycle - set the minimum number of work sessions to 1. - test the creation of Pominal variables. --- cases_test.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ pominal.go | 30 ++++++++++----- pominal_test.go | 46 ++++++++++++++++------- 3 files changed, 150 insertions(+), 23 deletions(-) create mode 100644 cases_test.go diff --git a/cases_test.go b/cases_test.go new file mode 100644 index 0000000..93a748c --- /dev/null +++ b/cases_test.go @@ -0,0 +1,97 @@ +/* + Pominal + Copyright (C) 2019 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 + +var pominalTestCases = []struct { + description string + workSession float64 + shortBreakSession float64 + longBreakSession float64 + maxWorkSessions int + expectedPominal Pominal +}{ + { + description: "Test case 1 - Create Pominal with all valid parameters.", + workSession: 1200, + shortBreakSession: 300, + longBreakSession: 600, + maxWorkSessions: 5, + expectedPominal: Pominal{ + workSession: 1, + maxWorkSessions: 5, + cycle: 1, + countdown: 0, + work: 1200, + shortBreak: 300, + longBreak: 600, + label: "", + }, + }, + { + description: "Test case 2 - Create Pominal with work session set below the minimum.", + workSession: 59.9999999999999999999999999999999, + shortBreakSession: 1536, + longBreakSession: 1200, + maxWorkSessions: 4, + expectedPominal: Pominal{ + workSession: 1, + maxWorkSessions: 4, + cycle: 1, + countdown: 0, + work: 60, + shortBreak: 1536, + longBreak: 1200, + label: "", + }, + }, + { + description: "Test case 3 - Create Pominal with both short break and long break below the minimum.", + workSession: 1000, + shortBreakSession: 1.23, + longBreakSession: 45.6743, + maxWorkSessions: 1, + expectedPominal: Pominal{ + workSession: 1, + maxWorkSessions: 1, + cycle: 1, + countdown: 0, + work: 1000, + shortBreak: 60, + longBreak: 60, + label: "", + }, + }, + { + description: "Test case 4 - Create Pominal with maximum work sessions set below 1.", + workSession: 1500, + shortBreakSession: 300, + longBreakSession: 1200, + maxWorkSessions: -1, + expectedPominal: Pominal{ + workSession: 1, + maxWorkSessions: 1, + cycle: 1, + countdown: 0, + work: 1500, + shortBreak: 300, + longBreak: 1200, + label: "", + }, + }, +} diff --git a/pominal.go b/pominal.go index 38412a0..d740dc4 100644 --- a/pominal.go +++ b/pominal.go @@ -59,11 +59,11 @@ func NewPominal(w, s, l float64, m int) Pominal { return Pominal{ workSession: 1, - maxWorkSessions: m, + maxWorkSessions: setMaxWorkSessions(m), cycle: 1, - work: sessionTime(w), - shortBreak: sessionTime(s), - longBreak: sessionTime(l), + work: setSessionTime(w), + shortBreak: setSessionTime(s), + longBreak: setSessionTime(l), } } @@ -134,13 +134,25 @@ func (p *Pominal) UpdateSession() { alert(p.label) } -// sessionTime returns the minimum session time +// setSessionTime returns the minimum session time // if the value is less than the minimum. // Otherwise it returns the value. -func sessionTime(s float64) float64 { - var minSessionTime float64 = 60 - if s < minSessionTime { - return minSessionTime +func setSessionTime(s float64) float64 { + var min float64 = 60 + if s < min { + return min } return s } + +// setMaxWorkSessions returns the minimum amount +// of work session per Pominal cycle if the user supplies +// a value below it. +// Otherwise it returns the value set by the user. +func setMaxWorkSessions(w int) int { + var min = 1 + if w < min { + return min + } + return w +} diff --git a/pominal_test.go b/pominal_test.go index 775bc8c..1a65d7c 100644 --- a/pominal_test.go +++ b/pominal_test.go @@ -1,23 +1,41 @@ +/* + Pominal + Copyright (C) 2019 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" ) -func TestSessionTime(t *testing.T) { - sessionTests := []struct { - input float64 - expected float64 - }{ - {120, 120}, - {59.9999, 60}, - {1, 60}, - } - - for _, s := range sessionTests { - res := sessionTime(s.input) - if res != s.expected { - t.Errorf("Unexpected result. Got %v, expected %v", res, s.expected) +func TestNewPominal(t *testing.T) { + for _, p := range pominalTestCases { + t.Log(p.description) + res := NewPominal( + p.workSession, + p.shortBreakSession, + p.longBreakSession, + p.maxWorkSessions, + ) + if !reflect.DeepEqual(res, p.expectedPominal) { + t.Errorf("Unexpected Pominal created. Expected %v, received %v.", p.expectedPominal, res) + } else { + t.Log("Test passed.") } } }