Datasets:
Add tests
Browse files- tests/conftest.py +31 -0
- tests/test_format.py +172 -0
- tests/test_grammar.py +32 -0
tests/conftest.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
|
6 |
+
@pytest.fixture()
|
7 |
+
def lyrics_files():
|
8 |
+
return [f for f in Path("lyrics").glob("*.txt")]
|
9 |
+
|
10 |
+
|
11 |
+
@pytest.fixture()
|
12 |
+
def lyrics(lyrics_files):
|
13 |
+
lyrics = []
|
14 |
+
for lyric_file in lyrics_files:
|
15 |
+
with open(lyric_file, "r", encoding="utf-8") as file:
|
16 |
+
lyrics.append((file.read(), str(lyric_file.name)))
|
17 |
+
return lyrics
|
18 |
+
|
19 |
+
|
20 |
+
@pytest.fixture()
|
21 |
+
def lines(lyrics):
|
22 |
+
all_lines = []
|
23 |
+
|
24 |
+
for lyric in lyrics:
|
25 |
+
line_number = 0
|
26 |
+
lines = lyric[0].splitlines()
|
27 |
+
for line in lines:
|
28 |
+
line_number += 1
|
29 |
+
all_lines.append((line, line_number, lyric[1]))
|
30 |
+
|
31 |
+
return all_lines
|
tests/test_format.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Incomplete implementation of the style guide.
|
3 |
+
"""
|
4 |
+
import logging
|
5 |
+
import re
|
6 |
+
import unicodedata
|
7 |
+
|
8 |
+
import pytest
|
9 |
+
|
10 |
+
LOG = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
|
13 |
+
def test_starts_with_upper(lines):
|
14 |
+
failures = []
|
15 |
+
for line, line_number, file in lines:
|
16 |
+
word_chars = re.findall(r"\w", line)
|
17 |
+
if word_chars and word_chars[0] != word_chars[0].upper():
|
18 |
+
failures.append(
|
19 |
+
f"{file}:{line_number}: Lower case letter at the beginning of the line.\n{line}\n^"
|
20 |
+
)
|
21 |
+
if failures:
|
22 |
+
pytest.fail("\n".join(failures))
|
23 |
+
|
24 |
+
|
25 |
+
def test_multiple_empty_lines(lines):
|
26 |
+
failures = []
|
27 |
+
last_line_empty = False
|
28 |
+
for line, line_number, file in lines:
|
29 |
+
if not line and last_line_empty:
|
30 |
+
failures.append(
|
31 |
+
f"{file}:{line_number-1}-{line_number}: Multiple empty lines."
|
32 |
+
)
|
33 |
+
last_line_empty = not line
|
34 |
+
if failures:
|
35 |
+
pytest.fail("\n".join(failures))
|
36 |
+
|
37 |
+
|
38 |
+
@pytest.mark.parametrize(
|
39 |
+
"heading", ["hook", "verse", "refrain", "ref.", "chorus", "coro"]
|
40 |
+
)
|
41 |
+
def test_section_headings(lines, heading):
|
42 |
+
for line, line_number, file in lines:
|
43 |
+
if line:
|
44 |
+
if heading in line.lower():
|
45 |
+
LOG.warning(
|
46 |
+
f"{file}:{line_number}: Suspect heading \"{heading}\".\n{line}\n{'': <{line.lower().index(heading)}}^"
|
47 |
+
)
|
48 |
+
|
49 |
+
|
50 |
+
@pytest.mark.parametrize("punctuation", ",.")
|
51 |
+
def test_line_ends_with_forbidden_punctuation(lines, punctuation):
|
52 |
+
failures = []
|
53 |
+
for line, line_number, file in lines:
|
54 |
+
if line:
|
55 |
+
if line[-1] == punctuation:
|
56 |
+
failures.append(
|
57 |
+
f"{file}:{line_number}: \"{punctuation}\" at EOL.\n{line}\n{'': <{len(line)-1}}^"
|
58 |
+
)
|
59 |
+
if failures:
|
60 |
+
pytest.fail("\n".join(failures))
|
61 |
+
|
62 |
+
|
63 |
+
@pytest.mark.parametrize("exaggeration", ["!!", "??", "ohh", "ahh", ".."])
|
64 |
+
def test_line_contains_exaggerations(lines, exaggeration):
|
65 |
+
failures = []
|
66 |
+
for line, line_number, file in lines:
|
67 |
+
if line:
|
68 |
+
if exaggeration in line:
|
69 |
+
failures.append(
|
70 |
+
f"{file}:{line_number}: Forbidden exaggeration: \"{exaggeration}\".\n{line}\n{'': <{line.index(exaggeration)}}^"
|
71 |
+
)
|
72 |
+
if failures:
|
73 |
+
pytest.fail("\n".join(failures))
|
74 |
+
|
75 |
+
|
76 |
+
_ALLOWED_CHAR_CATEGORIES = "LNPZ"
|
77 |
+
|
78 |
+
|
79 |
+
def test_allowed_character_categories(lines):
|
80 |
+
failures = []
|
81 |
+
for line, line_number, file in lines:
|
82 |
+
for char in line:
|
83 |
+
if unicodedata.category(char)[0] not in _ALLOWED_CHAR_CATEGORIES:
|
84 |
+
failures.append(
|
85 |
+
f"{file}:{line_number}: Forbidden character: {repr(char)} (category {unicodedata.category(char)}).\n{line}\n{'': <{line.index(char)}}^"
|
86 |
+
)
|
87 |
+
if failures:
|
88 |
+
pytest.fail("\n".join(failures))
|
89 |
+
|
90 |
+
|
91 |
+
_ALLOWED_WHITESPACE = " \n"
|
92 |
+
|
93 |
+
|
94 |
+
def test_allowed_whitespace(lines):
|
95 |
+
failures = []
|
96 |
+
for line, line_number, file in lines:
|
97 |
+
for char in line:
|
98 |
+
if unicodedata.category(char)[0] == "Z" and char not in _ALLOWED_WHITESPACE:
|
99 |
+
failures.append(
|
100 |
+
f"{file}:{line_number}: Forbidden whitespace: {repr(char)}.\n{line}\n{'': <{line.index(char)}}^"
|
101 |
+
)
|
102 |
+
if failures:
|
103 |
+
pytest.fail("\n".join(failures))
|
104 |
+
|
105 |
+
|
106 |
+
_ALLOWED_PUNCTUATION = ".,¿?¡!'\"«»()-*"
|
107 |
+
|
108 |
+
|
109 |
+
def test_allowed_punctuation(lines):
|
110 |
+
failures = []
|
111 |
+
for line, line_number, file in lines:
|
112 |
+
for char in line:
|
113 |
+
if (
|
114 |
+
unicodedata.category(char)[0] == "P"
|
115 |
+
and char not in _ALLOWED_PUNCTUATION
|
116 |
+
):
|
117 |
+
failures.append(
|
118 |
+
f"{file}:{line_number}: Forbidden punctuation: {repr(char)}.\n{line}\n{'': <{line.index(char)}}^"
|
119 |
+
)
|
120 |
+
if failures:
|
121 |
+
pytest.fail("\n".join(failures))
|
122 |
+
|
123 |
+
|
124 |
+
def test_first_or_last_line_is_empty(lines):
|
125 |
+
failures = []
|
126 |
+
last_line = ("", 1000, None)
|
127 |
+
line = ""
|
128 |
+
file = None
|
129 |
+
line_number = 0
|
130 |
+
for line, line_number, file in lines:
|
131 |
+
if line_number == 1:
|
132 |
+
if len(line.strip()) == 0:
|
133 |
+
failures.append(f"{file}:{line_number}: First line is empty.")
|
134 |
+
if last_line[1] > line_number and last_line[2] is not None:
|
135 |
+
if len(last_line[0].strip()) == 0:
|
136 |
+
failures.append(f"{last_line[2]}:{last_line[1]}: Last line is empty.")
|
137 |
+
last_line = (line, line_number, file)
|
138 |
+
|
139 |
+
# very last line
|
140 |
+
if line_number != 0 and len(line.strip()) == 0:
|
141 |
+
failures.append(f"{file}:{line_number}: Last line is empty.")
|
142 |
+
|
143 |
+
if failures:
|
144 |
+
pytest.fail("\n".join(failures))
|
145 |
+
|
146 |
+
|
147 |
+
def test_lines_contains_unnecessary_whitespace(lines):
|
148 |
+
failures = []
|
149 |
+
for line, line_number, file in lines:
|
150 |
+
if line:
|
151 |
+
if line.rstrip() != line:
|
152 |
+
failures.append(
|
153 |
+
f'{file}:{line_number}: Trailing whitespace.\n"{line}" <--'
|
154 |
+
)
|
155 |
+
if line.lstrip() != line:
|
156 |
+
failures.append(
|
157 |
+
f'{file}:{line_number}: Leading whitespace.\n--> "{line}"'
|
158 |
+
)
|
159 |
+
|
160 |
+
if failures:
|
161 |
+
pytest.fail("\n".join(failures))
|
162 |
+
|
163 |
+
|
164 |
+
def test_unicode_nfkc(lines):
|
165 |
+
failures = []
|
166 |
+
for line, line_number, file in lines:
|
167 |
+
if line:
|
168 |
+
if line != unicodedata.normalize("NFKC", line):
|
169 |
+
failures.append(f"{file}:{line_number}: Not in NFKC form.")
|
170 |
+
|
171 |
+
if failures:
|
172 |
+
pytest.fail("\n".join(failures))
|
tests/test_grammar.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Run through language tool, to get some hints.
|
3 |
+
"""
|
4 |
+
import logging
|
5 |
+
import langid
|
6 |
+
import language_tool_python
|
7 |
+
|
8 |
+
LOG = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
|
11 |
+
def test_report_language_tool_warnings(lyrics):
|
12 |
+
lyrics_by_language = {}
|
13 |
+
for lyric, file in lyrics:
|
14 |
+
lang = langid.classify(lyric)[0]
|
15 |
+
if lang in {"en", "fr", "es", "de"}:
|
16 |
+
lyrics_by_language.setdefault(lang, []).append((lyric, file))
|
17 |
+
else:
|
18 |
+
LOG.warning(
|
19 |
+
f'Failed to correctly identify language of {file}. Guessed "{lang}". Skipping.'
|
20 |
+
)
|
21 |
+
|
22 |
+
for lang, lyrics_list in lyrics_by_language.items():
|
23 |
+
LOG.info(f'Checking lyrics in "{lang}" with language tool.')
|
24 |
+
tool = language_tool_python.LanguageTool(
|
25 |
+
lang, config={"disabledRuleIds": "GONNA,OUTTA,WANNA,GOTTA,Y_ALL"}
|
26 |
+
)
|
27 |
+
try:
|
28 |
+
for lyric, file in lyrics_list:
|
29 |
+
for match in tool.check(lyric):
|
30 |
+
LOG.warning(f"{file}: {match}")
|
31 |
+
finally:
|
32 |
+
tool.close()
|