date parsing without spaces

This commit is contained in:
Erik Winter 2021-09-23 06:59:11 +02:00
parent 9557525b1e
commit 93ac14f539
2 changed files with 21 additions and 67 deletions

View File

@ -98,39 +98,35 @@ func NewDateFromString(date string) Date {
switch date { switch date {
case "": case "":
fallthrough fallthrough
case "no-date":
fallthrough
case "no date": case "no date":
return Date{} return Date{}
case "today": case "today":
fallthrough
case "vandaag":
return Today return Today
case "tomorrow": case "tomorrow":
fallthrough
case "morgen":
return Today.AddDays(1) return Today.AddDays(1)
case "day after tomorrow": case "day after tomorrow":
fallthrough fallthrough
case "overmorgen": case "day-after-tomorrow":
return Today.AddDays(2) return Today.AddDays(2)
case "this week": case "this week":
fallthrough fallthrough
case "deze week": case "this-week":
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday) daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday)
return Today.Add(daysToAdd) return Today.Add(daysToAdd)
case "next week": case "next week":
fallthrough fallthrough
case "volgende week": case "next-week":
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday) + 7 daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday) + 7
return Today.Add(daysToAdd) return Today.Add(daysToAdd)
case "this sprint": case "this sprint":
fallthrough
case "deze sprint":
tDate := NewDate(2021, 1, 28) // a sprint end tDate := NewDate(2021, 1, 28) // a sprint end
for { for {
if tDate.After(Today) { if tDate.After(Today) {

View File

@ -131,20 +131,15 @@ func TestNewDateFromString(t *testing.T) {
exp task.Date exp task.Date
}{ }{
{ {
name: "english dayname lowercase", name: "dayname lowercase",
input: "monday", input: "monday",
exp: task.NewDate(2021, 2, 1), exp: task.NewDate(2021, 2, 1),
}, },
{ {
name: "english dayname capitalized", name: "dayname capitalized",
input: "Monday", input: "Monday",
exp: task.NewDate(2021, 2, 1), exp: task.NewDate(2021, 2, 1),
}, },
{
name: "dutch dayname lowercase",
input: "maandag",
exp: task.NewDate(2021, 2, 1),
},
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, task.NewDateFromString(tc.input)) test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
@ -156,62 +151,31 @@ func TestNewDateFromString(t *testing.T) {
task.Today = task.NewDate(2021, 1, 30) task.Today = task.NewDate(2021, 1, 30)
for _, tc := range []struct { for _, tc := range []struct {
name string name string
input string
exp task.Date exp task.Date
}{ }{
{ {
name: "today english", name: "today",
input: "today",
exp: task.NewDate(2021, 1, 30), exp: task.NewDate(2021, 1, 30),
}, },
{ {
name: "today dutch", name: "tomorrow",
input: "vandaag",
exp: task.NewDate(2021, 1, 30),
},
{
name: "tomorrow english",
input: "tomorrow",
exp: task.NewDate(2021, 1, 31), exp: task.NewDate(2021, 1, 31),
}, },
{ {
name: "tomorrow dutch", name: "day after tomorrow",
input: "morgen",
exp: task.NewDate(2021, 1, 31),
},
{
name: "day after tomorrow english",
input: "day after tomorrow",
exp: task.NewDate(2021, 2, 1), exp: task.NewDate(2021, 2, 1),
}, },
{ {
name: "day after tomorrow dutch", name: "this week",
input: "overmorgen",
exp: task.NewDate(2021, 2, 1),
},
{
name: "this week english",
input: "this week",
exp: task.NewDate(2021, 2, 5), exp: task.NewDate(2021, 2, 5),
}, },
{ {
name: "this week dutch", name: "next week",
input: "deze week",
exp: task.NewDate(2021, 2, 5),
},
{
name: "next week english",
input: "next week",
exp: task.NewDate(2021, 2, 12),
},
{
name: "next week dutch",
input: "volgende week",
exp: task.NewDate(2021, 2, 12), exp: task.NewDate(2021, 2, 12),
}, },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, task.NewDateFromString(tc.input)) test.Equals(t, tc.exp, task.NewDateFromString(tc.name))
}) })
} }
}) })
@ -224,17 +188,11 @@ func TestNewDateFromString(t *testing.T) {
exp task.Date exp task.Date
}{ }{
{ {
name: "this sprint english", name: "this sprint",
today: task.NewDate(2021, 1, 30), today: task.NewDate(2021, 1, 30),
input: "this sprint", input: "this sprint",
exp: task.NewDate(2021, 2, 11), exp: task.NewDate(2021, 2, 11),
}, },
{
name: "this sprint dutch",
today: task.NewDate(2021, 1, 30),
input: "deze sprint",
exp: task.NewDate(2021, 2, 11),
},
{ {
name: "jump week", name: "jump week",
today: task.NewDate(2021, 2, 5), today: task.NewDate(2021, 2, 5),