summaryrefslogtreecommitdiffstats
path: root/tests/parser_unittest.py
blob: 80763694f98138cf28c43061ff4dd8920038c43f (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
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")