| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- BallController配置导入测试脚本
- 测试修复后的config_manager.py是否能正确导入BallController标准配置表.xlsx
- """
- import sys
- import os
- from pathlib import Path
- import json
- # 添加当前目录到Python路径
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
- try:
- import pandas as pd
- PANDAS_AVAILABLE = True
- except ImportError:
- PANDAS_AVAILABLE = False
- print("错误: pandas未安装,无法测试Excel导入功能")
- sys.exit(1)
- from config_manager import ConfigManagerGUI
- def test_ballcontroller_import():
- """测试BallController配置导入功能"""
- print("=== BallController配置导入测试 ===")
-
- # 创建配置管理器实例(不启动GUI)
- config_manager = ConfigManagerGUI()
-
- # 设置测试文件路径
- excel_file = Path("BallController标准配置表.xlsx")
-
- if not excel_file.exists():
- print(f"错误: 找不到测试文件 {excel_file}")
- return False
-
- print(f"测试文件: {excel_file}")
-
- # 模拟文件选择
- config_manager.selected_files = [str(excel_file)]
- config_manager._set_config_mapping_for_files()
-
- print(f"配置映射类型: {config_manager.current_mapping['format_type']}")
- print(f"支持的参数数量: {len(config_manager.current_mapping['param_types'])}")
- print(f"参数类型映射: {list(config_manager.current_mapping['param_types'].keys())}")
-
- # 测试Excel文件读取
- try:
- config_data = config_manager.read_excel_config(excel_file)
-
- if config_data:
- print(f"\n成功读取配置,共 {len(config_data)} 个参数:")
- for key, value in config_data.items():
- print(f" {key}: {value} ({type(value).__name__})")
-
- # 验证关键参数(使用Excel文件中的实际参数名)
- expected_params = [
- 'baseSpeed', 'maxReflectionRandomness', 'antiTrapTimeWindow', 'antiTrapHitThreshold',
- 'deflectionAttemptThreshold', 'antiTrapDeflectionMultiplier', 'FIRE_COOLDOWN', 'ballRadius',
- 'gravityScale', 'linearDamping', 'angularDamping', 'colliderGroup',
- 'colliderTag', 'friction', 'restitution', 'safeDistance',
- 'edgeOffset', 'sensor', 'maxAttempts'
- ]
-
- missing_params = [param for param in expected_params if param not in config_data]
- if missing_params:
- print(f"\n警告: 缺少以下参数: {missing_params}")
- else:
- print("\n✓ 所有预期参数都已正确读取")
-
- # 验证数据类型
- type_errors = []
- for param, expected_type in config_manager.current_mapping['param_types'].items():
- if param in config_data:
- actual_type = type(config_data[param])
- if expected_type == bool and actual_type != bool:
- type_errors.append(f"{param}: 期望 {expected_type.__name__}, 实际 {actual_type.__name__}")
- elif expected_type in [int, float] and actual_type not in [int, float]:
- type_errors.append(f"{param}: 期望 {expected_type.__name__}, 实际 {actual_type.__name__}")
-
- if type_errors:
- print(f"\n数据类型错误:")
- for error in type_errors:
- print(f" {error}")
- else:
- print("\n✓ 所有参数数据类型正确")
-
- # 生成JSON输出测试
- json_output = json.dumps(config_data, indent=2, ensure_ascii=False)
- print(f"\nJSON输出预览 (前200字符):")
- print(json_output[:200] + "..." if len(json_output) > 200 else json_output)
-
- return True
- else:
- print("错误: 未能读取到任何配置数据")
- return False
-
- except Exception as e:
- print(f"导入测试失败: {str(e)}")
- import traceback
- traceback.print_exc()
- return False
- if __name__ == "__main__":
- success = test_ballcontroller_import()
- if success:
- print("\n=== 测试通过 ===")
- print("BallController配置导入功能已修复,可以正常使用config_manager.py导入Excel配置")
- else:
- print("\n=== 测试失败 ===")
- print("BallController配置导入仍存在问题,需要进一步调试")
-
- sys.exit(0 if success else 1)
|