76 lines
2.7 KiB
Python
Executable File
76 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import unittest
|
|
import sub
|
|
import os
|
|
|
|
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")
|
|
|
|
class TestGetSub(unittest.TestCase):
|
|
def test_no_sub(self):
|
|
self.assertEqual(sub.getSub("(Z) gibberish"), None)
|
|
|
|
def test_sub_at_bol(self):
|
|
self.assertEqual(sub.getSub("sub/:test gibberish"), ["sub", "test"])
|
|
|
|
def test_sub_at_eol(self):
|
|
self.assertEqual(sub.getSub("(Z) gibberish sub/:test"), ["sub", "test"])
|
|
|
|
def test_sub_mid_line(self):
|
|
self.assertEqual(sub.getSub("(Z) sub/:test gibberish"), ["sub", "test"])
|
|
|
|
def test_sub_missing_folder(self):
|
|
self.assertEqual(sub.getSub("(Z) gibberish /:test"), None)
|
|
|
|
def test_sub_missing_file(self):
|
|
self.assertEqual(sub.getSub("(Z) sub/: gibberish"), None)
|
|
|
|
def setup_environment(path):
|
|
os.environ["TODO_DIR"] = os.path.join(TEST_DATA_DIR, path)
|
|
os.environ["TODO_FILE"] = os.path.join(TEST_DATA_DIR, path, "todo.txt")
|
|
os.environ["DONE_FILE"] = os.path.join(TEST_DATA_DIR, path, "done.txt")
|
|
os.environ["REPORT_FILE"] = os.path.join(TEST_DATA_DIR, path, "report.txt")
|
|
os.environ["TODOTXT_CFG_FILE"] = os.path.join(TEST_DATA_DIR, "todo.cfg")
|
|
|
|
class TestShowSub(unittest.TestCase):
|
|
def test_show(self):
|
|
setup_environment("test1")
|
|
|
|
output = sub.showSub("(Z) gibberish sub/:test1.txt\n", color=False)
|
|
self.assertEqual(output, "1 this is test 1\n")
|
|
|
|
class TestShowAll(unittest.TestCase):
|
|
def test_show_all_one(self):
|
|
self.show_all_tester("test1", "1 (Z) gibberish sub/:test1.txt\n 1 this is test 1\n--\nTODO: 1 of 1 tasks shown\n")
|
|
|
|
def test_show_all_two(self):
|
|
self.show_all_tester("test2", "1 (Z) gibberish sub/:test1.txt\n 1 this is test 1\n2 (Z) more gibberish sub/:test2.txt\n 1 this is test 2\n--\nTODO: 2 of 2 tasks shown\n")
|
|
|
|
def show_all_tester(self, path, expected_output):
|
|
from io import StringIO
|
|
setup_environment(path)
|
|
|
|
out = StringIO()
|
|
sub.showAll(out=out, color=False)
|
|
output = out.getvalue()
|
|
self.assertEqual(output, expected_output)
|
|
|
|
class TestAdd(unittest.TestCase):
|
|
def setUp(self):
|
|
import shutil
|
|
shutil.rmtree(os.path.join(TEST_DATA_DIR, "test3"))
|
|
os.mkdir(os.path.join(TEST_DATA_DIR, "test3"))
|
|
|
|
def test_add_sub(self):
|
|
with open(os.path.join(TEST_DATA_DIR, "test3/todo.txt"), "w") as f:
|
|
f.write("(Z) gibberish")
|
|
setup_environment("test3")
|
|
sub.addSub(1, "sub", False)
|
|
out = sub.getSub(sub.getItem(1))
|
|
self.assertTrue(out is not None)
|
|
fileExists = os.path.isfile(os.path.join(os.getenv("TODO_DIR"), out[0], out[1]))
|
|
self.assertTrue(fileExists)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|