""" 工具函数单元测试 测试颜色转换和格式验证函数 """ import pytest from utils import hex_to_rgb class TestHexToRgb: """hex_to_rgb 函数测试类""" def test_full_hex_format(self): """测试完整的 #RRGGBB 格式""" assert hex_to_rgb("#ffffff") == (255, 255, 255) assert hex_to_rgb("#000000") == (0, 0, 0) assert hex_to_rgb("#4a90e2") == (74, 144, 226) assert hex_to_rgb("#FF0000") == (255, 0, 0) assert hex_to_rgb("#00FF00") == (0, 255, 0) assert hex_to_rgb("#0000FF") == (0, 0, 255) def test_short_hex_format(self): """测试短格式 #RGB""" assert hex_to_rgb("#fff") == (255, 255, 255) assert hex_to_rgb("#000") == (0, 0, 0) assert hex_to_rgb("#abc") == (170, 187, 204) assert hex_to_rgb("#ABC") == (170, 187, 204) def test_without_hash_sign(self): """测试没有 # 号""" assert hex_to_rgb("ffffff") == (255, 255, 255) assert hex_to_rgb("4a90e2") == (74, 144, 226) def test_invalid_length(self): """测试无效长度""" with pytest.raises(ValueError, match="无效的颜色格式"): hex_to_rgb("#ff") # 太短 with pytest.raises(ValueError, match="无效的颜色格式"): hex_to_rgb("#fffff") # 5 位 with pytest.raises(ValueError, match="无效的颜色格式"): hex_to_rgb("#fffffff") # 7 位 def test_invalid_characters(self): """测试无效字符""" with pytest.raises(ValueError): hex_to_rgb("#gggggg") with pytest.raises(ValueError): hex_to_rgb("#xyz") def test_mixed_case(self): """测试大小写混合""" assert hex_to_rgb("#AbCdEf") == (171, 205, 239) assert hex_to_rgb("#aBcDeF") == (171, 205, 239)