From 2976acd639b57853df315f80172b61ff75b9d7f3 Mon Sep 17 00:00:00 2001 From: Stuart Skelton Date: Sun, 6 Aug 2017 21:40:19 +0100 Subject: Allow for ranged ids in some commands --- todolist/app_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'todolist/app_test.go') diff --git a/todolist/app_test.go b/todolist/app_test.go index f29a1b6..72a0292 100644 --- a/todolist/app_test.go +++ b/todolist/app_test.go @@ -108,3 +108,21 @@ func TestListbyContext(t *testing.T) { } assert.Equal(true, hasACompletedTodo) } + +func TestGetIds(t *testing.T) { + assert := assert.New(t) + app := &App{TodoList: &TodoList{}, TodoStore: &MemoryStore{}} + // no valid id here + assert.Equal(0, len(app.getIds("p"))) + // one valid value here + assert.Equal([]int{6}, app.getIds("6")) + // lots of single post numbers + assert.Equal([]int{6, 10, 8, 4}, app.getIds("6,10,8,4")) + // a correct range + assert.Equal([]int{6, 7, 8}, app.getIds("6-8")) + // some incorrect ranges + assert.Equal(0, len(app.getIds("6-6"))) + assert.Equal(0, len(app.getIds("8-6"))) + // some compsite ranges + assert.Equal([]int{5, 6, 7, 8, 10, 11, 9}, app.getIds("5,6-8,10-11,9")) +} -- cgit v1.3 From 8f8dcb6def8ef78c6b144f96e61dc627dd1cc3a8 Mon Sep 17 00:00:00 2001 From: Stuart Skelton Date: Tue, 8 Aug 2017 19:36:58 +0100 Subject: Refactor GetID to only return an ID, and not a ID and Todo --- todolist/app.go | 33 +++++++++++++-------------------- todolist/app_test.go | 11 +++++++++++ 2 files changed, 24 insertions(+), 20 deletions(-) (limited to 'todolist/app_test.go') 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{}} -- cgit v1.3