go-todotxt/todotxt_test.go

203 lines
4.7 KiB
Go
Raw Permalink Normal View History

/* 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"
"os"
"testing"
)
2014-01-03 19:00:40 +00:00
var (
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{}
)
func TestLoadFromFile(t *testing.T) {
file, err := os.Open(testInputTasklist)
if err != nil {
t.Fatal(err)
}
defer file.Close()
2014-01-03 19:00:40 +00:00
if testTasklist, err := LoadFromFile(file); err != nil {
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)
}
}
}
func TestLoadFromFilename(t *testing.T) {
if testTasklist, err := LoadFromFilename(testInputTasklist); err != nil {
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 19:00:40 +00:00
func TestWriteFile(t *testing.T) {
os.Remove(testOutput)
os.Create(testOutput)
var err error
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 19:00:40 +00:00
}
func TestTaskListWriteFile(t *testing.T) {
os.Remove(testOutput)
os.Create(testOutput)
testTasklist := TaskList{}
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
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{}
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) {
testTasklist.LoadFromFilename(testInputTasklist)
2014-01-03 19:00:40 +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)
}
}