kards-env/tests/unit/test_position_deployment.py

165 lines
5.4 KiB
Python
Raw Permalink Normal View History

2025-09-05 17:05:43 +08:00
#!/usr/bin/env python3
"""
测试支援线指定位置部署功能
"""
import sys
import os
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from kards_battle.core.battle_engine import BattleEngine
from kards_battle.examples.sample_units import create_german_infantry, create_american_gi, create_german_panzer_iv
def test_position_deployment():
"""测试指定位置部署"""
print("\n🎯 支援线指定位置部署测试")
print("-" * 40)
engine = BattleEngine("Germany", "USA", debug_mode=True)
# 创建多个单位
unit1 = create_german_infantry()
unit1.name = "Infantry 1"
unit2 = create_german_panzer_iv()
unit2.name = "Panzer 2"
unit3 = create_german_infantry()
unit3.name = "Infantry 3"
print("初始战场:")
print(engine.battlefield)
# 测试1: 默认位置部署(末尾)
result1 = engine.deploy_unit_to_support(unit1, 0)
print(f"\n部署Infantry 1到默认位置: {result1['success']}")
if result1['success']:
print(f" 实际位置: {result1['position']}")
print("部署后战场:")
print(engine.battlefield)
# 测试2: 指定位置1部署
result2 = engine.deploy_unit_to_support(unit2, 0, position=1)
print(f"\n部署Panzer 2到位置1: {result2['success']}")
if result2['success']:
print(f" 实际位置: {result2['position']}")
print("部署后战场:")
print(engine.battlefield)
# 测试3: 指定位置0部署最前面
result3 = engine.deploy_unit_to_support(unit3, 0, position=0)
print(f"\n部署Infantry 3到位置0: {result3['success']}")
if result3['success']:
print(f" 实际位置: {result3['position']}")
print("最终战场:")
print(engine.battlefield)
# 验证单位顺序是否正确
support_line = engine.battlefield.get_player_support_line(engine.player_names[0])
objectives = support_line.get_all_objectives()
print(f"\n单位顺序验证:")
for i, obj in enumerate(objectives):
if hasattr(obj, 'name'):
print(f" 位置{i}: {obj.name}")
else:
print(f" 位置{i}: HQ")
# 期望顺序: Infantry 3(位置0), HQ(被挤到位置1), Panzer 2(位置2), Infantry 1(被挤到位置3)
expected_order = ["Infantry 3", "HQ", "Panzer 2", "Infantry 1"]
actual_order = []
for obj in objectives:
if hasattr(obj, 'name'):
actual_order.append(obj.name)
else:
actual_order.append("HQ")
order_correct = actual_order == expected_order
print(f"✅ 单位顺序正确: {'' if order_correct else ''}")
print(f"期望: {expected_order}")
print(f"实际: {actual_order}")
assert all([result1['success'], result2['success'], result3['success'], order_correct]), "Position deployment should work correctly"
def test_edge_cases():
"""测试边界情况"""
print("\n🔍 边界情况测试")
print("-" * 40)
engine = BattleEngine("Germany", "USA", debug_mode=True)
unit1 = create_german_infantry()
unit1.name = "Test Unit"
# 测试1: 负数位置
result1 = engine.deploy_unit_to_support(unit1, 0, position=-1)
print(f"部署到负数位置: {'失败' if not result1['success'] else '成功'}")
print(f" 错误信息: {result1.get('reason', 'None')}")
# 测试2: 超大位置(应该部署到末尾)
result2 = engine.deploy_unit_to_support(unit1, 0, position=999)
print(f"部署到超大位置: {result2['success']}")
if result2['success']:
print(f" 实际位置: {result2['position']}")
# 测试3: 支援线满了的情况
# 先填满支援线
for i in range(4): # 支援线最多5个目标已有HQ再加4个单位就满了
unit = create_german_infantry()
unit.name = f"Filler {i}"
engine.deploy_unit_to_support(unit, 0)
print(f"\n支援线满后状态:")
print(engine.battlefield)
# 尝试再部署一个单位
extra_unit = create_german_infantry()
extra_unit.name = "Extra Unit"
result3 = engine.deploy_unit_to_support(extra_unit, 0, position=2)
print(f"支援线满时部署: {'失败' if not result3['success'] else '成功'}")
print(f" 错误信息: {result3.get('reason', 'None')}")
assert not result1['success'], "Negative position deployment should fail"
assert result2['success'], "Large position deployment should succeed"
assert not result3['success'], "Full support line deployment should fail"
def run_all_tests():
"""运行所有测试"""
print("🧪 支援线位置部署测试套件")
print("=" * 50)
tests = [
("指定位置部署", test_position_deployment),
("边界情况", test_edge_cases),
]
results = {}
for name, test_func in tests:
try:
results[name] = test_func()
except Exception as e:
print(f"{name} 测试异常: {e}")
results[name] = False
print("\n📊 测试结果汇总")
print("=" * 50)
all_passed = True
for name, passed in results.items():
status = "✅ 通过" if passed else "❌ 失败"
print(f"{name}: {status}")
if not passed:
all_passed = False
print(f"\n总体结果: {'🎉 全部通过!' if all_passed else '❌ 有测试失败'}")
return all_passed
if __name__ == "__main__":
run_all_tests()