From 8d3cc3ca53d6ca10a35376a4613975718ceefef7 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Mon, 12 Jun 2017 06:52:24 -0400 Subject: Re-order functions in file_store --- todolist/file_store.go | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) (limited to 'todolist') diff --git a/todolist/file_store.go b/todolist/file_store.go index a6fa0d7..e92f2d7 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -17,17 +17,21 @@ func NewFileStore() *FileStore { return &FileStore{FileLocation: "", Loaded: false} } -func getLocation() string { - localrepo := ".todos.json" - usr, _ := user.Current() - homerepo := fmt.Sprintf("%s/.todos.json", usr.HomeDir) - _, ferr := os.Stat(localrepo) +func (f *FileStore) Initialize() { + if f.FileLocation == "" { + f.FileLocation = ".todos.json" + } - if ferr == nil { - return localrepo - } else { - return homerepo + _, err := ioutil.ReadFile(f.FileLocation) + if err == nil { + fmt.Println("It looks like a .todos.json file already exists! Doing nothing.") + os.Exit(0) + } + if err := ioutil.WriteFile(f.FileLocation, []byte("[]"), 0644); err != nil { + fmt.Println("Error writing json file", err) + os.Exit(1) } + fmt.Println("Todo repo initialized.") } func (f *FileStore) Load() ([]*Todo, error) { @@ -55,26 +59,23 @@ func (f *FileStore) Load() ([]*Todo, error) { return todos, nil } -func (f *FileStore) Initialize() { - if f.FileLocation == "" { - f.FileLocation = ".todos.json" - } - - _, err := ioutil.ReadFile(f.FileLocation) - if err == nil { - fmt.Println("It looks like a .todos.json file already exists! Doing nothing.") - os.Exit(0) - } - if err := ioutil.WriteFile(f.FileLocation, []byte("[]"), 0644); err != nil { - fmt.Println("Error writing json file", err) - os.Exit(1) - } - fmt.Println("Todo repo initialized.") -} - func (f *FileStore) Save(todos []*Todo) { data, _ := json.Marshal(todos) if err := ioutil.WriteFile(f.FileLocation, []byte(data), 0644); err != nil { fmt.Println("Error writing json file", err) } } + +func getLocation() string { + localrepo := ".todos.json" + usr, _ := user.Current() + homerepo := fmt.Sprintf("%s/.todos.json", usr.HomeDir) + _, ferr := os.Stat(localrepo) + + if ferr == nil { + return localrepo + } else { + return homerepo + } +} + -- cgit v1.3 From cb849e5a8e0280d9fc10941d915f6252a0ad1cdc Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Mon, 12 Jun 2017 06:52:53 -0400 Subject: Remove return statements to silence linter warning --- todolist/file_store.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'todolist') diff --git a/todolist/file_store.go b/todolist/file_store.go index e92f2d7..6111de2 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -43,7 +43,6 @@ func (f *FileStore) Load() ([]*Todo, error) { if err != nil { fmt.Println("No todo file found!") fmt.Println("Initialize a new todo repo by running 'todo init'") - return nil, err os.Exit(0) } @@ -51,7 +50,6 @@ func (f *FileStore) Load() ([]*Todo, error) { jerr := json.Unmarshal(data, &todos) if jerr != nil { fmt.Println("Error reading json data", jerr) - return nil, jerr os.Exit(1) } f.Loaded = true -- cgit v1.3 From c50de79c4828dbb4f9b5c23bf9e00ea4dea38a01 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Mon, 12 Jun 2017 08:13:15 -0400 Subject: 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`. --- todolist/app.go | 2 +- todolist/app_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ todolist/file_store.go | 1 - todolist/memory_store.go | 19 +++++++++++++++++++ todolist/parser.go | 6 +++--- todolist/parser_test.go | 40 ++++++++++++++++++++++++++++------------ todolist/store.go | 4 ++-- 7 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 todolist/app_test.go create mode 100644 todolist/memory_store.go (limited to 'todolist') 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) } -- cgit v1.3 From 0d815737ccb61a6664a9943516518decdddf0bb2 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Mon, 12 Jun 2017 08:16:41 -0400 Subject: Put the return after the os.Exit --- todolist/file_store.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'todolist') diff --git a/todolist/file_store.go b/todolist/file_store.go index 7b512ec..2eee5c1 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -44,6 +44,7 @@ func (f *FileStore) Load() ([]*Todo, error) { fmt.Println("No todo file found!") fmt.Println("Initialize a new todo repo by running 'todo init'") os.Exit(0) + return nil, err } var todos []*Todo @@ -51,6 +52,7 @@ func (f *FileStore) Load() ([]*Todo, error) { if jerr != nil { fmt.Println("Error reading json data", jerr) os.Exit(1) + return nil, jerr } f.Loaded = true -- cgit v1.3