2014-01-03 00:29:54 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package todotxt
|
|
|
|
|
|
|
|
import (
|
2014-01-03 16:35:38 +00:00
|
|
|
"io/ioutil"
|
2014-01-03 00:29:54 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-01-03 19:00:40 +00:00
|
|
|
var (
|
2014-01-04 13:13:37 +00:00
|
|
|
testInputTasklist = "testdata/tasklist_todo.txt"
|
|
|
|
testInputTasklistCreatedDateError = "testdata/tasklist_createdDate_error.txt"
|
|
|
|
testInputTasklistDueDateError = "testdata/tasklist_dueDate_error.txt"
|
|
|
|
testInputTasklistCompletedDateError = "testdata/tasklist_completedDate_error.txt"
|
|
|
|
testInputTasklistScannerError = "testdata/tasklist_scanner_error.txt"
|
|
|
|
testOutput = "testdata/ouput_todo.txt"
|
|
|
|
testExpectedOutput = "testdata/expected_todo.txt"
|
|
|
|
testTasklist TaskList
|
|
|
|
testExpected interface{}
|
|
|
|
testGot interface{}
|
2014-01-03 19:00:40 +00:00
|
|
|
)
|
|
|
|
|
2014-01-03 00:29:54 +00:00
|
|
|
func TestLoadFromFile(t *testing.T) {
|
2014-01-04 00:43:27 +00:00
|
|
|
file, err := os.Open(testInputTasklist)
|
2014-01-03 00:29:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2014-01-03 19:00:40 +00:00
|
|
|
if testTasklist, err := LoadFromFile(file); err != nil {
|
2014-01-03 00:29:54 +00:00
|
|
|
t.Fatal(err)
|
2014-01-03 12:01:04 +00:00
|
|
|
} else {
|
2014-01-03 19:00:40 +00:00
|
|
|
data, err := ioutil.ReadFile(testExpectedOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testExpected := string(data)
|
|
|
|
testGot := testTasklist.String()
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
|
|
|
|
}
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
|
|
|
|
if testTasklist, err := LoadFromFile(nil); testTasklist != nil || err == nil {
|
|
|
|
t.Errorf("Expected LoadFromFile to fail, but got TaskList back: [%s]", testTasklist)
|
|
|
|
}
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadFromFilename(t *testing.T) {
|
2014-01-04 00:43:27 +00:00
|
|
|
if testTasklist, err := LoadFromFilename(testInputTasklist); err != nil {
|
2014-01-03 00:29:54 +00:00
|
|
|
t.Fatal(err)
|
2014-01-03 12:01:04 +00:00
|
|
|
} else {
|
2014-01-03 19:00:40 +00:00
|
|
|
data, err := ioutil.ReadFile(testExpectedOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testExpected := string(data)
|
|
|
|
testGot := testTasklist.String()
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
|
|
|
|
}
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
|
|
|
|
if testTasklist, err := LoadFromFilename("some_file_that_does_not_exists.txt"); testTasklist != nil || err == nil {
|
|
|
|
t.Errorf("Expected LoadFromFilename to fail, but got TaskList back: [%s]", testTasklist)
|
|
|
|
}
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:00:40 +00:00
|
|
|
func TestWriteFile(t *testing.T) {
|
|
|
|
os.Remove(testOutput)
|
|
|
|
os.Create(testOutput)
|
2014-01-03 00:29:54 +00:00
|
|
|
var err error
|
|
|
|
|
2014-01-04 00:43:27 +00:00
|
|
|
fileInput, err := os.Open(testInputTasklist)
|
2014-01-03 19:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fileInput.Close()
|
|
|
|
fileOutput, err := os.OpenFile(testOutput, os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fileInput.Close()
|
|
|
|
|
|
|
|
if testTasklist, err = LoadFromFile(fileInput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = WriteToFile(&testTasklist, fileOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fileInput.Close()
|
|
|
|
fileOutput, err = os.Open(testOutput)
|
2014-01-03 16:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-03 19:00:40 +00:00
|
|
|
if testTasklist, err = LoadFromFile(fileOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-01-03 16:35:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:00:40 +00:00
|
|
|
data, err := ioutil.ReadFile(testExpectedOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testExpected := string(data)
|
|
|
|
testGot := testTasklist.String()
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
|
2014-01-03 00:29:54 +00:00
|
|
|
}
|
2014-01-03 19:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskListWriteFile(t *testing.T) {
|
|
|
|
os.Remove(testOutput)
|
|
|
|
os.Create(testOutput)
|
|
|
|
testTasklist := TaskList{}
|
2014-01-03 00:29:54 +00:00
|
|
|
|
2014-01-04 00:43:27 +00:00
|
|
|
fileInput, err := os.Open(testInputTasklist)
|
2014-01-03 19:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-01-03 16:35:38 +00:00
|
|
|
}
|
2014-01-03 19:00:40 +00:00
|
|
|
defer fileInput.Close()
|
|
|
|
fileOutput, err := os.OpenFile(testOutput, os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fileInput.Close()
|
|
|
|
|
|
|
|
if err := testTasklist.LoadFromFile(fileInput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := testTasklist.WriteToFile(fileOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fileInput.Close()
|
|
|
|
fileOutput, err = os.Open(testOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := testTasklist.LoadFromFile(fileOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(testExpectedOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testExpected := string(data)
|
|
|
|
testGot := testTasklist.String()
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFilename(t *testing.T) {
|
|
|
|
os.Remove(testOutput)
|
|
|
|
var err error
|
|
|
|
|
2014-01-04 00:43:27 +00:00
|
|
|
if testTasklist, err = LoadFromFilename(testInputTasklist); err != nil {
|
2014-01-03 19:00:40 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = WriteToFilename(&testTasklist, testOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if testTasklist, err = LoadFromFilename(testOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(testExpectedOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testExpected := string(data)
|
|
|
|
testGot := testTasklist.String()
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskListWriteFilename(t *testing.T) {
|
|
|
|
os.Remove(testOutput)
|
|
|
|
testTasklist := TaskList{}
|
|
|
|
|
2014-01-04 00:43:27 +00:00
|
|
|
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
|
2014-01-03 19:00:40 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := testTasklist.WriteToFilename(testOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := testTasklist.LoadFromFilename(testOutput); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(testExpectedOutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testExpected := string(data)
|
|
|
|
testGot := testTasklist.String()
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to be [%s], but got [%s]", testExpected, testGot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskListCount(t *testing.T) {
|
2014-01-04 13:13:37 +00:00
|
|
|
if err := testTasklist.LoadFromFilename(testInputTasklist); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-03 19:00:40 +00:00
|
|
|
|
2014-01-04 00:43:27 +00:00
|
|
|
testExpected := 63
|
2014-01-03 19:00:40 +00:00
|
|
|
testGot := len(testTasklist)
|
|
|
|
if testGot != testExpected {
|
|
|
|
t.Errorf("Expected TaskList to contain %d tasks, but got %d", testExpected, testGot)
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
|
|
|
|
func TestTaskListReadErrors(t *testing.T) {
|
|
|
|
if testTasklist, err := LoadFromFilename(testInputTasklistCreatedDateError); testTasklist != nil || err == nil {
|
|
|
|
t.Errorf("Expected LoadFromFilename to fail because of invalid created date, but got TaskList back: [%s]", testTasklist)
|
|
|
|
} else if err.Error() != `parsing time "2013-13-01": month out of range` {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testTasklist, err := LoadFromFilename(testInputTasklistDueDateError); testTasklist != nil || err == nil {
|
|
|
|
t.Errorf("Expected LoadFromFilename to fail because of invalid due date, but got TaskList back: [%s]", testTasklist)
|
|
|
|
} else if err.Error() != `parsing time "2014-02-32": day out of range` {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testTasklist, err := LoadFromFilename(testInputTasklistCompletedDateError); testTasklist != nil || err == nil {
|
|
|
|
t.Errorf("Expected LoadFromFilename to fail because of invalid completed date, but got TaskList back: [%s]", testTasklist)
|
|
|
|
} else if err.Error() != `parsing time "2014-25-04": month out of range` {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// really silly test
|
|
|
|
if testTasklist, err := LoadFromFilename(testInputTasklistScannerError); testTasklist != nil || err == nil {
|
|
|
|
t.Errorf("Expected LoadFromFilename to fail because of invalid file, but got TaskList back: [%s]", testTasklist)
|
|
|
|
} else if err.Error() != `bufio.Scanner: token too long` {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|