1
This commit is contained in:
2024-11-30 22:07:38 +08:00
parent 4142bd9423
commit ee8bf46701
8 changed files with 219 additions and 29 deletions

50
tests/test_hand.py Normal file
View File

@@ -0,0 +1,50 @@
from src.engine.hand import Hand
def test_hand():
# 创建一个玩家的手牌
hand = Hand()
# 添加一些牌到手牌中
hand.add_tile("1条")
hand.add_tile("1条")
hand.add_tile("2条")
hand.add_tile("2条")
hand.add_tile("2条")
hand.add_tile("3条")
# 打印手牌
print("\n当前手牌:", hand)
# 测试获取某张牌的数量
assert hand.get_tile_count("1条") == 2, f"测试失败1条应该有 2 张"
assert hand.get_tile_count("2条") == 3, f"测试失败2条应该有 3 张"
assert hand.get_tile_count("3条") == 1, f"测试失败3条应该有 1 张"
# 测试移除一张牌
hand.remove_tile("1条")
print("移除 1条 后的手牌:", hand)
assert hand.get_tile_count("1条") == 1, f"测试失败1条应该有 1 张"
# 确保移除后有足够的牌可以碰
# 添加一张 1条确保可以碰
hand.add_tile("1条")
print("添加 1条 后的手牌:", hand)
# 测试是否可以碰
assert hand.can_pong("1条") == True, f"测试失败1条应该可以碰"
assert hand.can_pong("3条") == False, f"测试失败3条不可以碰"
# 测试是否可以杠
assert hand.can_gang("1条") == False, f"测试失败1条不可以杠"
assert hand.can_gang("2条") == False, f"测试失败2条不可以杠"
# 添加更多牌来形成杠
hand.add_tile("2条")
hand.add_tile("2条")
print("添加牌后手牌:", hand)
assert hand.can_gang("2条") == True, f"测试失败2条应该可以杠"
print("所有测试通过!")

View File

@@ -0,0 +1,45 @@
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("所有测试通过!")

36
tests/test_utils.py Normal file
View File

@@ -0,0 +1,36 @@
from src.engine.utils import get_suit,get_tile_name
def test_get_suit():
# 测试条花色0-35
for i in range(36):
assert get_suit(i) == "", f"测试失败:索引 {i} 应该是 ''"
# 测试筒花色36-71
for i in range(36, 72):
assert get_suit(i) == "", f"测试失败:索引 {i} 应该是 ''"
# 测试万花色72-107
for i in range(72, 108):
assert get_suit(i) == "", f"测试失败:索引 {i} 应该是 ''"
# 测试无效索引
try:
get_suit(108)
assert False, "测试失败:索引 108 应该抛出 ValueError"
except ValueError:
pass # 如果抛出 ValueError测试通过
print("get_suit 测试通过!")
def test_get_tile_name():
# 测试每个牌的名称是否正确
for i in range(108):
tile_name = get_tile_name(i)
assert tile_name == f"{i % 36 + 1}{get_suit(i)}", \
f"测试失败:索引 {i} 应该是 '{i % 36 + 1}{get_suit(i)}',但实际返回 '{tile_name}'"
print("get_tile_name 测试通过!")
# 运行测试
test_get_suit()
test_get_tile_name()