2021-01-30 11:20:12 +01:00
|
|
|
package task_test
|
|
|
|
|
|
|
|
import (
|
2021-02-02 08:47:58 +01:00
|
|
|
"sort"
|
2021-01-30 11:20:12 +01:00
|
|
|
"testing"
|
2021-02-02 08:47:58 +01:00
|
|
|
"time"
|
2021-01-30 11:20:12 +01:00
|
|
|
|
2021-09-19 11:59:26 +02:00
|
|
|
"ewintr.nl/go-kit/test"
|
|
|
|
"ewintr.nl/gte/internal/task"
|
2021-01-30 11:20:12 +01:00
|
|
|
)
|
|
|
|
|
2021-02-02 08:47:58 +01:00
|
|
|
func TestWeekdaysSort(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
input task.Weekdays
|
|
|
|
exp task.Weekdays
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one",
|
|
|
|
input: task.Weekdays{time.Tuesday},
|
|
|
|
exp: task.Weekdays{time.Tuesday},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple",
|
|
|
|
input: task.Weekdays{time.Wednesday, time.Tuesday, time.Monday},
|
|
|
|
exp: task.Weekdays{time.Monday, time.Tuesday, time.Wednesday},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sunday is last",
|
|
|
|
input: task.Weekdays{time.Saturday, time.Sunday, time.Monday},
|
|
|
|
exp: task.Weekdays{time.Monday, time.Saturday, time.Sunday},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
sort.Sort(tc.input)
|
|
|
|
test.Equals(t, tc.exp, tc.input)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWeekdaysUnique(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
input task.Weekdays
|
|
|
|
exp task.Weekdays
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
input: task.Weekdays{},
|
|
|
|
exp: task.Weekdays{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single",
|
|
|
|
input: task.Weekdays{time.Monday},
|
|
|
|
exp: task.Weekdays{time.Monday},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no doubles",
|
|
|
|
input: task.Weekdays{time.Monday, time.Tuesday, time.Wednesday},
|
|
|
|
exp: task.Weekdays{time.Monday, time.Tuesday, time.Wednesday},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "doubles",
|
|
|
|
input: task.Weekdays{time.Monday, time.Monday, time.Wednesday, time.Monday},
|
|
|
|
exp: task.Weekdays{time.Monday, time.Wednesday},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, tc.input.Unique())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:25:25 +01:00
|
|
|
func TestNewDateFromString(t *testing.T) {
|
2021-02-09 07:12:21 +01:00
|
|
|
t.Run("no date", func(t *testing.T) {
|
|
|
|
task.Today = task.NewDate(2021, 1, 30)
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
exp task.Date
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
exp: task.Date{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no date",
|
|
|
|
input: "no date",
|
|
|
|
exp: task.Date{},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("digits", func(t *testing.T) {
|
|
|
|
task.Today = task.NewDate(2021, 1, 30)
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
exp task.Date
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "normal",
|
|
|
|
input: "2021-01-30 (saturday)",
|
|
|
|
exp: task.NewDate(2021, 1, 30),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "short",
|
|
|
|
input: "2021-01-30",
|
|
|
|
exp: task.NewDate(2021, 1, 30),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("day name", func(t *testing.T) {
|
|
|
|
task.Today = task.NewDate(2021, 1, 30)
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
exp task.Date
|
|
|
|
}{
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "dayname lowercase",
|
2021-02-09 07:12:21 +01:00
|
|
|
input: "monday",
|
|
|
|
exp: task.NewDate(2021, 2, 1),
|
|
|
|
},
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "dayname capitalized",
|
2021-02-09 07:12:21 +01:00
|
|
|
input: "Monday",
|
|
|
|
exp: task.NewDate(2021, 2, 1),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("relative days", func(t *testing.T) {
|
|
|
|
task.Today = task.NewDate(2021, 1, 30)
|
|
|
|
for _, tc := range []struct {
|
2021-09-23 06:59:11 +02:00
|
|
|
name string
|
|
|
|
exp task.Date
|
2021-02-09 07:12:21 +01:00
|
|
|
}{
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "today",
|
|
|
|
exp: task.NewDate(2021, 1, 30),
|
2021-02-09 07:12:21 +01:00
|
|
|
},
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "tomorrow",
|
|
|
|
exp: task.NewDate(2021, 1, 31),
|
2021-02-09 07:12:21 +01:00
|
|
|
},
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "day after tomorrow",
|
|
|
|
exp: task.NewDate(2021, 2, 1),
|
2021-02-09 07:12:21 +01:00
|
|
|
},
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "this week",
|
|
|
|
exp: task.NewDate(2021, 2, 5),
|
2021-02-09 07:12:21 +01:00
|
|
|
},
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "next week",
|
|
|
|
exp: task.NewDate(2021, 2, 12),
|
2021-02-09 07:12:21 +01:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-09-23 06:59:11 +02:00
|
|
|
test.Equals(t, tc.exp, task.NewDateFromString(tc.name))
|
2021-02-09 07:12:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sprint", func(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
today task.Date
|
|
|
|
input string
|
|
|
|
exp task.Date
|
|
|
|
}{
|
|
|
|
{
|
2021-09-23 06:59:11 +02:00
|
|
|
name: "this sprint",
|
2021-02-09 07:12:21 +01:00
|
|
|
today: task.NewDate(2021, 1, 30),
|
|
|
|
input: "this sprint",
|
|
|
|
exp: task.NewDate(2021, 2, 11),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jump week",
|
|
|
|
today: task.NewDate(2021, 2, 5),
|
|
|
|
input: "this sprint",
|
|
|
|
exp: task.NewDate(2021, 2, 11),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
task.Today = tc.today
|
|
|
|
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-06-24 06:56:00 +02:00
|
|
|
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
test.Equals(t, task.Date{}, task.NewDateFromString("test"))
|
|
|
|
})
|
2021-01-30 15:25:25 +01:00
|
|
|
}
|
|
|
|
|
2021-06-04 10:45:56 +02:00
|
|
|
func TestDateDaysBetween(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
d1 task.Date
|
|
|
|
d2 task.Date
|
|
|
|
exp int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "same",
|
|
|
|
d1: task.NewDate(2021, 6, 23),
|
|
|
|
d2: task.NewDate(2021, 6, 23),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one",
|
|
|
|
d1: task.NewDate(2021, 6, 23),
|
|
|
|
d2: task.NewDate(2021, 6, 24),
|
|
|
|
exp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "many",
|
|
|
|
d1: task.NewDate(2021, 6, 23),
|
|
|
|
d2: task.NewDate(2024, 3, 7),
|
|
|
|
exp: 988,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "edge",
|
|
|
|
d1: task.NewDate(2020, 12, 30),
|
|
|
|
d2: task.NewDate(2021, 1, 3),
|
|
|
|
exp: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reverse",
|
|
|
|
d1: task.NewDate(2021, 6, 23),
|
|
|
|
d2: task.NewDate(2021, 5, 23),
|
|
|
|
exp: 31,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, tc.d1.DaysBetween(tc.d2))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:20:12 +01:00
|
|
|
func TestDateString(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
2021-01-30 15:25:25 +01:00
|
|
|
date task.Date
|
2021-01-30 11:20:12 +01:00
|
|
|
exp string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "zero",
|
|
|
|
date: task.NewDate(0, 0, 0),
|
|
|
|
exp: "no date",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normal",
|
2021-02-03 10:25:25 +01:00
|
|
|
date: task.NewDate(2021, 5, 30),
|
|
|
|
exp: "2021-05-30 (sunday)",
|
2021-01-30 11:20:12 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normalize",
|
2021-02-03 10:25:25 +01:00
|
|
|
date: task.NewDate(2021, 5, 32),
|
|
|
|
exp: "2021-06-01 (tuesday)",
|
2021-01-30 11:20:12 +01:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, tc.date.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 12:11:02 +01:00
|
|
|
|
2021-06-24 06:56:00 +02:00
|
|
|
func TestDateIsZero(t *testing.T) {
|
|
|
|
test.Equals(t, true, task.Date{}.IsZero())
|
|
|
|
test.Equals(t, false, task.NewDate(2021, 6, 24).IsZero())
|
|
|
|
}
|
|
|
|
|
2021-01-31 12:11:02 +01:00
|
|
|
func TestDateAfter(t *testing.T) {
|
|
|
|
day := task.NewDate(2021, 1, 31)
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
tDay task.Date
|
|
|
|
exp bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "after",
|
|
|
|
tDay: task.NewDate(2021, 1, 30),
|
|
|
|
exp: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "on",
|
|
|
|
tDay: day,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "before",
|
|
|
|
tDay: task.NewDate(2021, 2, 1),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
test.Equals(t, tc.exp, day.After(tc.tDay))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|