You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.9 KiB
77 lines
1.9 KiB
"""
|
|
测试所有设计模式实现
|
|
|
|
此文件用于验证所有创建型设计模式的实现是否正常工作。
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
def run_file(file_name):
|
|
"""运行指定的Python文件
|
|
|
|
Args:
|
|
file_name: 文件名
|
|
|
|
Returns:
|
|
bool: 是否运行成功
|
|
"""
|
|
print(f"\n=== 运行 {file_name} ===")
|
|
try:
|
|
result = subprocess.run(
|
|
[sys.executable, file_name],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True
|
|
)
|
|
print(f"✅ 成功运行 {file_name}")
|
|
# 打印部分输出以验证
|
|
output_lines = result.stdout.split('\n')
|
|
print("输出预览:")
|
|
for i, line in enumerate(output_lines[:5]):
|
|
print(f" {line}")
|
|
if len(output_lines) > 5:
|
|
print(" ...")
|
|
return True
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"❌ 运行 {file_name} 失败")
|
|
print(f"错误输出: {e.stderr}")
|
|
return False
|
|
|
|
def main():
|
|
"""测试所有设计模式实现"""
|
|
print("=== 测试所有创建型设计模式实现 ===")
|
|
|
|
# 要测试的文件列表
|
|
files_to_test = [
|
|
"singleton.py",
|
|
"factory_method.py",
|
|
"abstract_factory.py",
|
|
"builder.py",
|
|
"prototype.py"
|
|
]
|
|
|
|
# 测试每个文件
|
|
success_count = 0
|
|
total_count = len(files_to_test)
|
|
|
|
for file in files_to_test:
|
|
if os.path.exists(file):
|
|
if run_file(file):
|
|
success_count += 1
|
|
else:
|
|
print(f"❌ 文件 {file} 不存在")
|
|
|
|
# 打印测试结果
|
|
print(f"\n=== 测试结果 ===")
|
|
print(f"成功: {success_count}/{total_count}")
|
|
|
|
if success_count == total_count:
|
|
print("✅ 所有设计模式实现测试通过!")
|
|
else:
|
|
print("❌ 有测试失败,请检查问题。")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |