From d2df88f217cef638bdca527794969fcaa835bfd7 Mon Sep 17 00:00:00 2001 From: Michael Strüder Date: Tue, 2 May 2017 17:00:26 +0200 Subject: add test for func (p Parser) Parse() --- todolist/parser_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'todolist/parser_test.go') diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 8cd6b53..9dfa454 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -169,3 +169,24 @@ func TestDueIntelligentlyChoosesCorrectYear(t *testing.T) { assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", septemberTime)) assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", decemberTime)) } + +func TestParseCommandIdSubject(t *testing.T) { + assert := assert.New(t) + parser := Parser{"es 24 a new subject"} + command, id, subject := parser.Parse() + + assert.Equal("es", command) + assert.Equal(24, id) + assert.Equal("a new subject", subject) +} + +func TestParseInvalidCommandIdSubject(t *testing.T) { + assert := assert.New(t) + input := "es a new project" + parser := Parser{input} + command, id, subject := parser.Parse() + + assert.Equal("", command) + assert.Equal(-1, id) + assert.Equal(input, subject) +} -- cgit v1.3 From 950aabce192d6dd15eee60717a510be2bb4f9c7c Mon Sep 17 00:00:00 2001 From: Michael Strüder Date: Wed, 3 May 2017 10:59:26 +0200 Subject: test multiple whitespace --- todolist/parser_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'todolist/parser_test.go') diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 9dfa454..05f390a 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -170,6 +170,16 @@ func TestDueIntelligentlyChoosesCorrectYear(t *testing.T) { assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", decemberTime)) } +func TestParseCommandIdSubjectWhitespace(t *testing.T) { + assert := assert.New(t) + parser := Parser{"es 24\t a new subject"} + command, id, subject := parser.Parse() + + assert.Equal("es", command) + assert.Equal(24, id) + assert.Equal("a new subject", subject) +} + func TestParseCommandIdSubject(t *testing.T) { assert := assert.New(t) parser := Parser{"es 24 a new subject"} @@ -188,5 +198,5 @@ func TestParseInvalidCommandIdSubject(t *testing.T) { assert.Equal("", command) assert.Equal(-1, id) - assert.Equal(input, subject) + assert.Equal("", subject) } -- cgit v1.3 From fdca747e173c20f59901207c3f1887cece2a9351 Mon Sep 17 00:00:00 2001 From: Michael Strüder Date: Wed, 3 May 2017 15:18:13 +0200 Subject: subcommands like delete don't take a subject, so make it optional --- todolist/parser.go | 18 ++++++++++++------ todolist/parser_test.go | 10 ++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'todolist/parser_test.go') diff --git a/todolist/parser.go b/todolist/parser.go index 616e82f..3598e12 100644 --- a/todolist/parser.go +++ b/todolist/parser.go @@ -31,22 +31,28 @@ func (p *Parser) ParseNewTodo(input string) *Todo { } // Parse accepts user input and splits it into subcommand, the todo id to -// work on and the input to the subcommand function. -func (p Parser) Parse() (subcommand string, id int, input string) { - r := regexp.MustCompile(`(\w+)\s+(\d+)\s+(.*)`) +// work on and the subject for the subcommand function. +func (p Parser) Parse() (subcommand string, id int, subject string) { + r := regexp.MustCompile(`(\w+)\s+(\d+)(\s+(.*))?`) matches := r.FindStringSubmatch(p.input) - if len(matches) < 4 { - fmt.Println("Could match command, id or subject") + if len(matches) < 3 { + fmt.Println("Could match command or id") return "", -1, "" } + subcommand = matches[1] + // because of the regexp match, this can never fail id, err := strconv.Atoi(matches[2]) if err != nil { panic(err) } - return matches[1], id, matches[3] + if len(matches) == 5 { + subject = matches[4] + } + + return } func (p *Parser) Subject(input string) string { diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 05f390a..4466489 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -170,6 +170,16 @@ func TestDueIntelligentlyChoosesCorrectYear(t *testing.T) { assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", decemberTime)) } +func TestParseCommandIdSubjectOptionalSubject(t *testing.T) { + assert := assert.New(t) + parser := Parser{"d 24"} + command, id, subject := parser.Parse() + + assert.Equal("d", command) + assert.Equal(24, id) + assert.Equal("", subject) +} + func TestParseCommandIdSubjectWhitespace(t *testing.T) { assert := assert.New(t) parser := Parser{"es 24\t a new subject"} -- cgit v1.3 From 580183ad7f155e4cdce047bdeb22fd775be69fc8 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Tue, 9 May 2017 20:17:33 -0400 Subject: Update the tests * Test editing a todo with just a due date does not affect subject * Test editing a todo with just subject does not affect due date * Editing a subject should also update projects and contexts --- todolist/parser_test.go | 79 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 25 deletions(-) (limited to 'todolist/parser_test.go') diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 4466489..6920c7c 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -170,43 +170,72 @@ func TestDueIntelligentlyChoosesCorrectYear(t *testing.T) { assert.Equal("2017-01-10", parser.parseArbitraryDate("jan 10", decemberTime)) } -func TestParseCommandIdSubjectOptionalSubject(t *testing.T) { +func TestParseEditTodoJustDate(t *testing.T) { assert := assert.New(t) - parser := Parser{"d 24"} - command, id, subject := parser.Parse() + parser := &Parser{} + todo := NewTodo() + tomorrow := time.Now().AddDate(0,0,1).Format("2006-01-02") + + parser.ParseEditTodo(todo, "e 24 due tom") + + assert.Equal(todo.Due, tomorrow) +} + +func TestParseEditTodoJustDateDoesNotEditExistingSubject(t *testing.T) { + assert := assert.New(t) + parser := &Parser{} + todo := NewTodo() + todo.Subject = "pick up the trash" + tomorrow := time.Now().AddDate(0,0,1).Format("2006-01-02") + + parser.ParseEditTodo(todo, "e 24 due tom") - assert.Equal("d", command) - assert.Equal(24, id) - assert.Equal("", subject) + assert.Equal(todo.Due, tomorrow) + assert.Equal(todo.Subject, "pick up the trash") } -func TestParseCommandIdSubjectWhitespace(t *testing.T) { +func TestParseEditTodoJustSubject(t *testing.T) { assert := assert.New(t) - parser := Parser{"es 24\t a new subject"} - command, id, subject := parser.Parse() + parser := &Parser{} + todo := &Todo{Subject: "pick up the trash", Due: "2016-11-25"} + + parser.ParseEditTodo(todo, "e 24 changed the todo") - assert.Equal("es", command) - assert.Equal(24, id) - assert.Equal("a new subject", subject) + assert.Equal(todo.Due, "2016-11-25") + assert.Equal(todo.Subject, "changed the todo") } -func TestParseCommandIdSubject(t *testing.T) { +func TestParseEditTodoSubjectUpdatesProjectsAndContexts(t *testing.T) { assert := assert.New(t) - parser := Parser{"es 24 a new subject"} - command, id, subject := parser.Parse() + parser := &Parser{} + todo := &Todo{ + Subject: "pick up the +trash with @dad", + Due: "2016-11-25", + Projects: []string{"trash"}, + Contexts: []string{"dad"}, + } - assert.Equal("es", command) - assert.Equal(24, id) - assert.Equal("a new subject", subject) + parser.ParseEditTodo(todo, "e 24 get the +garbage with @mom") + + assert.Equal(todo.Due, "2016-11-25") + assert.Equal(todo.Subject, "get the +garbage with @mom") + assert.Equal(todo.Projects, []string{"garbage"}) + assert.Equal(todo.Contexts, []string{"mom"}) } -func TestParseInvalidCommandIdSubject(t *testing.T) { +func TestParseEditTodoWithSubjectAndDue(t *testing.T) { assert := assert.New(t) - input := "es a new project" - parser := Parser{input} - command, id, subject := parser.Parse() + parser := &Parser{} + todo := &Todo{ + Subject: "pick up the +trash with @dad", + Due: "2016-11-25", + Projects: []string{"trash"}, + Contexts: []string{"dad"}, + } + tomorrow := time.Now().AddDate(0,0,1).Format("2006-01-02") + + parser.ParseEditTodo(todo, "e 24 get the +garbage with @mom due tom") - assert.Equal("", command) - assert.Equal(-1, id) - assert.Equal("", subject) + assert.Equal(todo.Due, tomorrow) + assert.Equal(todo.Subject, "get the +garbage with @mom") } -- cgit v1.3 From d86bc4ea3f85bab91be80792c423b2a96aa6d626 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Wed, 10 May 2017 20:21:41 -0400 Subject: Formatting for test --- todolist/parser_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'todolist/parser_test.go') diff --git a/todolist/parser_test.go b/todolist/parser_test.go index 6920c7c..f5cc185 100644 --- a/todolist/parser_test.go +++ b/todolist/parser_test.go @@ -174,7 +174,7 @@ func TestParseEditTodoJustDate(t *testing.T) { assert := assert.New(t) parser := &Parser{} todo := NewTodo() - tomorrow := time.Now().AddDate(0,0,1).Format("2006-01-02") + tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02") parser.ParseEditTodo(todo, "e 24 due tom") @@ -186,7 +186,7 @@ func TestParseEditTodoJustDateDoesNotEditExistingSubject(t *testing.T) { parser := &Parser{} todo := NewTodo() todo.Subject = "pick up the trash" - tomorrow := time.Now().AddDate(0,0,1).Format("2006-01-02") + tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02") parser.ParseEditTodo(todo, "e 24 due tom") @@ -209,8 +209,8 @@ func TestParseEditTodoSubjectUpdatesProjectsAndContexts(t *testing.T) { assert := assert.New(t) parser := &Parser{} todo := &Todo{ - Subject: "pick up the +trash with @dad", - Due: "2016-11-25", + Subject: "pick up the +trash with @dad", + Due: "2016-11-25", Projects: []string{"trash"}, Contexts: []string{"dad"}, } @@ -227,12 +227,12 @@ func TestParseEditTodoWithSubjectAndDue(t *testing.T) { assert := assert.New(t) parser := &Parser{} todo := &Todo{ - Subject: "pick up the +trash with @dad", - Due: "2016-11-25", + Subject: "pick up the +trash with @dad", + Due: "2016-11-25", Projects: []string{"trash"}, Contexts: []string{"dad"}, } - tomorrow := time.Now().AddDate(0,0,1).Format("2006-01-02") + tomorrow := time.Now().AddDate(0, 0, 1).Format("2006-01-02") parser.ParseEditTodo(todo, "e 24 get the +garbage with @mom due tom") -- cgit v1.3