summaryrefslogtreecommitdiffstats
path: root/src/parser_unittest.py
blob: 9ada2657b6d6c29a6fae97f1f80b2f0ae0cb1929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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")