diff options
Diffstat (limited to 'tests/parser_unittest.py')
-rw-r--r-- | tests/parser_unittest.py | 73 |
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") |