File size: 5,429 Bytes
70954e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Incomplete implementation of the style guide.
"""
import logging
import re
import unicodedata

import pytest

LOG = logging.getLogger(__name__)


def test_starts_with_upper(lines):
    failures = []
    for line, line_number, file in lines:
        word_chars = re.findall(r"\w", line)
        if word_chars and word_chars[0] != word_chars[0].upper():
            failures.append(
                f"{file}:{line_number}: Lower case letter at the beginning of the line.\n{line}\n^"
            )
    if failures:
        pytest.fail("\n".join(failures))


def test_multiple_empty_lines(lines):
    failures = []
    last_line_empty = False
    for line, line_number, file in lines:
        if not line and last_line_empty:
            failures.append(
                f"{file}:{line_number-1}-{line_number}: Multiple empty lines."
            )
        last_line_empty = not line
    if failures:
        pytest.fail("\n".join(failures))


@pytest.mark.parametrize(
    "heading", ["hook", "verse", "refrain", "ref.", "chorus", "coro"]
)
def test_section_headings(lines, heading):
    for line, line_number, file in lines:
        if line:
            if heading in line.lower():
                LOG.warning(
                    f"{file}:{line_number}: Suspect heading \"{heading}\".\n{line}\n{'': <{line.lower().index(heading)}}^"
                )


@pytest.mark.parametrize("punctuation", ",.")
def test_line_ends_with_forbidden_punctuation(lines, punctuation):
    failures = []
    for line, line_number, file in lines:
        if line:
            if line[-1] == punctuation:
                failures.append(
                    f"{file}:{line_number}: \"{punctuation}\" at EOL.\n{line}\n{'': <{len(line)-1}}^"
                )
    if failures:
        pytest.fail("\n".join(failures))


@pytest.mark.parametrize("exaggeration", ["!!", "??", "ohh", "ahh", ".."])
def test_line_contains_exaggerations(lines, exaggeration):
    failures = []
    for line, line_number, file in lines:
        if line:
            if exaggeration in line:
                failures.append(
                    f"{file}:{line_number}: Forbidden exaggeration: \"{exaggeration}\".\n{line}\n{'': <{line.index(exaggeration)}}^"
                )
    if failures:
        pytest.fail("\n".join(failures))


_ALLOWED_CHAR_CATEGORIES = "LNPZ"


def test_allowed_character_categories(lines):
    failures = []
    for line, line_number, file in lines:
        for char in line:
            if unicodedata.category(char)[0] not in _ALLOWED_CHAR_CATEGORIES:
                failures.append(
                    f"{file}:{line_number}: Forbidden character: {repr(char)} (category {unicodedata.category(char)}).\n{line}\n{'': <{line.index(char)}}^"
                )
    if failures:
        pytest.fail("\n".join(failures))


_ALLOWED_WHITESPACE = " \n"


def test_allowed_whitespace(lines):
    failures = []
    for line, line_number, file in lines:
        for char in line:
            if unicodedata.category(char)[0] == "Z" and char not in _ALLOWED_WHITESPACE:
                failures.append(
                    f"{file}:{line_number}: Forbidden whitespace: {repr(char)}.\n{line}\n{'': <{line.index(char)}}^"
                )
    if failures:
        pytest.fail("\n".join(failures))


_ALLOWED_PUNCTUATION = ".,¿?¡!'\"«»()-*"


def test_allowed_punctuation(lines):
    failures = []
    for line, line_number, file in lines:
        for char in line:
            if (
                unicodedata.category(char)[0] == "P"
                and char not in _ALLOWED_PUNCTUATION
            ):
                failures.append(
                    f"{file}:{line_number}: Forbidden punctuation: {repr(char)}.\n{line}\n{'': <{line.index(char)}}^"
                )
    if failures:
        pytest.fail("\n".join(failures))


def test_first_or_last_line_is_empty(lines):
    failures = []
    last_line = ("", 1000, None)
    line = ""
    file = None
    line_number = 0
    for line, line_number, file in lines:
        if line_number == 1:
            if len(line.strip()) == 0:
                failures.append(f"{file}:{line_number}: First line is empty.")
        if last_line[1] > line_number and last_line[2] is not None:
            if len(last_line[0].strip()) == 0:
                failures.append(f"{last_line[2]}:{last_line[1]}: Last line is empty.")
        last_line = (line, line_number, file)

    # very last line
    if line_number != 0 and len(line.strip()) == 0:
        failures.append(f"{file}:{line_number}: Last line is empty.")

    if failures:
        pytest.fail("\n".join(failures))


def test_lines_contains_unnecessary_whitespace(lines):
    failures = []
    for line, line_number, file in lines:
        if line:
            if line.rstrip() != line:
                failures.append(
                    f'{file}:{line_number}: Trailing whitespace.\n"{line}" <--'
                )
            if line.lstrip() != line:
                failures.append(
                    f'{file}:{line_number}: Leading whitespace.\n--> "{line}"'
                )

    if failures:
        pytest.fail("\n".join(failures))


def test_unicode_nfkc(lines):
    failures = []
    for line, line_number, file in lines:
        if line:
            if line != unicodedata.normalize("NFKC", line):
                failures.append(f"{file}:{line_number}: Not in NFKC form.")

    if failures:
        pytest.fail("\n".join(failures))