summaryrefslogtreecommitdiffstats
path: root/src/parser_unittest.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser_unittest.py')
-rw-r--r--src/parser_unittest.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/parser_unittest.py b/src/parser_unittest.py
new file mode 100644
index 0000000..02638e6
--- /dev/null
+++ b/src/parser_unittest.py
@@ -0,0 +1,79 @@
+import pytest
+import parser
+from pathlib import Path
+
+# Happy path
+def test_validFile(tmp_path):
+ file_contents = """
+
+FRONT
+
+
+Foo
+
+Bar
+
+BACK
+
+
+Fizz
+
+Buzz
+
+
+
+FRONT
+
+Another card
+
+BACK
+
+Another back
+
+
+ """
+ expected = [
+ ("Foo\n\nBar", "Fizz\n\nBuzz"),
+ ("Another card", "Another back")
+ ]
+
+ path = tmp_path / "valid_file.fcard"
+ with open(path, "w") as f:
+ f.write(file_contents)
+
+ cards = parser.parse(path)
+
+ assert expected == [(c.front, c.back) for c in cards]
+
+ # Cards have unique IDs
+ assert len(set([c.id for c in cards])) == len(cards)
+
+# Edge cases
+def test_emptyFile(tmp_path):
+ path = tmp_path / "empty.fcard"
+ with open(path, "w") as f:
+ f.write("")
+
+ cards = parser.parse(path)
+ assert cards == []
+
+def test_missingFile(tmp_path):
+ cards = parser.parse(tmp_path / "missing_file.fcard")
+ assert cards == []
+
+def checkException(tmp_path, file_contents):
+ path = tmp_path / "invalid_file.fcard"
+ with open(path, "w") as f:
+ f.write(file_contents)
+
+ with pytest.raises(Exception):
+ cards = parser.parse(path)
+
+def test_doesNotStartWithFront(tmp_path):
+ checkException(tmp_path, "BACK\noops")
+
+def test_frontTwiceInARow(tmp_path):
+ checkException(tmp_path, "FRONT\noops\nFRONT\nbad")
+
+def test_doesNotEndWithBack(tmp_path):
+ checkException(tmp_path, "FRONT\ntest\nBACK\ntest\nFRONT\noops")