mjAi/tests/test_mahjong_tile.py

46 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from src.engine.mahjong_tile import MahjongTile
def test_mahjong_tile():
# 测试合法的牌
tile1 = MahjongTile("", 5)
assert tile1.suit == "", f"测试失败:预期花色是 '',但实际是 {tile1.suit}"
assert tile1.value == 5, f"测试失败:预期面值是 5但实际是 {tile1.value}"
assert repr(tile1) == "5条", f"测试失败:预期牌名是 '5条',但实际是 {repr(tile1)}"
tile2 = MahjongTile("", 3)
assert tile2.suit == "", f"测试失败:预期花色是 '',但实际是 {tile2.suit}"
assert tile2.value == 3, f"测试失败:预期面值是 3但实际是 {tile2.value}"
assert repr(tile2) == "3筒", f"测试失败:预期牌名是 '3筒',但实际是 {repr(tile2)}"
tile3 = MahjongTile("", 9)
assert tile3.suit == "", f"测试失败:预期花色是 '',但实际是 {tile3.suit}"
assert tile3.value == 9, f"测试失败:预期面值是 9但实际是 {tile3.value}"
assert repr(tile3) == "9万", f"测试失败:预期牌名是 '9万',但实际是 {repr(tile3)}"
# 测试非法的牌
try:
MahjongTile("", 10) # 面值超出范围
assert False, "测试失败:面值为 10 的牌应该抛出异常"
except ValueError:
pass # 正确抛出异常
try:
MahjongTile("", 5) # 花色无效
assert False, "测试失败:花色为 '' 的牌应该抛出异常"
except ValueError:
pass # 正确抛出异常
# 测试相等判断
tile4 = MahjongTile("", 5)
assert tile1 == tile4, f"测试失败:预期 {tile1}{tile4} 相等"
tile5 = MahjongTile("", 5)
assert tile1 != tile5, f"测试失败:预期 {tile1}{tile5} 不相等"
# 测试哈希
tile_set = {tile1, tile4, tile2}
assert len(tile_set) == 2, f"测试失败:集合中应该有 2 张牌,而实际有 {len(tile_set)}"
print("所有测试通过!")