diff options
| author | Grant Ammons <gammons@gmail.com> | 2017-06-12 08:13:15 -0400 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2017-06-12 08:13:15 -0400 |
| commit | c50de79c4828dbb4f9b5c23bf9e00ea4dea38a01 (patch) | |
| tree | 1c6a3a75ca21299e160778f9dd5fddb3be9f3828 | |
| parent | cb849e5a8e0280d9fc10941d915f6252a0ad1cdc (diff) | |
Fix #64, parsing a todo with europe date format
* the `hasDue` function was not running, and therefore it was not parsing the due date.
* Also added the beginnings of a more holistic test approach, using `app_test.go`.
| -rw-r--r-- | todolist/app.go | 2 | ||||
| -rw-r--r-- | todolist/app_test.go | 45 | ||||
| -rw-r--r-- | todolist/file_store.go | 1 | ||||
| -rw-r--r-- | todolist/memory_store.go | 19 | ||||
| -rw-r--r-- | todolist/parser.go | 6 | ||||
| -rw-r--r-- | todolist/parser_test.go | 40 | ||||
| -rw-r--r-- | todolist/store.go | 4 |
7 files changed, 98 insertions, 19 deletions
diff --git a/todolist/app.go b/todolist/app.go index 56787d3..42b1361 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -8,7 +8,7 @@ import ( ) type App struct { - TodoStore *FileStore + TodoStore Store TodoList *TodoList } diff --git a/todolist/app_test.go b/todolist/app_test.go new file mode 100644 index 0000000..f35a7bc --- /dev/null +++ b/todolist/app_test.go @@ -0,0 +1,45 @@ +package todolist + +import ( + "fmt" + "strconv" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestAddTodo(t *testing.T) { + assert := assert.New(t) + app := &App{TodoList: &TodoList{}, TodoStore: &MemoryStore{}} + year := strconv.Itoa(time.Now().Year()) + + app.AddTodo("a do some stuff due may 23") + + todo := app.TodoList.FindById(1) + assert.Equal("do some stuff", todo.Subject) + assert.Equal(fmt.Sprintf("%s-05-23", year), todo.Due) + assert.Equal(false, todo.Completed) + assert.Equal(false, todo.Archived) + assert.Equal(false, todo.IsPriority) + assert.Equal("", todo.CompletedDate) + assert.Equal([]string{}, todo.Projects) + assert.Equal([]string{}, todo.Contexts) +} + +func TestAddTodoWithEuropeanDates(t *testing.T) { + assert := assert.New(t) + app := &App{TodoList: &TodoList{}, TodoStore: &MemoryStore{}} + + app.AddTodo("a do some stuff due 23 may") + + todo := app.TodoList.FindById(1) + assert.Equal("do some stuff", todo.Subject) + assert.Equal("2017-05-23", todo.Due) + assert.Equal(false, todo.Completed) + assert.Equal(false, todo.Archived) + assert.Equal(false, todo.IsPriority) + assert.Equal("", todo.CompletedDate) + assert.Equal([]string{}, todo.Projects) + assert.Equal([]string{}, todo.Contexts) +} diff --git a/todolist/file_store.go b/todolist/file_store.go index 6111de2..7b512ec 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -76,4 +76,3 @@ func getLocation() string { return homerepo } } - diff --git a/todolist/memory_store.go b/todolist/memory_store.go new file mode 100644 index 0000000..44a703c --- /dev/null +++ b/todolist/memory_store.go @@ -0,0 +1,19 @@ +package todolist + +type MemoryStore struct { + Todos []*Todo +} + +func NewMemoryStore() *MemoryStore { + return &MemoryStore{} +} + +func (m *MemoryStore) Initialize() {} + +func (m *MemoryStore) Load() ([]*Todo, error) { + return m.Todos, nil +} + +func (m *MemoryStore) Save(todos []*Todo) { + m.Todos = todos +} diff --git a/todolist/parser.go b/todolist/parser.go index 256e782..cdfb179 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -86,7 +86,8 @@ func (p *Parser) Contexts(input string) []string { func (p *Parser) hasDue(input string) bool { r1, _ := regexp.Compile(`due \w+$`) r2, _ := regexp.Compile(`due \w+ \d+$`) - return (r1.MatchString(input) || r2.MatchString(input)) + r3, _ := regexp.Compile(`due \d+ \w+$`) + return (r1.MatchString(input) || r2.MatchString(input) || r3.MatchString(input)) } func (p *Parser) Due(input string, day time.Time) string { @@ -137,9 +138,8 @@ func (p *Parser) parseArbitraryDate(_date string, pivot time.Time) string { d2 := p.parseArbitraryDateWithYear(_date, pivot.Year()+1) if d2.Sub(pivot) > diff1 { return d1.Format("2006-01-02") - } else { - return d2.Format("2006-01-02") } + return d2.Format("2006-01-02") } func (p *Parser) parseArbitraryDateWithYear(_date string, year int) time.Time { diff --git a/todolist/parser_test.go b/todolist/parser_test.go index f5cc185..476edaa 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -75,27 +75,27 @@ func TestParseContexts(t *testing.T) { } func TestDueToday(t *testing.T) { + assert := assert.New(t) parser := &Parser{} + expectedDate := bod(time.Now()).Format("2006-01-02") + todo := parser.ParseNewTodo("do this thing with @bob and @mary due today") - if todo.Due != bod(time.Now()).Format("2006-01-02") { - fmt.Println("Date is different", todo.Due, time.Now()) - } + assert.Equal(expectedDate, todo.Due) + todo = parser.ParseNewTodo("do this thing with @bob and @mary due tod") - if todo.Due != bod(time.Now()).Format("2006-01-02") { - fmt.Println("Date is different", todo.Due, time.Now()) - } + assert.Equal(expectedDate, todo.Due) } func TestDueTomorrow(t *testing.T) { + assert := assert.New(t) parser := &Parser{} + expectedDate := bod(time.Now()).AddDate(0, 0, 1).Format("2006-01-02") + todo := parser.ParseNewTodo("do this thing with @bob and @mary due tomorrow") - if todo.Due != bod(time.Now()).AddDate(0, 0, 1).Format("2006-01-02") { - fmt.Println("Date is different", todo.Due, time.Now()) - } + assert.Equal(expectedDate, todo.Due) + todo = parser.ParseNewTodo("do this thing with @bob and @mary due tom") - if todo.Due != bod(time.Now()).AddDate(0, 0, 1).Format("2006-01-02") { - fmt.Println("Date is different", todo.Due, time.Now()) - } + assert.Equal(expectedDate, todo.Due) } func TestDueSpecific(t *testing.T) { @@ -106,6 +106,14 @@ func TestDueSpecific(t *testing.T) { assert.Equal(fmt.Sprintf("%s-06-01", year), todo.Due) } +func TestDueSpecificEuropeanDate(t *testing.T) { + assert := assert.New(t) + parser := &Parser{} + todo := parser.ParseNewTodo("do this thing with @bob and @mary due 1 jun") + year := strconv.Itoa(time.Now().Year()) + assert.Equal(fmt.Sprintf("%s-06-01", year), todo.Due) +} + func TestMondayOnSunday(t *testing.T) { assert := assert.New(t) parser := &Parser{} @@ -149,6 +157,14 @@ func TestDueOnSpecificDate(t *testing.T) { assert.Equal(fmt.Sprintf("%s-06-01", year), parser.Due("due jun 1", time.Now())) } +func TestDueOnSpecificDateEuropeFormat(t *testing.T) { + assert := assert.New(t) + parser := &Parser{} + year := strconv.Itoa(time.Now().Year()) + assert.Equal(fmt.Sprintf("%s-05-02", year), parser.Due("due 2 may", time.Now())) + assert.Equal(fmt.Sprintf("%s-06-01", year), parser.Due("due 1 jun", time.Now())) +} + func TestDueOnSpecificDateEuropean(t *testing.T) { assert := assert.New(t) parser := &Parser{} diff --git a/todolist/store.go b/todolist/store.go index 518a30e..b246d8b 100644 --- a/todolist/store.go +++ b/todolist/store.go @@ -2,6 +2,6 @@ package todolist type Store interface { Initialize() - Load() - Save() + Load() ([]*Todo, error) + Save(todos []*Todo) } |
