diff options
| author | Grant Ammons <gammons@gmail.com> | 2016-04-23 16:59:52 -0400 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2016-04-23 16:59:52 -0400 |
| commit | 8138ffaf380fb646b6877dd20f874735eddd2d6d (patch) | |
| tree | da3077b68ff432d4ffe2aeae234ecb70843a3165 | |
Initial commit
| -rw-r--r-- | parser.go | 40 | ||||
| -rw-r--r-- | parser_test.go | 33 | ||||
| -rw-r--r-- | todo.go | 19 | ||||
| -rw-r--r-- | todo_test.go | 23 |
4 files changed, 115 insertions, 0 deletions
diff --git a/parser.go b/parser.go new file mode 100644 index 0000000..bb54b97 --- /dev/null +++ b/parser.go @@ -0,0 +1,40 @@ +package todolist + +import ( + "fmt" + "regexp" + "strings" +) + +type Parser struct{} + +func (p *Parser) Parse(input string) *Todo { + todo := NewTodo() + todo.Subject = p.Subject(input) + todo.Projects = p.Projects(input) + return todo +} + +func (p *Parser) Subject(input string) string { + if strings.Contains(input, " due") { + index := strings.LastIndex(input, " due") + return input[0:index] + } else { + return input + } +} + +func (p *Parser) Projects(input string) []string { + r, err := regexp.Compile(`\+\w+`) + if err != nil { + fmt.Printf("There was a problem with the regexp", err) + } + results := r.FindAllString(input, -1) + ret := []string{} + + for _, val := range results { + fmt.Println("Result is ", val) + ret = append(ret, val[1:len(val)]) + } + return ret +} diff --git a/parser_test.go b/parser_test.go new file mode 100644 index 0000000..9e1c9cf --- /dev/null +++ b/parser_test.go @@ -0,0 +1,33 @@ +package todolist + +import "testing" + +func TestParseSubject(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing") + if todo.Subject != "do this thing" { + t.Error("Expected todo.Subject to equal 'do this thing'") + } +} + +func TestParseSubjectWithDue(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing due tomorrow") + if todo.Subject != "do this thing" { + t.Error("Expected todo.Subject to equal 'do this thing', got ", todo.Subject) + } +} + +func TestParseProjects(t *testing.T) { + parser := &Parser{} + todo := parser.Parse("do this thing +proj1 +proj2 due tomorrow") + if len(todo.Projects) != 2 { + t.Error("Expected Projects length to be 2") + } + if todo.Projects[0] != "proj1" { + t.Error("todo.Projects[0] should equal 'proj1' but got", todo.Projects[0]) + } + if todo.Projects[1] != "proj2" { + t.Error("todo.Projects[1] should equal 'proj2' but got", todo.Projects[1]) + } +} @@ -0,0 +1,19 @@ +package todolist + +type Todo struct { + Id string + Subject string + Projects []string + Contexts []string + Due string + Completed bool + Archived bool +} + +func NewTodo() *Todo { + return &Todo{Completed: false, Archived: false} +} + +func (t Todo) Valid() bool { + return (t.Subject != "") +} diff --git a/todo_test.go b/todo_test.go new file mode 100644 index 0000000..aab2a74 --- /dev/null +++ b/todo_test.go @@ -0,0 +1,23 @@ +package todolist + +import "testing" + +func TestNewTodo(t *testing.T) { + todo := NewTodo() + + if todo.Completed || todo.Archived { + t.Error("Completed should be false for new todos") + } +} + +func TestValidity(t *testing.T) { + todo := &Todo{Subject: "test"} + if !todo.Valid() { + t.Error("Expected valid todo to be valid") + } + + invalidTodo := &Todo{Subject: ""} + if invalidTodo.Valid() { + t.Error("Invalid todo is being reported as valid") + } +} |
