Unittesting begins

This commit is contained in:
Bel LaPointe
2019-11-12 13:45:32 -07:00
commit 8c4bc81694
35 changed files with 3231 additions and 0 deletions

100
server/ajax/task/task.go Normal file
View File

@@ -0,0 +1,100 @@
package task
import (
"errors"
"local/todo-server/server/ajax/form"
"net/http"
"regexp"
"strings"
"time"
"github.com/google/uuid"
)
type Task struct {
ID string
UUID string
Title string
Priority int
Tags []string
Created time.Time
Edited time.Time
Completed time.Time
Complete bool
Note []string
Due time.Time
}
func New(r *http.Request) (*Task, error) {
task := &Task{
UUID: uuid.New().String(),
Title: form.Get(r, "title"),
Priority: form.ToInt(form.Get(r, "prio")),
Tags: form.ToStrArr(form.Get(r, "tags")),
Created: time.Now(),
Edited: time.Now(),
Due: form.ToTime(form.Get(r, "duedate")),
}
task.ID = task.UUID
task.SetNote(form.Get(r, "note"))
return task, task.validate()
}
func (t *Task) AppendTags(tags []string) {
t.touch()
t.Tags = append(t.Tags, tags...)
}
func (t *Task) SetComplete(state bool) {
t.touch()
t.Complete = state
if t.Complete {
t.Completed = time.Now()
} else {
t.Completed = time.Time{}
}
}
func (t *Task) SetPrio(prio int) {
t.touch()
t.Priority = prio
}
func (t *Task) SetNote(note string) {
t.touch()
t.Note = strings.Split(note, "\n")
}
func (t *Task) touch() {
t.Edited = time.Now()
}
func (t *Task) validate() error {
if t.Title == "" {
return errors.New("task cannot have nil title")
}
if err := t.smartSyntax(); err != nil {
return err
}
return nil
}
func (t *Task) smartSyntax() error {
re := regexp.MustCompile(`^(/([+-]{0,1}\d+)?/)?(.*?)(\s+/([^/]*)/$)?$|`)
matches := re.FindAllStringSubmatch(t.Title, 1)[0]
if len(matches) != 6 {
return nil
}
if matches[1] != "" {
t.Priority = form.ToInt(matches[1])
}
if matches[3] != "" {
t.Title = matches[3]
}
if matches[5] != "" {
t.Tags = form.ToStrArr(matches[5])
}
return nil
}

View File

@@ -0,0 +1,59 @@
package task
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestNew(t *testing.T) {
if _, err := New(toReq(map[string]interface{}{})); err != nil {
t.Error(err)
}
if _, err := New(toReq(map[string]interface{}{"title": ""})); err == nil {
t.Error(err)
}
if task, err := New(toReq(map[string]interface{}{"title": "i want dogs /a,b,c/"})); err != nil {
t.Error(err)
} else if task.Title != "i want dogs" {
t.Error(task.Title)
} else if fmt.Sprint(task.Tags) != "[a b c]" {
t.Error(task.Tags)
} else {
was := task.Edited
task.touch()
if was == task.Edited {
t.Error(was)
}
was = task.Edited
task.SetNote("hell\nno")
if was == task.Edited {
t.Error(was)
} else if len(task.Note) != 2 {
t.Error(task.Note)
}
}
}
func toReq(m map[string]interface{}) *http.Request {
if m == nil {
m = map[string]interface{}{}
}
els := map[string]interface{}{
"title": "title",
"prio": 1,
"tags": "a, b,c",
"duedate": "2010-02-03 05:06:07",
"note": "hello\nworld\ni\nam a note\nand\ni have\nlots\nof\nlines",
}
for k := range els {
if _, ok := m[k]; !ok {
m[k] = els[k]
}
}
b, _ := json.Marshal(m)
return httptest.NewRequest("POST", "/paht", bytes.NewReader(b))
}