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:
@@ -3,6 +3,7 @@ using CodePlay.Core.Models;
|
||||
using CodePlay.Core.Common;
|
||||
using CodePlay.Core.Parsers;
|
||||
using CodePlay.Core.Converters;
|
||||
using CodePlay.Core.Validators;
|
||||
|
||||
namespace CodePlay.Core.Services;
|
||||
|
||||
@@ -12,12 +13,16 @@ namespace CodePlay.Core.Services;
|
||||
public class ConversionService
|
||||
{
|
||||
private readonly Dictionary<(LanguageType, LanguageType), IConverter> _converters = new();
|
||||
private readonly ValidationPipeline _validationPipeline;
|
||||
|
||||
public ConversionService()
|
||||
{
|
||||
// 注册转换器
|
||||
RegisterConverter(LanguageType.CSharp, LanguageType.Java, new CSharpToJavaConverter());
|
||||
RegisterConverter(LanguageType.Java, LanguageType.CSharp, new JavaToCSharpConverter());
|
||||
|
||||
// 初始化验证流水线
|
||||
_validationPipeline = new ValidationPipeline();
|
||||
}
|
||||
|
||||
private void RegisterConverter(LanguageType source, LanguageType target, IConverter converter)
|
||||
@@ -59,6 +64,24 @@ public class ConversionService
|
||||
// 执行转换
|
||||
var result = await converter.ConvertAsync(syntaxTree, request.TargetLanguage, request.Options, cancellationToken);
|
||||
|
||||
// 执行验证(仅当目标语言是 C# 时)
|
||||
if (result.Success && request.TargetLanguage == LanguageType.CSharp && request.ValidationRounds > 0)
|
||||
{
|
||||
var validationSummary = await _validationPipeline.ValidateAsync(
|
||||
result.TransformedCode,
|
||||
request.TargetLanguage,
|
||||
request.ValidationRounds,
|
||||
cancellationToken);
|
||||
|
||||
result.ValidationSummary = validationSummary;
|
||||
|
||||
if (!validationSummary.Passed)
|
||||
{
|
||||
result.Success = false;
|
||||
result.ErrorMessage = $"Validation failed after {validationSummary.RoundsExecuted} rounds";
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user