Skip to content

Commit

Permalink
player/lyric: add some unit tests (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven authored Sep 10, 2024
1 parent 9aff59b commit f3bf108
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion feeluown/player/lyric.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ def parse_lyric_text(content: str) -> Dict[int, str]:
>>> r = parse_lyric_text("[00:00.00] 作曲 : 周杰伦\\n[00:01.00] 作词 : 周杰伦\\n")
>>> list(r.items())[0]
(0, ' 作曲 : 周杰伦')
>>> r = parse_lyric_text("[01:30][00:01:10][01:00]再等直至再吻到你")
>>> r = parse_lyric_text("[01:30][01:10][01:00]再等直至再吻到你")
>>> list(r.items())[-1]
(90000, '再等直至再吻到你')
"""
def to_mileseconds(time_str: str):
mileseconds = 0
unit = 1000

# According to wikipedia, the time_str should have the foramt [mm:ss.xx]
t_seq = time_str.split(":", 1)
# Many lyrics have wrong time-tag, and they may look like 'mm:ss:xx'.
# They should be changed to 'mm:ss.xx'. Check #863 for details.
t_seq[1] = t_seq[1].replace(":", ".")
t_seq.reverse()

Expand Down
13 changes: 12 additions & 1 deletion tests/player/test_live_lyric.py → tests/player/test_lyric.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import pytest

from feeluown.player import LiveLyric
from feeluown.player import LiveLyric, parse_lyric_text
from feeluown.library import LyricModel


Expand Down Expand Up @@ -106,3 +106,14 @@ def test_live_lyric_with_trans(app_mock):
assert live_lyric.current_sentence == ''
assert live_lyric.current_line[0] == ''
assert live_lyric.current_line[2] is False


def test_parse_ill_formed_lyric():
result = parse_lyric_text('''
[00:00.00] 作词 : シャノン
[00:00.05] 作曲 : シャノン
[00:00:10]僕らの最後は死別にしよう
[00:05:64]嫌いになりそうな日差しの中
''')
assert result[50] == ' 作曲 : シャノン'
assert result[100] == '僕らの最後は死別にしよう'

0 comments on commit f3bf108

Please sign in to comment.