diff options
| author | Stuart Skelton <stuarts@broadbean.com> | 2017-08-08 19:36:58 +0100 |
|---|---|---|
| committer | Stuart Skelton <stuarts@broadbean.com> | 2017-08-12 17:59:23 +0100 |
| commit | 8f8dcb6def8ef78c6b144f96e61dc627dd1cc3a8 (patch) | |
| tree | d838aa9dfe9eeb9cb11926f05eb85d2ebc9ba67e /todolist | |
| parent | d435ab85b203765e27b165d47e6155a73d84034c (diff) | |
Refactor GetID to only return an ID, and not a ID and Todo
Diffstat (limited to 'todolist')
| -rw-r--r-- | todolist/app.go | 33 | ||||
| -rw-r--r-- | todolist/app_test.go | 11 |
2 files changed, 24 insertions, 20 deletions
diff --git a/todolist/app.go b/todolist/app.go index 5045fdf..ffac17f 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -93,10 +93,15 @@ func (a *App) UnarchiveTodo(input string) { func (a *App) EditTodo(input string) { a.Load() - id, todo := a.getId(input) + id := a.getId(input) if id == -1 { return } + todo := a.TodoList.FindById(id) + if todo == nil { + fmt.Println("No such id.") + return + } parser := &Parser{} if parser.ParseEditTodo(todo, input) { @@ -107,7 +112,7 @@ func (a *App) EditTodo(input string) { func (a *App) ExpandTodo(input string) { a.Load() - id, _ := a.getId(input) + id := a.getId(input) parser := &Parser{} if id == -1 { return @@ -174,30 +179,21 @@ func (a *App) UnprioritizeTodo(input string) { fmt.Println("Todo un-prioritized.") } -func (a *App) getId(input string) (int, *Todo) { +func (a *App) getId(input string) int { re, _ := regexp.Compile("\\d+") if re.MatchString(input) { id, _ := strconv.Atoi(re.FindString(input)) - todo := a.TodoList.FindById(id) - if todo == nil { - fmt.Println("No such id.") - return -1, nil - - } - return id, todo - - } else { - fmt.Println("Invalid id.") - return -1, nil - + return id } + + fmt.Println("Invalid id.") + return -1 } func (a *App) getIds(input string) []int { var ids []int idGroups := strings.Split(input, ",") - singleNumberRE, _ := regexp.Compile("\\d+") rangeNumberRE, _ := regexp.Compile("(\\d+)-(\\d+)") for _, idGroup := range idGroups { @@ -211,11 +207,8 @@ func (a *App) getIds(input string) []int { for id := lowerID; id <= upperID; id++ { ids = append(ids, id) } - - } else if singleNumberRE.MatchString(idGroup) { - id, _ := strconv.Atoi(singleNumberRE.FindString(idGroup)) + } else if id := a.getId(idGroup); id != -1 { ids = append(ids, id) - } else { fmt.Printf("Invalid id: %s.\n", idGroup) } diff --git a/todolist/app_test.go b/todolist/app_test.go index 72a0292..2fb7717 100644 --- a/todolist/app_test.go +++ b/todolist/app_test.go @@ -109,6 +109,17 @@ func TestListbyContext(t *testing.T) { assert.Equal(true, hasACompletedTodo) } +func TestGetId(t *testing.T) { + assert := assert.New(t) + app := &App{TodoList: &TodoList{}, TodoStore: &MemoryStore{}} + // not a valid id + assert.Equal(-1, app.getId("p")) + // a single digit id + assert.Equal(6, app.getId("6")) + // a double digit id + assert.Equal(66, app.getId("66")) +} + func TestGetIds(t *testing.T) { assert := assert.New(t) app := &App{TodoList: &TodoList{}, TodoStore: &MemoryStore{}} |
