- Rename
const
s to generic names - Removed duplication in test code
package kumite import "time" func CheckWorkHours(dateTime time.Time) bool { const ( firstWorkingDay = 1 lastWorkingDay = 5 firstWorkingHour = 8 lastWorkingHour = 18 ) day, hour := dateTime.Weekday(), dateTime.Hour() return firstWorkingDay <= day && day <= lastWorkingDay && firstWorkingHour <= hour && hour < lastWorkingHour }
- package kumite
- import "time"
- func CheckWorkHours(dateTime time.Time) bool {
- const (
monday, friday = 1, 5morning, evening = 8, 18- firstWorkingDay = 1
- lastWorkingDay = 5
- firstWorkingHour = 8
- lastWorkingHour = 18
- )
- day, hour := dateTime.Weekday(), dateTime.Hour()
return monday <= day && day <= friday && morning <= hour && hour < evening}- return firstWorkingDay <= day && day <= lastWorkingDay && firstWorkingHour <= hour && hour < lastWorkingHour
- }
package kumite_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/kumite" "time" ) func testCheckWorkHours(descr string, val string, exp bool) { It(descr, func() { timeFormat := "2006-01-02 15:04:05 -0700 MST" dateTime, err := time.Parse(timeFormat, val) if err != nil { panic(err) } Expect(CheckWorkHours(dateTime)).To(Equal(exp)) }) } var _ = Describe("Test CheckWorkHours", func() { testCheckWorkHours("Monday @ 3pm should be True", "2017-01-16 15:00:00 -0500 EST", true) testCheckWorkHours("Friday @ 5:59pm should be True", "2017-01-16 17:59:59 -0500 EST", true) testCheckWorkHours("Sunday @ 3pm should be False", "2017-01-15 15:00:00 -0500 EST", false) testCheckWorkHours("Friday @ 6pm should be False", "2017-01-13 18:00:00 -0500 EST", false) testCheckWorkHours("Tuesday @ 7am should be False", "2017-01-17 07:00:00 -0500 EST", false) })
// TODO: replace with your own tests (TDD). An example to get you started is included below.// Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/>// Gomega Matcher Library <http://onsi.github.io/gomega/>- package kumite_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/kumite"
- "time"
- )
var _ = Describe("Test Example", func() {// Tests the functiontimeFormat := "2006-01-02 15:04:05 -0700 MST"It("Monday @ 3pm should be True", func() {dateTime, err := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST")- func testCheckWorkHours(descr string, val string, exp bool) {
- It(descr, func() {
- timeFormat := "2006-01-02 15:04:05 -0700 MST"
- dateTime, err := time.Parse(timeFormat, val)
- if err != nil {
- panic(err)
- }
Expect(CheckWorkHours(dateTime)).To(Equal(true))})It("Friday @ 5:59pm should be True", func() {dateTime, err := time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST")if err != nil {panic(err)}Expect(CheckWorkHours(dateTime)).To(Equal(true))})// False CasesIt("Sunday @ 3pm should be False", func() {dateTime, err := time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST")if err != nil {panic(err)}Expect(CheckWorkHours(dateTime)).To(Equal(false))})It("Friday @ 6pm should be False", func() {dateTime, err := time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST")if err != nil {panic(err)}Expect(CheckWorkHours(dateTime)).To(Equal(false))})It("Tuesday @ 7am should be False", func() {dateTime, err := time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST")if err != nil {panic(err)}Expect(CheckWorkHours(dateTime)).To(Equal(false))})- Expect(CheckWorkHours(dateTime)).To(Equal(exp))
- })
- }
- var _ = Describe("Test CheckWorkHours", func() {
- testCheckWorkHours("Monday @ 3pm should be True", "2017-01-16 15:00:00 -0500 EST", true)
- testCheckWorkHours("Friday @ 5:59pm should be True", "2017-01-16 17:59:59 -0500 EST", true)
- testCheckWorkHours("Sunday @ 3pm should be False", "2017-01-15 15:00:00 -0500 EST", false)
- testCheckWorkHours("Friday @ 6pm should be False", "2017-01-13 18:00:00 -0500 EST", false)
- testCheckWorkHours("Tuesday @ 7am should be False", "2017-01-17 07:00:00 -0500 EST", false)
- })