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 == 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.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")