ae4de8a116
核心功能实现: - 实现 CSharpToJavaStrategy 转换策略(包含类型映射) - 实现 CSharpToJavaConverter 转换器 - 实现 JavaCodeGenerator 代码生成器 - 实现 ConversionService 转换服务 - 实现 ConversionController Web API 控制器 - 注册 Swagger 文档和依赖注入 测试覆盖: - CSharpParserTests: 8 个测试用例 - CSharpToJavaConverterTests: 5 个测试用例 - 共 13 个测试全部通过 任务完成: - Task 1.2: 配置项目依赖 - Task 1.3: 建立基础架构 - Task 2.1: 实现 C# 解析器和测试 - Task 2.4: 实现 C# → Java 转换器 - Task 4.1: 创建 ASP.NET Core Web API Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
153 lines
3.3 KiB
C#
153 lines
3.3 KiB
C#
using CodePlay.Core.Parsers;
|
|
using CodePlay.Core.Common;
|
|
using Xunit;
|
|
|
|
namespace CodePlay.Tests.Parsers;
|
|
|
|
public class CSharpParserTests
|
|
{
|
|
private readonly CSharpParser _parser;
|
|
|
|
public CSharpParserTests()
|
|
{
|
|
_parser = new CSharpParser();
|
|
}
|
|
|
|
[Fact]
|
|
public void SupportedLanguage_ShouldReturn_CSharp()
|
|
{
|
|
Assert.Equal(LanguageType.CSharp, _parser.SupportedLanguage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_SimpleClass_ShouldParseSuccessfully()
|
|
{
|
|
var sourceCode = @"
|
|
namespace TestApp
|
|
{
|
|
public class Person
|
|
{
|
|
public string Name { get; set; }
|
|
public int Age { get; set; }
|
|
|
|
public void SayHello()
|
|
{
|
|
Console.WriteLine(""Hello"");
|
|
}
|
|
}
|
|
}";
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(LanguageType.CSharp, result.Language);
|
|
Assert.NotNull(result.Root);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_WithComments_ShouldExtractComments()
|
|
{
|
|
var sourceCode = @"
|
|
// This is a single-line comment
|
|
/* This is a
|
|
multi-line comment */
|
|
public class Test
|
|
{
|
|
/// <summary>
|
|
/// This is XML documentation
|
|
/// </summary>
|
|
public void Method() { }
|
|
}";
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.NotEmpty(result.Comments);
|
|
Assert.NotEmpty(result.Documentation);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_WithGenerics_ShouldParseGenerics()
|
|
{
|
|
var sourceCode = @"
|
|
public class GenericClass<T> where T : class
|
|
{
|
|
public List<T> Items { get; set; }
|
|
|
|
public T GetItem(int index)
|
|
{
|
|
return Items[index];
|
|
}
|
|
}";
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(LanguageType.CSharp, result.Language);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_WithLambda_ShouldParseLambda()
|
|
{
|
|
var sourceCode = @"
|
|
public class LambdaTest
|
|
{
|
|
public void TestMethod()
|
|
{
|
|
var numbers = new List<int> { 1, 2, 3 };
|
|
var even = numbers.Where(n => n % 2 == 0);
|
|
}
|
|
}";
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_WithAsyncMethod_ShouldParseAsync()
|
|
{
|
|
var sourceCode = @"
|
|
public class AsyncTest
|
|
{
|
|
public async Task<string> GetDataAsync()
|
|
{
|
|
await Task.Delay(100);
|
|
return ""data"";
|
|
}
|
|
}";
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_EmptyCode_ShouldParseSuccessfully()
|
|
{
|
|
var sourceCode = string.Empty;
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(LanguageType.CSharp, result.Language);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ParseAsync_InvalidSyntax_ShouldHandleGracefully()
|
|
{
|
|
var sourceCode = @"
|
|
public class InvalidClass
|
|
{
|
|
public void InvalidMethod(
|
|
{
|
|
// Missing closing parenthesis
|
|
}
|
|
}";
|
|
|
|
var result = await _parser.ParseAsync(sourceCode);
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
}
|