Repository navigation
Expand file tree
/
Copy pathtest_parser.py
More file actions
184 lines (156 loc) · 5.85 KB
/
Copy pathtest_parser.py
File metadata and controls
184 lines (156 loc) · 5.85 KB
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
174
175
176
177
178
179
180
181
182
183
184
from lrcparser import LrcLine, LrcParser, LrcText, LrcTextSegment, LrcTime
with open("tests/files/example.lrc", encoding="utf-8") as lrc_file:
example = lrc_file.read()
class TestLrcParserGeneral:
result = LrcParser.parse(example)
result_translation = LrcParser.parse(example, parse_translations=True)
timestamps = [
LrcTime(0, 0, 20),
LrcTime(0, 0, 280),
LrcTime(0, 2, 830),
LrcTime(0, 28, 330),
LrcTime(0, 28, 330),
LrcTime(0, 28, 330),
LrcTime(28, 33, 750),
]
def test_parse_attributes(self):
assert self.result["offset"] == 250
assert self.result["attributes"] == {
"ti": "test_lyric",
"al": "TEST ~AVOIDING ERRORS~",
"by": "283375",
"offset": "250",
}
def test_parse_lrc_lines(self):
lrc_lines = self.result["lrc_lines"]
assert len(lrc_lines) == 7
for i, line in enumerate(lrc_lines):
assert line.start_time == self.timestamps[i]
assert line.translations is None
def test_parse_lrc_lines_translations(self):
lrc_lines = self.result_translation["lrc_lines"]
assert len(lrc_lines) == 5
assert lrc_lines[3].text == LrcText(
LrcTextSegment(self.timestamps[3], "Line 4 with translation")
)
assert lrc_lines[3].translations is not None
assert lrc_lines[3].translations == [
LrcText(LrcTextSegment(self.timestamps[3], "一般大家都这么打翻译")),
LrcText(LrcTextSegment(self.timestamps[3], "可惜我更喜欢换行")),
LrcText(
LrcTextSegment(self.timestamps[3], "你说得对,但是《lrcparser》是由……")
),
]
def test_find_duplicate(self):
dups = LrcParser.find_duplicate(self.result["lrc_lines"])
assert dups == [
[
LrcLine(
start_time=LrcTime(0, 28, 330),
text="Line 4 with translation | 一般大家都这么打翻译",
),
LrcLine(
start_time=LrcTime(0, 28, 330),
text="可惜我更喜欢换行",
),
LrcLine(
start_time=LrcTime(0, 28, 330),
text="你说得对,但是《lrcparser》是由……",
),
]
]
def test_combine_translation(self):
translations = LrcParser.combine_translation(self.result["lrc_lines"])
assert translations == [
LrcLine(
start_time=LrcTime(0, 28, 330),
text="Line 4 with translation | 一般大家都这么打翻译",
translations=["可惜我更喜欢换行", "你说得对,但是《lrcparser》是由……"],
)
]
with open("tests/files/example_spec.lrc", encoding="utf-8") as lrc_file:
example_spec = lrc_file.read()
class TestLrcParserSpecial:
result = LrcParser.parse(example_spec)
result_translations = LrcParser.parse(example_spec, parse_translations=True)
timestamps = [
LrcTime(1, 23, 456),
LrcTime(1, 23, 456),
LrcTime(2, 34, 560),
LrcTime(2, 34, 560),
LrcTime(2, 37, 500),
LrcTime(2, 37, 500),
LrcTime(3, 23, 370),
LrcTime(3, 23, 375),
LrcTime(3, 23, 375),
LrcTime(3, 45, 678),
LrcTime(3, 45, 678),
LrcTime(8, 33, 750),
]
def test_parse_attributes(self):
assert self.result["offset"] == 75
assert self.result["attributes"] == {
"ti": "Special LRC File Test",
"al": "TEST",
"by": "283375",
"offset": "75",
}
def test_parse_lrc_lines(self):
lrc_lines = self.result["lrc_lines"]
assert len(lrc_lines) == 12
for i, line in enumerate(lrc_lines):
assert line.start_time == self.timestamps[i]
assert (
lrc_lines[0].text.to_str()[-5:]
== lrc_lines[2].text.to_str()[-5:]
== lrc_lines[9].text.to_str()[-5:]
== "yrics"
)
assert (
len(lrc_lines[4].text) < len(lrc_lines[6].text) == len(lrc_lines[11].text)
)
_4_word_timestamps = [
LrcTime(2, 37, 500),
LrcTime(2, 37, 520),
LrcTime(2, 37, 550),
]
for i, segment in enumerate(lrc_lines[4].text):
assert isinstance(segment, LrcTextSegment)
assert segment.time == _4_word_timestamps[i]
def test_parse_lrc_lines_translations(self):
lrc_lines = self.result_translations["lrc_lines"]
assert (
lrc_lines[3].text.to_str(force_word_timestamp=False)
== "Totally outrageous line"
)
assert lrc_lines[3].translations is not None
assert lrc_lines[3].translations == [
LrcText(
LrcTextSegment(LrcTime(3, 23, 375), "?"),
LrcTextSegment(LrcTime(3, 24, 475), "?"),
LrcTextSegment(LrcTime(3, 25, 575), "?"),
),
]
class TestLrcParserLoose:
def test_loose_timestamps(self):
with open("tests/files/example_loose.lrc", encoding="utf-8") as lrc_file:
loose = lrc_file.read()
result = LrcParser.parse(loose)
assert result["offset"] == 100
assert result["attributes"] == {
"ti": "loose test",
"ar": "someone",
"offset": "100",
}
assert [str(line.start_time) for line in result["lrc_lines"]] == [
"00:01.50",
"00:12.00",
"01:02.00",
"75:33.75",
]
assert [str(line.text) for line in result["lrc_lines"]] == [
"single digit minute, one decimal digit",
"no milliseconds",
"minimal",
"minutes overflow",
]