46 lines
1.9 KiB
Python
46 lines
1.9 KiB
Python
from src 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("所有测试通过!")
|
||
|
||
|