aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2017-06-19 12:58:27 -0400
committerGitHub <noreply@github.com>2017-06-19 12:58:27 -0400
commitdafb74428d8a16b28533aa25f83f25ea255c18a3 (patch)
treeb5df1531dec03b2a2beb3a1ea05b42938684b937 /todolist
parent4a91b284cc1840ad4dc4e60e831c8c6e3de283de (diff)
parent0d815737ccb61a6664a9943516518decdddf0bb2 (diff)
Merge pull request #68 from gammons/code-cleanup
Fix parsing a todo with europe date format
Diffstat (limited to 'todolist')
-rw-r--r--todolist/app.go2
-rw-r--r--todolist/app_test.go45
-rw-r--r--todolist/file_store.go56
-rw-r--r--todolist/memory_store.go19
-rw-r--r--todolist/parser.go6
-rw-r--r--todolist/parser_test.go40
-rw-r--r--todolist/store.go4
7 files changed, 126 insertions, 46 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 a6fa0d7..2eee5c1 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) {
@@ -39,42 +43,38 @@ 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)
+ return nil, err
}
var todos []*Todo
jerr := json.Unmarshal(data, &todos)
if jerr != nil {
fmt.Println("Error reading json data", jerr)
- return nil, jerr
os.Exit(1)
+ return nil, jerr
}
f.Loaded = true
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
+ }
+}
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)
}