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