From f3bf10895140034b26d9bd43dd73716fdef2615b Mon Sep 17 00:00:00 2001 From: Shaowen Yin Date: Tue, 10 Sep 2024 21:08:18 +0800 Subject: [PATCH] player/lyric: add some unit tests (#871) --- feeluown/player/lyric.py | 5 ++++- tests/player/{test_live_lyric.py => test_lyric.py} | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) rename tests/player/{test_live_lyric.py => test_lyric.py} (89%) diff --git a/feeluown/player/lyric.py b/feeluown/player/lyric.py index c7fcf01d68..f2b23e91ad 100644 --- a/feeluown/player/lyric.py +++ b/feeluown/player/lyric.py @@ -52,7 +52,7 @@ 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, '再等直至再吻到你') """ @@ -60,7 +60,10 @@ 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() diff --git a/tests/player/test_live_lyric.py b/tests/player/test_lyric.py similarity index 89% rename from tests/player/test_live_lyric.py rename to tests/player/test_lyric.py index 3d3594f57c..610cd698d7 100644 --- a/tests/player/test_live_lyric.py +++ b/tests/player/test_lyric.py @@ -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 @@ -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] == '僕らの最後は死別にしよう'