GetTask bugfix
correctly return pointer to Task inside TaskList.
This commit is contained in:
parent
e29d17784e
commit
2e04f1d233
19
todotxt.go
19
todotxt.go
@ -43,7 +43,7 @@ func (tasklist TaskList) String() (text string) {
|
|||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTask appends a task to the current TaskList and takes care to set the Task.Id correctly, modifying the Task by the given pointer!
|
// AddTask appends a Task to the current TaskList and takes care to set the Task.Id correctly, modifying the Task by the given pointer!
|
||||||
func (tasklist *TaskList) AddTask(task *Task) {
|
func (tasklist *TaskList) AddTask(task *Task) {
|
||||||
task.Id = 0
|
task.Id = 0
|
||||||
for _, t := range *tasklist {
|
for _, t := range *tasklist {
|
||||||
@ -56,18 +56,19 @@ func (tasklist *TaskList) AddTask(task *Task) {
|
|||||||
*tasklist = append(*tasklist, *task)
|
*tasklist = append(*tasklist, *task)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTask returns task by given task 'id' from the TaskList. Returns an error if task could not be found.
|
// GetTask returns a Task by given task 'id' from the TaskList. The returned Task pointer can be used to update the Task inside the TaskList.
|
||||||
|
// Returns an error if Task could not be found.
|
||||||
func (tasklist *TaskList) GetTask(id int) (*Task, error) {
|
func (tasklist *TaskList) GetTask(id int) (*Task, error) {
|
||||||
for _, t := range *tasklist {
|
for i := range *tasklist {
|
||||||
if t.Id == id {
|
if ([]Task(*tasklist))[i].Id == id {
|
||||||
return &t, nil
|
return &([]Task(*tasklist))[i], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, errors.New("task not found")
|
return nil, errors.New("task not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTaskById removes any task with given task 'id' from the TaskList.
|
// RemoveTaskById removes any Task with given Task 'id' from the TaskList.
|
||||||
// Returns an error if no task was removed.
|
// Returns an error if no Task was removed.
|
||||||
func (tasklist *TaskList) RemoveTaskById(id int) error {
|
func (tasklist *TaskList) RemoveTaskById(id int) error {
|
||||||
var newList TaskList
|
var newList TaskList
|
||||||
|
|
||||||
@ -87,8 +88,8 @@ func (tasklist *TaskList) RemoveTaskById(id int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTask removes any task from the TaskList with the same String representation as the given task.
|
// RemoveTask removes any Task from the TaskList with the same String representation as the given Task.
|
||||||
// Returns an error if no task was removed.
|
// Returns an error if no Task was removed.
|
||||||
func (tasklist *TaskList) RemoveTask(task Task) error {
|
func (tasklist *TaskList) RemoveTask(task Task) error {
|
||||||
var newList TaskList
|
var newList TaskList
|
||||||
|
|
||||||
|
@ -329,6 +329,52 @@ func TestTaskListGetTask(t *testing.T) {
|
|||||||
taskId++
|
taskId++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTaskListUpdateTask(t *testing.T) {
|
||||||
|
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
taskId := 3
|
||||||
|
task, err := testTasklist.GetTask(taskId)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
testExpected = "(B) 2013-12-01 Outline chapter 5 @Computer +Novel Level:5 private:false due:2014-02-17"
|
||||||
|
testGot = task.String()
|
||||||
|
if testGot != testExpected {
|
||||||
|
t.Errorf("Expected Task[%d] to be [%s], but got [%s]", taskId, testExpected, testGot)
|
||||||
|
}
|
||||||
|
testExpected = 3
|
||||||
|
testGot = testTasklist[taskId-1].Id
|
||||||
|
if testGot != testExpected {
|
||||||
|
t.Errorf("Expected Task[%d] to be [%d], but got [%d]", taskId, testExpected, testGot)
|
||||||
|
}
|
||||||
|
|
||||||
|
task.Priority = "C"
|
||||||
|
task.Todo = "Go home!"
|
||||||
|
date, err := time.Parse(DateLayout, "2011-11-11")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
task.DueDate = date
|
||||||
|
testGot := task
|
||||||
|
|
||||||
|
os.Remove(testOutput)
|
||||||
|
if err := testTasklist.WriteToFilename(testOutput); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := testTasklist.LoadFromFilename(testOutput); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
testExpected, err := testTasklist.GetTask(taskId)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if testGot.Task() != testExpected.Task() {
|
||||||
|
t.Errorf("Expected Task to be [%v]\n, but got [%v]", testExpected, testGot)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTaskListRemoveTaskById(t *testing.T) {
|
func TestTaskListRemoveTaskById(t *testing.T) {
|
||||||
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
|
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user