kards-env/tests/test_interactive_attack.py....

197 lines
6.7 KiB
Plaintext
Raw Permalink Normal View History

2025-09-05 17:05:43 +08:00
#!/usr/bin/env python3
"""
测试交互式攻击命令
包括单位攻击单位和单位攻击HQ的测试
"""
import sys
import os
# 添加项目路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from kards_battle.core.battle_engine import BattleEngine
from kards_battle.units.unit_loader import load_unit
from interactive.command_parser import CommandParser
from interactive.test_scenarios import TestScenarios
def test_attack_unit_to_unit():
"""测试单位攻击单位"""
print("=== 测试单位攻击单位 ===")
# 创建引擎
engine = BattleEngine("Germany", "USA", debug_mode=True)
parser = CommandParser(engine)
scenarios = TestScenarios()
# 加载前线场景
scenarios.load_scenario('frontline', engine)
# 测试攻击支援线上的单位,而不是前线
# 前线已被德军控制,美军无法部署到前线
# 所以我们测试德军前线单位攻击美军支援线单位
print("初始状态:")
_print_battlefield_state(engine)
# 测试攻击命令:德军掷弹兵(F0)攻击美军海军陆战队(S1)
print("\n执行攻击: F0 -> S1 (前线攻击敌方支援线)")
success, message = parser.parse("attack F0 S1")
print(f"攻击结果: {success}")
print(f"消息: {message}")
assert success, f"攻击应该成功,但失败了: {message}"
print("\n攻击后状态:")
_print_battlefield_state(engine)
def test_attack_unit_to_hq():
"""测试单位攻击HQ"""
print("\n=== 测试单位攻击HQ ===")
# 创建引擎
engine = BattleEngine("Germany", "USA", debug_mode=True)
parser = CommandParser(engine)
scenarios = TestScenarios()
# 加载前线场景
scenarios.load_scenario('frontline', engine)
print("初始状态:")
_print_battlefield_state(engine)
print(f"美军HQ初始血量: {engine.battlefield.player2_hq.current_defense}/{engine.battlefield.player2_hq.defense}")
# 测试攻击命令:德军掷弹兵(F0)攻击美军HQ(S0)
print("\n执行攻击: F0 -> S0 (攻击敌方HQ)")
success, message = parser.parse("attack F0 S0")
print(f"攻击结果: {success}")
print(f"消息: {message}")
assert success, f"攻击HQ应该成功但失败了: {message}"
print("\n攻击后状态:")
print(f"美军HQ血量: {engine.battlefield.player2_hq.current_defense}/{engine.battlefield.player2_hq.defense}")
_print_battlefield_state(engine)
def test_attack_support_to_front():
"""测试支援线攻击 - 使用能成功的攻击组合"""
print("\n=== 测试支援线攻击 ===")
# 创建引擎并部署火炮单位
engine = BattleEngine("Germany", "USA", debug_mode=True)
parser = CommandParser(engine)
# 部署德军火炮到支援线
from kards_battle.units.unit_loader import load_unit
artillery = load_unit('ger_artillery_88mm')
engine.deploy_unit_to_support(artillery, 0) # 德军
# 部署美军步兵到支援线作为目标
marine = load_unit('usa_infantry_marine')
engine.deploy_unit_to_support(marine, 1) # 美军
# 确保有足够的Kredits进行攻击
engine.debug_set_kredits(0, kredits=5)
print("初始状态:")
_print_battlefield_state(engine)
# 测试攻击命令:德军火炮(S1)攻击美军海军陆战队(S0)
# 火炮应该能攻击敌方支援线单位
print("\n执行攻击: S1 -> S0 (己方支援线火炮攻击敌方支援线)")
success, message = parser.parse("attack S1 S0")
print(f"攻击结果: {success}")
print(f"消息: {message}")
assert success, f"火炮攻击应该成功,但失败了: {message}"
print("\n攻击后状态:")
_print_battlefield_state(engine)
def test_attack_simplified_vs_full_syntax():
"""测试简化格式 vs 完整格式"""
print("\n=== 测试简化格式 vs 完整格式 ===")
# 创建两个相同的引擎进行对比
engine1 = BattleEngine("Germany", "USA", debug_mode=True)
engine2 = BattleEngine("Germany", "USA", debug_mode=True)
parser1 = CommandParser(engine1)
parser2 = CommandParser(engine2)
scenarios = TestScenarios()
# 加载相同场景
scenarios.load_scenario('frontline', engine1)
scenarios.load_scenario('frontline', engine2)
# 测试简化格式
print("测试简化格式: attack F0 S0")
success1, message1 = parser1.parse("attack F0 S0")
# 测试完整格式
print("测试完整格式: attack FRONT 0 SUPPORT 0")
success2, message2 = parser2.parse("attack FRONT 0 SUPPORT 0")
print(f"简化格式结果: {success1} - {message1}")
print(f"完整格式结果: {success2} - {message2}")
# 验证结果一致
assert success1 == success2, f"两种格式结果不一致: 简化={success1}, 完整={success2}"
print("✅ 两种格式结果一致")
def test_attack_error_cases():
"""测试攻击错误情况"""
print("\n=== 测试攻击错误情况 ===")
engine = BattleEngine("Germany", "USA", debug_mode=True)
parser = CommandParser(engine)
scenarios = TestScenarios()
scenarios.load_scenario('frontline', engine)
test_cases = [
("attack", "参数不足"),
("attack F0", "参数不足"),
("attack F0 F0", "攻击自己"),
("attack F1 S0", "攻击者不存在"),
("attack S0 S1", "HQ不能主动攻击"),
("attack F0 S5", "目标位置不存在"),
("attack X0 S0", "无效位置格式"),
]
all_passed = True
for command, expected_type in test_cases:
print(f"\n测试错误情况: {command} ({expected_type})")
success, message = parser.parse(command)
print(f"结果: {success} - {message}")
assert not success, f"预期失败但成功了: {command} -> {message}"
print("✅ 正确识别错误")
def _print_battlefield_state(engine):
"""打印战场状态"""
print("\n当前战场:")
# 美军支援线
usa_support = engine.battlefield.player2_support.get_all_objectives()
print("美军支援线:", [f"[{i}]{obj.name if hasattr(obj, 'name') else 'HQ'}"
for i, obj in enumerate(usa_support) if obj])
# 前线
front = engine.battlefield.front_line.get_all_objectives()
print("前线:", [f"[{i}]{obj.name}({obj.owner})"
for i, obj in enumerate(front) if obj])
# 德军支援线
ger_support = engine.battlefield.player1_support.get_all_objectives()
print("德军支援线:", [f"[{i}]{obj.name if hasattr(obj, 'name') else 'HQ'}"
for i, obj in enumerate(ger_support) if obj])
if __name__ == "__main__":
# 运行所有测试
import pytest
pytest.main([__file__, "-v"])