"""douyin/utils/time_utils 的 pytest 单元测试。 覆盖 parse_mm_ss(mm:ss 解析)和 extract_2digits(数字提取)两个纯函数。 """ from __future__ import annotations import pytest from douyin.utils.time_utils import extract_2digits, parse_mm_ss # ============================================================ # parse_mm_ss # ============================================================ class TestParseMmSs: @pytest.mark.parametrize( "text,expected", [ ("11:37", (11, 37)), ("00:00", (0, 0)), ("5:30", (5, 30)), ("12:59", (12, 59)), # 中文冒号 ("11:37".replace(":", ":"), (11, 37)), ("5:30后开奖".replace(":", ":"), (5, 30)), # 嵌在文本里 ("剩余 03:25 开奖", (3, 25)), ], ) def test_valid(self, text, expected): assert parse_mm_ss(text) == expected @pytest.mark.parametrize( "text", [ "", None, "abc", "12分钟", # 没冒号 "12", "::", ], ) def test_invalid(self, text): assert parse_mm_ss(text) is None # ============================================================ # extract_2digits # ============================================================ class TestExtract2Digits: @pytest.mark.parametrize( "text,expected", [ ("12", "12"), ("123", "12"), # 取前 2 位 ("9", "09"), # 单位补 0 ("a3b", "03"), # 过滤非数字 ("10秒", "10"), ("剩余3", "03"), ("", ""), # 无数字 → 空 ("abc", ""), (None, ""), ], ) def test_extract(self, text, expected): assert extract_2digits(text) == expected