feat: 完善验证器和单元测试 (Task 3.1)
实现 Task 3.1 - C# 编译验证: - CSharpCompilerValidator: C# 编译验证器 - AutoFixEngine: 3 轮自动修复引擎 - ValidationPipeline: 验证流水线服务 - 集成到 ConversionService 功能: - 使用 Roslyn 进行实时编译验证 - 捕获编译错误和警告 - 自动修复第 1 轮:添加缺失的 using 语句 - 自动修复第 2 轮:类型映射修复 - 支持 1-3 轮验证迭代 测试覆盖 (新增 9 个测试): - CSharpCompilerValidatorTests: 3 个测试 - AutoFixEngineTests: 3 个测试 - ValidationPipelineTests: 3 个测试 总测试数:26 个,全部通过 ✅ Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using CodePlay.Core.Interfaces;
|
||||
using CodePlay.Core.Models;
|
||||
using CodePlay.Core.Common;
|
||||
|
||||
namespace CodePlay.Core.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// 验证流水线服务
|
||||
/// </summary>
|
||||
public class ValidationPipeline
|
||||
{
|
||||
private readonly CSharpCompilerValidator _validator;
|
||||
private readonly AutoFixEngine _fixEngine;
|
||||
|
||||
public ValidationPipeline()
|
||||
{
|
||||
_validator = new CSharpCompilerValidator();
|
||||
_fixEngine = new AutoFixEngine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行验证流水线
|
||||
/// </summary>
|
||||
public async Task<ValidationSummary> ValidateAsync(
|
||||
string code,
|
||||
LanguageType language,
|
||||
int maxRounds = 3,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var summary = new ValidationSummary
|
||||
{
|
||||
Passed = false,
|
||||
RoundsExecuted = 0,
|
||||
NeedsManualReview = false,
|
||||
CompilationErrors = new List<CompilationError>(),
|
||||
ValidationLog = new List<string>()
|
||||
};
|
||||
|
||||
summary.ValidationLog.Add($"Starting validation at {DateTime.UtcNow:HH:mm:ss.fff}");
|
||||
|
||||
for (int round = 1; round <= maxRounds; round++)
|
||||
{
|
||||
summary.RoundsExecuted = round;
|
||||
summary.ValidationLog.Add($"Round {round} starting");
|
||||
|
||||
// 编译验证
|
||||
var compileResult = await _validator.ValidateAsync(code, cancellationToken);
|
||||
|
||||
if (compileResult.Success)
|
||||
{
|
||||
summary.Passed = true;
|
||||
summary.ValidationLog.Add($"Round {round}: Compilation succeeded");
|
||||
break;
|
||||
}
|
||||
|
||||
summary.CompilationErrors = compileResult.Errors;
|
||||
summary.ValidationLog.Add($"Round {round}: Compilation failed with {compileResult.Errors.Count} errors");
|
||||
|
||||
// 尝试自动修复
|
||||
var fixResult = await _fixEngine.FixAsync(code, compileResult.Errors, round, cancellationToken);
|
||||
|
||||
if (fixResult.CanFix)
|
||||
{
|
||||
code = fixResult.FixedCode!;
|
||||
summary.ValidationLog.Add($"Round {round}: Auto-fix applied - {fixResult.FixDescription}");
|
||||
}
|
||||
else
|
||||
{
|
||||
summary.NeedsManualReview = true;
|
||||
summary.ValidationLog.Add($"Round {round}: Cannot auto-fix, needs manual review");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!summary.Passed)
|
||||
{
|
||||
summary.ValidationLog.Add($"Validation completed - Needs manual review");
|
||||
}
|
||||
|
||||
return summary;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user