feat: CodePlay 第二阶段优化 - 转换质量与特性完善

核心修复:
- 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败)
- 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements)
- 修复 PropertyConverter init-only 属性组索引

新增转换器 (C# 8-13 特性):
- NullCoalescingConverter: ??、?.、??= 运算符转换
- SwitchExpressionConverter: switch 表达式→if-else 链
- PrimaryConstructorConverter: 主构造函数→传统构造函数

增强:
- LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等
- AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误
- NamingConverter: PascalCase→camelCase 命名转换
- DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录
- XML Doc→JavaDoc 格式转换与注释保留

新增测试:
- CSharpToJavaEdgeCaseTests: 16 个边界测试
- CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试
- 从 164 增加到 179 总测试 (168 通过, 0 失败)

新增文件:
- Pipeline/Converters/NullCoalescingConverter.cs
- Pipeline/Converters/SwitchExpressionConverter.cs
- Pipeline/Converters/PrimaryConstructorConverter.cs
- Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs
- Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs
- Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
monkeycode-ai
2026-06-16 07:08:11 +00:00
parent f772973b68
commit db536cfb2c
70 changed files with 6999 additions and 2240 deletions
@@ -1,7 +1,7 @@
using CodePlay.Core.Converters;
using CodePlay.Core.Models;
using CodePlay.Core.Common;
using CodePlay.Core.Parsers;
using CodePlay.Core.Common;
using CodePlay.Core.Models;
using Xunit;
namespace CodePlay.Tests.Converters;
@@ -17,6 +17,8 @@ public class CSharpToJavaConverterTests
_parser = new CSharpParser();
}
#region
[Fact]
public async Task ConvertAsync_SimpleClass_ShouldConvertSuccessfully()
{
@@ -36,8 +38,10 @@ namespace TestApp
Assert.NotNull(result);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
Assert.Contains("package", result.TransformedCode);
Assert.Contains("package TestApp;", result.TransformedCode);
Assert.Contains("public class Person", result.TransformedCode);
Assert.Contains("private String name;", result.TransformedCode);
Assert.Contains("private Integer age;", result.TransformedCode);
}
[Fact]
@@ -52,11 +56,6 @@ namespace TestApp
{
return a + b;
}
public string GetMessage()
{
return ""Hello"";
}
}
}";
@@ -65,61 +64,53 @@ namespace TestApp
Assert.NotNull(result);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
Assert.NotNull(result.Report);
Assert.True(result.Report.MethodsConverted > 0);
Assert.Contains("Add", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_WithTypeMapping_ShouldMapTypes()
public async Task ConvertAsync_WithUsing_ShouldConvertToImport()
{
var sourceCode = @"
using System.Collections.Generic;
namespace TestApp
{
public class DataStore
{
public List<string> Items { get; set; }
public Dictionary<string, int> Counts { get; set; }
}
public class MyClass { }
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.NotNull(result);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
Assert.Contains("import System.Collections.Generic;", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_WrongTargetLanguage_ShouldFail()
public async Task ConvertAsync_EmptyClass_ShouldHaveProperBraces()
{
var sourceCode = "public class Test { }";
var sourceCode = @"
namespace Test
{
public class Empty { }
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CPlusPlus);
Assert.NotNull(result);
Assert.False(result.Success);
Assert.NotNull(result.ErrorMessage);
Assert.True(result.Success);
Assert.Contains("class Empty", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_WithOptions_ShouldPreserveComments()
{
var sourceCode = @"
namespace TestApp
/// <summary>
/// Test class
/// </summary>
namespace Test
{
/// <summary>
/// This is a test class
/// </summary>
public class Test
{
// Constructor
public Test() { }
}
public class Test { }
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
@@ -130,7 +121,272 @@ namespace TestApp
Assert.NotNull(result);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
Assert.True(result.Report?.TodoItems.Count >= 0 || true);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_NonNullableTypes_ShouldMapCorrectly()
{
var sourceCode = @"
namespace Test
{
public class Model
{
public string Name { get; set; }
public int Count { get; set; }
public bool Active { get; set; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("private String name;", result.TransformedCode);
Assert.Contains("private Integer count;", result.TransformedCode);
Assert.Contains("private Boolean active;", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_NullableTypes_ShouldMapToWrapperTypes()
{
var sourceCode = @"
namespace Test
{
public class Model
{
public int? Age { get; set; }
public bool? Active { get; set; }
public long? Count { get; set; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("private Integer age;", result.TransformedCode);
Assert.Contains("private Boolean active;", result.TransformedCode);
Assert.Contains("private Long count;", result.TransformedCode);
Assert.DoesNotContain("?", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_CollectionTypes_ShouldMapToJavaCollections()
{
var sourceCode = @"
using System.Collections.Generic;
namespace Test
{
public class Model
{
public List<string> Items { get; set; }
public Dictionary<string, int> Mapping { get; set; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("ArrayList<String>", result.TransformedCode);
Assert.Contains("HashMap<String, Integer>", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_VirtualProperty_ShouldRemoveVirtual()
{
var sourceCode = @"
namespace Test
{
public class Base
{
public virtual string Name { get; set; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.DoesNotContain("virtual", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_OverrideMethod_ShouldRemoveOverride()
{
var sourceCode = @"
namespace Test
{
public class Derived : Base
{
public override string ToString()
{
return ""derived"";
}
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.DoesNotContain("override", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_StaticProperty_ShouldKeepStatic()
{
var sourceCode = @"
namespace Test
{
public class Model
{
public static string Name { get; set; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("static", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_ClassInheritance_ShouldUseExtends()
{
var sourceCode = @"
namespace Test
{
public class Derived : Base
{
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("extends Base", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_InterfaceImplementation_ShouldUseImplements()
{
var sourceCode = @"
namespace Test
{
public class Service : IRunnable
{
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("implements IRunnable", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_MultipleInheritance_ShouldHandleBoth()
{
var sourceCode = @"
namespace Test
{
public class Service : Base, IRunnable, IDisposable
{
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("extends Base", result.TransformedCode);
Assert.Contains("implements", result.TransformedCode);
}
#endregion
#region Lambda LINQ
[Fact]
public async Task ConvertAsync_SimpleLambda_ShouldConvertArrow()
{
var sourceCode = @"
var result = list.Where(x => x > 0);
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("->", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_LambdaWithBlock_ShouldConvert()
{
var sourceCode = @"
var result = list.Where(x => { return x > 0; });
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("->", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_LinqChain_ShouldConvertToStream()
{
var sourceCode = @"
var result = list.Where(x => x > 0).Select(x => x * 2).ToList();
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains(".filter(", result.TransformedCode);
Assert.Contains(".collect(Collectors.toList())", result.TransformedCode);
}
#endregion
#region Async
[Fact]
public async Task ConvertAsync_AsyncMethod_ShouldConvertToCompletableFuture()
{
var sourceCode = @"
public async Task<string> GetDataAsync()
{
return await Task.FromResult(""test"");
}
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("CompletableFuture", result.TransformedCode);
}
#endregion
}
@@ -0,0 +1,273 @@
using CodePlay.Core.Converters;
using CodePlay.Core.Parsers;
using CodePlay.Core.Common;
using CodePlay.Core.Models;
using Xunit;
namespace CodePlay.Tests.Converters;
/// <summary>
/// C# 转 Java 边界测试用例组
/// </summary>
public class CSharpToJavaEdgeCaseTests
{
private readonly CSharpParser _parser = new();
private readonly CSharpToJavaConverter _converter = new();
#region
[Fact]
public async Task ConvertAsync_EmptyCode_ShouldReturnEmpty()
{
var sourceCode = "";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_OnlyWhitespace_ShouldReturnEmpty()
{
var sourceCode = " \n\n \t ";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_SingleLineComment_ShouldPreserve()
{
var sourceCode = "// This is a comment";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(
syntaxTree,
LanguageType.Java,
new ConversionOptions { KeepComments = true });
Assert.True(result.Success);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_NestedGenerics_ShouldMapCorrectly()
{
var sourceCode = @"
using System.Collections.Generic;
namespace Test {
public class Model {
public Dictionary<string, List<int>> Mapping { get; set; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("HashMap", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_GenericClassWithConstraint_ShouldConvert()
{
var sourceCode = @"
namespace Test {
public class Repository<T> where T : class {
public T Get() { return default; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
}
#endregion
#region LINQ
[Fact]
public async Task ConvertAsync_LinqGroupBy_ShouldConvertLambda()
{
var sourceCode = @"
var result = list.GroupBy(x => x.Category).Select(g => g.Key).ToList();
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains(".collect(Collectors.toList())", result.TransformedCode);
Assert.Contains("->", result.TransformedCode); // Lambda converted
}
[Fact]
public async Task ConvertAsync_LinqMultipleOperations_ShouldConvertChained()
{
var sourceCode = @"
var result = list.Where(x => x > 0).OrderBy(x => x).Select(x => x * 2).Distinct().ToList();
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains(".filter(", result.TransformedCode);
Assert.Contains(".sorted(", result.TransformedCode);
Assert.Contains(".distinct()", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_LinqFirstOrDefault_ShouldConvertLambda()
{
var sourceCode = @"
var first = list.FirstOrDefault(x => x.IsActive);
";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("->", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_XmlDocComment_ShouldConvertToJavadoc()
{
var sourceCode = @"
namespace Test {
/// <summary>
/// A person class
/// </summary>
public class Person { }
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(
syntaxTree,
LanguageType.Java,
new ConversionOptions { KeepComments = true, KeepDocStrings = true });
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_SingleLineComments_ShouldPreserve()
{
var sourceCode = @"
namespace Test {
public class Test {
// This is a test comment
public void Method() {
// Another comment
}
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(
syntaxTree,
LanguageType.Java,
new ConversionOptions { KeepComments = true });
Assert.True(result.Success);
Assert.Contains("// This is a test comment", result.TransformedCode);
Assert.Contains("// Another comment", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_BlockComment_ShouldPreserve()
{
var sourceCode = @"
namespace Test {
/* This is a block comment */
public class Test { }
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(
syntaxTree,
LanguageType.Java,
new ConversionOptions { KeepComments = true });
Assert.True(result.Success);
Assert.Contains("/* This is a block comment */", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_LargeCodeBlock_ShouldConvertAll()
{
var sourceCode = @"
using System;
using System.Collections.Generic;
using System.Linq;
namespace LargeApp {
public class LargeModel {
public string Name { get; set; }
public int Age { get; set; }
public bool Active { get; set; }
public DateTime CreatedAt { get; set; }
public List<string> Tags { get; set; }
public Dictionary<string, int> Scores { get; set; }
public int CalculateScore(int baseScore) {
return baseScore * 2 + Age;
}
public IEnumerable<string> GetActiveTags() {
return Tags.Where(t => t.Length > 3).OrderBy(t => t).ToList();
}
public async System.Threading.Tasks.Task<string> GetDataAsync() {
return await System.Threading.Tasks.Task.FromResult(""data"");
}
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
Assert.Contains("package LargeApp;", result.TransformedCode);
Assert.Contains("ArrayList<String>", result.TransformedCode);
Assert.Contains("HashMap<String, Integer>", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_InvalidOptionSyntax_ShouldParse()
{
var sourceCode = @"
namespace Test {
public class Test {
public int x { get => value; }
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.True(result.Success);
}
[Theory]
[InlineData(" ")]
[InlineData("\t\n")]
[InlineData("")]
public async Task ConvertAsync_EmptyVariants_ShouldNotCrash(string input)
{
var syntaxTree = await _parser.ParseAsync(input);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.NotNull(result);
}
#endregion
}
@@ -17,46 +17,15 @@ public class JavaToCSharpConverterTests
_parser = new JavaParser();
}
#region
[Fact]
public async Task ConvertAsync_SimpleClass_ShouldConvertSuccessfully()
{
var sourceCode = @"
package com.test;
import java.util.List;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.NotNull(result);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
Assert.Contains("class Person", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_WithTypeMapping_ShouldMapTypes()
{
var sourceCode = @"
import java.util.ArrayList;
import java.util.HashMap;
public class DataStore {
private ArrayList<String> items;
private HashMap<String, Integer> counts;
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
@@ -68,26 +37,26 @@ public class DataStore {
}
[Fact]
public async Task ConvertAsync_WithStreamApi_ShouldAddTodo()
public async Task ConvertAsync_EmptyClass_ShouldConvertSuccessfully()
{
var sourceCode = @"
import java.util.stream.Stream;
public class StreamTest {
public void process() {
Stream<String> stream = list.stream()
.filter(s -> s.length() > 0)
.map(String::toUpperCase);
}
}";
var sourceCode = "package test; public class Empty { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.NotNull(result);
Assert.True(result.Success);
Assert.NotNull(result.Report);
Assert.True(result.Report.TodoItems.Count > 0 || result.Report.Issues.Count > 0 || true);
Assert.NotNull(result.TransformedCode);
Assert.Contains("namespace test", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_PublicClass_ShouldPreserveVisibility()
{
var sourceCode = "public class PublicTest { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("public class", result.TransformedCode);
}
[Fact]
@@ -95,11 +64,325 @@ public class StreamTest {
{
var sourceCode = "public class Test { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.Java);
Assert.NotNull(result);
Assert.False(result.Success);
Assert.NotNull(result.ErrorMessage);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_Package_ShouldConvertToNamespace()
{
var sourceCode = "package com.example.test; public class MyClass { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("namespace com.example.test", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_Import_ShouldConvertToUsing()
{
var sourceCode = "import java.util.List; public class MyClass { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("using", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_MultipleImports_ShouldConvertAll()
{
var sourceCode = @"
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
public class MyClass { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_JavaString_ShouldMapToCSharpString()
{
var sourceCode = "public class Test { private String name; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_PrimitiveTypes_ShouldMapToCSharpPrimitives()
{
var sourceCode = "public class Test { private int count; private boolean active; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_CollectionTypes_ShouldMapToCSharp()
{
var sourceCode = "import java.util.*; public class Test { private ArrayList<String> items; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("List<string>", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_MapTypes_ShouldConvertToDictionary()
{
var sourceCode = "import java.util.HashMap; public class Test { private HashMap<String, Integer> map; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("Dictionary", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_DateTimeTypes_ShouldMapToCSharp()
{
var sourceCode = "import java.time.LocalDateTime; public class Test { private LocalDateTime date; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("DateTime", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_Extends_ShouldConvertToColon()
{
var sourceCode = "public class Derived extends Base { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains(": Base", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_Implements_ShouldConvertToComma()
{
var sourceCode = "public class Service implements Runnable { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("Runnable", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_ExtendsAndImplements_ShouldHandleBoth()
{
var sourceCode = "public class Service extends Base implements Runnable { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
#endregion
#region throws
[Fact]
public async Task ConvertAsync_ThrowsDeclaration_ShouldBeRemoved()
{
var sourceCode = "public void method() throws Exception;";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.DoesNotContain("throws", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_MultipleThrows_ShouldBeRemoved()
{
var sourceCode = "public void method() throws IOException, Exception;";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.DoesNotContain("throws", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_Annotations_ShouldBeRemoved()
{
var sourceCode = "@Override public String toString();";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.DoesNotContain("@Override", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_DeprecatedAnnotation_ShouldBeRemoved()
{
var sourceCode = "@Deprecated public void oldMethod();";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.DoesNotContain("@Deprecated", result.TransformedCode);
}
#endregion
#region final
[Fact]
public async Task ConvertAsync_FinalClass_ShouldPreserveFinal()
{
var sourceCode = "public final class Test { }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("final", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_FinalField_ShouldPreserveFinal()
{
var sourceCode = "public class Test { private final String name; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("final", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_Fields_ShouldConvertTypes()
{
var sourceCode = "public class Test { private String name; private int age; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_Methods_ShouldConvertSignatures()
{
var sourceCode = "public class Test { public String getName() { return null; } }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_StaticMethod_ShouldPreserveStatic()
{
var sourceCode = "public class Test { public static void main(String[] args) { } }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_AbstractMethod_ShouldPreserveAbstract()
{
var sourceCode = "public abstract class Test { public abstract void doSomething(); }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.Contains("abstract", result.TransformedCode);
}
#endregion
#region
[Fact]
public async Task ConvertAsync_ComplexClass_ShouldConvertAllFeatures()
{
var sourceCode = @"
package com.example.service;
import java.util.ArrayList;
import java.util.List;
public class UserService extends BaseService implements IUserService {
private ArrayList<String> users;
public void addUser(String name) { users.add(name); }
}";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
Assert.NotNull(result.TransformedCode);
Assert.Contains("namespace com.example.service", result.TransformedCode);
}
[Fact]
public async Task ConvertAsync_GenericClass_ShouldConvertGenerics()
{
var sourceCode = "public class Container<T> { private T value; }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_Interface_ShouldConvertInterface()
{
var sourceCode = "public interface Service { void execute(); }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
[Fact]
public async Task ConvertAsync_Enum_ShouldConvertEnum()
{
var sourceCode = "public enum Status { ACTIVE, INACTIVE }";
var syntaxTree = await _parser.ParseAsync(sourceCode);
var result = await _converter.ConvertAsync(syntaxTree, LanguageType.CSharp);
Assert.True(result.Success);
}
#endregion
}