summaryrefslogtreecommitdiffstats
path: root/tests/parser_unittest.py
diff options
context:
space:
mode:
authorEddy Pedroni <epedroni@pm.me>2024-09-26 10:02:15 +0200
committerEddy Pedroni <epedroni@pm.me>2024-09-26 10:02:15 +0200
commite65bef9c22244fc9bcd22a37d335f5f76ba16ff5 (patch)
tree9af6fa41bfee6fc03c3ab30cf1b23a82bdf8f2e7 /tests/parser_unittest.py
parentce76b00d7b2ccac6843732f92becfabb753864a0 (diff)
Create separate packages for library and CLI
Diffstat (limited to 'tests/parser_unittest.py')
-rw-r--r--tests/parser_unittest.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/parser_unittest.py b/tests/parser_unittest.py
new file mode 100644
index 0000000..8076369
--- /dev/null
+++ b/tests/parser_unittest.py
@@ -0,0 +1,73 @@
+import pytest
+from pathlib import Path
+
+from flashcards import parser
+
+# 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.parseFile(path)
+
+ assert expected == set(cards.values())
+
+# Edge cases
+def test_emptyFile(tmp_path):
+ path = tmp_path / "empty.fcard"
+ with open(path, "w") as f:
+ f.write("")
+
+ cards = parser.parseFile(path)
+ 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.parseFile(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")