diff --git a/.gitignore b/.gitignore index bcb39fd..0c05e79 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /History/ /Private/ /__pycache__/ +/Tests/Temp/ \ No newline at end of file diff --git a/Tests/Unit/test_log.py b/Tests/Unit/test_log.py new file mode 100644 index 0000000..5b18b41 --- /dev/null +++ b/Tests/Unit/test_log.py @@ -0,0 +1,96 @@ +import unittest + +import _log +import _defaults +import _about +import _strings + +class TestLog(unittest.TestCase): + def setUp(self): + pass + + def test_init(self): + log = _log.Log(".") + + self.assertTrue(log.path.startswith("./")) + self.assertTrue(log.path.endswith(".txt")) + + self.assertFalse(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + self.assertEqual(log.content, []) + self.assertEqual(log.summary, []) + + self.assertEqual(log.repeated_line, "") + self.assertEqual(log.repeated_count, 0) + + def test_report(self): + # will test all strings in all languages + for language in _strings.languages: + _strings.strings = _strings.languages[language] + + for string in _strings.strings.keys(): + if string.startswith("email"): + continue + + log = _log.Log(".") + detail = "test" + log.report(string, detail=detail) + + if string.startswith("ok"): + self.assertFalse(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + elif string.startswith("warning"): + self.assertFalse(log.error_occurred) + self.assertTrue(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + elif string.startswith("error"): + self.assertTrue(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + elif string.startswith("message"): + self.assertFalse(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + elif string.startswith("prefix"): + self.assertFalse(log.error_occurred) + self.assertFalse(log.warning_occurred) + self.assertFalse(log.sync_occurred) + + def test_insert(self): + # will test all strings in all languages + for language in _strings.languages: + _strings.strings = _strings.languages[language] + + for string in _strings.strings.keys(): + if string.startswith("email"): + continue + + log = _log.Log(".") + detail = "test" + log.insert(string, detail=detail) + self.assertEqual(log.content[0][18:], _strings.strings[string] + detail) + + def test_get_content(self): + log = _log.Log(".") + log.content = ["test"] + self.assertEqual(log.get_content(), _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\ntest\n") + + def test_write(self): + log = _log.Log("Tests/Temp") + log.content = ["test"] + log.write() + fl = open(log.path, "r") + text = fl.read() + fl.close() + self.assertEqual(text, _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\ntest\n") + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/_log.py b/_log.py index fef5ee8..576bb26 100644 --- a/_log.py +++ b/_log.py @@ -14,7 +14,8 @@ import datetime class Log: - def __init__(self, root): # initializes an empty log + # initializes an empty log + def __init__(self, root): # test in Tests/Unit/test_log.test_init self.path = os.path.join(root, _paths.logs_folder, time.strftime("%Y-%m-%d %H-%M-%S") + ".txt") self.error_occurred = False @@ -27,7 +28,8 @@ def __init__(self, root): # initializes an empty log self.repeated_line = "" self.repeated_count = 0 - def report(self, id, detail="", critical=False): # reports events in the log + # reports events in the log + def report(self, id, detail="", critical=False): # test in Tests/Unit/test_log.test_report if id.startswith("error"): self.error_occurred = True @@ -54,7 +56,8 @@ def report(self, id, detail="", critical=False): # reports events in the log return True - def insert(self, id, detail): + # will insert a line in the log file + def insert(self, id, detail): # test in Tests/Unit/test_log.test_insert timestamp = datetime.datetime.now().strftime("[%H:%M:%S]") if id.startswith("ok"): @@ -63,14 +66,17 @@ def insert(self, id, detail): self.content.append(timestamp + _strings.strings["prefix_error"] + _strings.strings[id] + detail) elif id.startswith("warning"): self.content.append(timestamp + _strings.strings["prefix_warning"] + _strings.strings[id] + detail) + elif id.startswith("prefix"): + self.content.append(timestamp + _strings.strings["prefix_spacing"] + _strings.strings[id] + detail) elif id.startswith("message"): - self.content.append(timestamp + _strings.strings[id] + detail) + self.content.append(timestamp + _strings.strings["prefix_spacing"] + _strings.strings[id] + detail) else: # skip timestamp in this case but include some spacing for indentation self.content.append(_strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + id.replace("\n", "\n" + _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"]) + detail) - def get_content(self): + # returns the full log file in the correct format + def get_content(self): # test in Tests/Unit/test_log.test_get_content string = "" string += _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\n" @@ -79,7 +85,8 @@ def get_content(self): return string - def write(self): # writes the message to the file system + # writes the message to the file system + def write(self): # test in Tests/Unit/test_log.test_write try: self.file = open(self.path, "w") self.file.write(self.get_content()) diff --git a/_strings.py b/_strings.py index ad7e0c3..d59787a 100644 --- a/_strings.py +++ b/_strings.py @@ -102,7 +102,7 @@ "warning_file_evaluate_mathematical_comparison_not_identified": "\tmathematical comparison not identified: ", # Log - "message_repeated_lines": " \tthe previous line was repeated this more times: " + "message_repeated_lines": "\tthe previous line was repeated this more times: " } languages = {