创建新工程
This commit is contained in:
169
tests/test_property_keyboard_input.gd
Normal file
169
tests/test_property_keyboard_input.gd
Normal file
@@ -0,0 +1,169 @@
|
||||
extends Node
|
||||
## 键盘输入响应属性测试
|
||||
## Feature: godot-ai-town-game, Property 11: 键盘输入响应
|
||||
|
||||
const TEST_ITERATIONS = 100
|
||||
|
||||
# 测试结果统计
|
||||
var test_results = {
|
||||
"passed": 0,
|
||||
"failed": 0,
|
||||
"errors": []
|
||||
}
|
||||
|
||||
func _ready():
|
||||
print("\n=== Property Test: Keyboard Input Response ===")
|
||||
await test_property_keyboard_input_response()
|
||||
print_results()
|
||||
print("=== Property Test Completed ===\n")
|
||||
|
||||
## Feature: godot-ai-town-game, Property 11: 键盘输入响应
|
||||
func test_property_keyboard_input_response():
|
||||
"""
|
||||
属性测试:键盘输入响应
|
||||
对于任意有效的键盘移动输入(方向键或 WASD),
|
||||
系统应该将玩家角色移动到相应方向,并将移动数据同步到服务器
|
||||
|
||||
验证需求: 5.1
|
||||
"""
|
||||
print("\n[Property Test] Testing keyboard input response...")
|
||||
print("Running ", TEST_ITERATIONS, " iterations...")
|
||||
|
||||
# 定义所有可能的移动动作
|
||||
var move_actions = ["move_up", "move_down", "move_left", "move_right"]
|
||||
var expected_directions = {
|
||||
"move_up": Vector2(0, -1),
|
||||
"move_down": Vector2(0, 1),
|
||||
"move_left": Vector2(-1, 0),
|
||||
"move_right": Vector2(1, 0)
|
||||
}
|
||||
|
||||
for i in range(TEST_ITERATIONS):
|
||||
# 随机选择一个移动动作
|
||||
var action = move_actions[randi() % move_actions.size()]
|
||||
var expected_dir = expected_directions[action]
|
||||
|
||||
# 创建输入处理器
|
||||
var input_handler = InputHandler.new()
|
||||
add_child(input_handler)
|
||||
|
||||
# 等待一帧以确保初始化完成
|
||||
await get_tree().process_frame
|
||||
|
||||
# 模拟按键输入
|
||||
var input_event = InputEventAction.new()
|
||||
input_event.action = action
|
||||
input_event.pressed = true
|
||||
Input.parse_input_event(input_event)
|
||||
|
||||
# 等待输入处理
|
||||
await get_tree().process_frame
|
||||
|
||||
# 获取移动方向
|
||||
var actual_dir = input_handler.get_move_direction()
|
||||
|
||||
# 验证方向是否正确
|
||||
var direction_correct = actual_dir.normalized().distance_to(expected_dir.normalized()) < 0.01
|
||||
|
||||
if direction_correct:
|
||||
test_results["passed"] += 1
|
||||
else:
|
||||
test_results["failed"] += 1
|
||||
test_results["errors"].append({
|
||||
"iteration": i + 1,
|
||||
"action": action,
|
||||
"expected": expected_dir,
|
||||
"actual": actual_dir
|
||||
})
|
||||
|
||||
# 释放按键
|
||||
input_event.pressed = false
|
||||
Input.parse_input_event(input_event)
|
||||
|
||||
# 清理
|
||||
input_handler.queue_free()
|
||||
await get_tree().process_frame
|
||||
|
||||
# 测试组合输入(对角线移动)
|
||||
await test_diagonal_movement()
|
||||
|
||||
func test_diagonal_movement():
|
||||
"""测试对角线移动(组合输入)"""
|
||||
print("\n[Property Test] Testing diagonal movement...")
|
||||
|
||||
var diagonal_tests = [
|
||||
{
|
||||
"actions": ["move_up", "move_right"],
|
||||
"expected": Vector2(1, -1).normalized()
|
||||
},
|
||||
{
|
||||
"actions": ["move_up", "move_left"],
|
||||
"expected": Vector2(-1, -1).normalized()
|
||||
},
|
||||
{
|
||||
"actions": ["move_down", "move_right"],
|
||||
"expected": Vector2(1, 1).normalized()
|
||||
},
|
||||
{
|
||||
"actions": ["move_down", "move_left"],
|
||||
"expected": Vector2(-1, 1).normalized()
|
||||
}
|
||||
]
|
||||
|
||||
for test_case in diagonal_tests:
|
||||
var input_handler = InputHandler.new()
|
||||
add_child(input_handler)
|
||||
await get_tree().process_frame
|
||||
|
||||
# 模拟组合按键
|
||||
for action in test_case["actions"]:
|
||||
var input_event = InputEventAction.new()
|
||||
input_event.action = action
|
||||
input_event.pressed = true
|
||||
Input.parse_input_event(input_event)
|
||||
|
||||
await get_tree().process_frame
|
||||
|
||||
# 获取移动方向
|
||||
var actual_dir = input_handler.get_move_direction()
|
||||
|
||||
# 验证方向
|
||||
var direction_correct = actual_dir.distance_to(test_case["expected"]) < 0.01
|
||||
|
||||
if direction_correct:
|
||||
test_results["passed"] += 1
|
||||
else:
|
||||
test_results["failed"] += 1
|
||||
test_results["errors"].append({
|
||||
"type": "diagonal",
|
||||
"actions": test_case["actions"],
|
||||
"expected": test_case["expected"],
|
||||
"actual": actual_dir
|
||||
})
|
||||
|
||||
# 释放所有按键
|
||||
for action in test_case["actions"]:
|
||||
var input_event = InputEventAction.new()
|
||||
input_event.action = action
|
||||
input_event.pressed = false
|
||||
Input.parse_input_event(input_event)
|
||||
|
||||
input_handler.queue_free()
|
||||
await get_tree().process_frame
|
||||
|
||||
func print_results():
|
||||
"""打印测试结果"""
|
||||
print("\n=== Test Results ===")
|
||||
print("Total iterations: ", TEST_ITERATIONS + 4) # +4 for diagonal tests
|
||||
print("Passed: ", test_results["passed"])
|
||||
print("Failed: ", test_results["failed"])
|
||||
|
||||
if test_results["failed"] > 0:
|
||||
print("\n❌ FAILED: Some iterations failed")
|
||||
print("First 5 errors:")
|
||||
for i in range(min(5, test_results["errors"].size())):
|
||||
var error = test_results["errors"][i]
|
||||
print(" Error ", i + 1, ": ", error)
|
||||
else:
|
||||
print("\n✅ PASSED: All keyboard input responses work correctly")
|
||||
print("Property 11 validated: Keyboard inputs correctly translate to character movement")
|
||||
Reference in New Issue
Block a user