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 00:43:27 +00:00
|
|
|
testInputTasklist = "testdata/tasklist_todo.txt"
|
2014-01-03 19:00:40 +00:00
|
|
|
testOutput = "testdata/ouput_todo.txt"
|
|
|
|
testExpectedOutput = "testdata/expected_todo.txt"
|
|
|
|
testTasklist TaskList
|
|
|
|
testExpected interface{}
|
|
|
|
testGot interface{}
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 00:43:27 +00:00
|
|
|
testTasklist.LoadFromFilename(testInputTasklist)
|
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)
|
|
|
|
}
|
|
|
|
}
|