1
1
This commit is contained in:
50
tests/test_hand.py
Normal file
50
tests/test_hand.py
Normal 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("所有测试通过!")
|
||||
|
||||
|
||||
45
tests/test_mahjong_tile.py
Normal file
45
tests/test_mahjong_tile.py
Normal 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
36
tests/test_utils.py
Normal 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()
|
||||
Reference in New Issue
Block a user