|
| 1 | +using YamlDotNet.Core; |
| 2 | + |
| 3 | +namespace JavaToCSharp.Tests; |
| 4 | + |
| 5 | +public class SyntaxMappingTests |
| 6 | +{ |
| 7 | + [Fact] |
| 8 | + public void Deserialize_Mappings() |
| 9 | + { |
| 10 | + var mappingString = """ |
| 11 | + ImportMappings: |
| 12 | + org.junit.Test : XUnit |
| 13 | + java.util.List : "" |
| 14 | + """; |
| 15 | + |
| 16 | + var mappings = SyntaxMapping.Deserialize(mappingString); |
| 17 | + Assert.NotNull(mappings); |
| 18 | + Assert.Equal(2, mappings.ImportMappings.Count); |
| 19 | + Assert.Equal("XUnit", mappings.ImportMappings["org.junit.Test"]); |
| 20 | + Assert.Equal("", mappings.ImportMappings["java.util.List"]); |
| 21 | + Assert.False(mappings.ImportMappings.ContainsKey("other.Clazz")); |
| 22 | + Assert.Empty(mappings.VoidMethodMappings); |
| 23 | + Assert.Empty(mappings.NonVoidMethodMappings); |
| 24 | + Assert.Empty(mappings.AnnotationMappings); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void Conversion_Options_Defaults_To_Empty_Mappings() |
| 29 | + { |
| 30 | + var options = new JavaConversionOptions(); |
| 31 | + Assert.Empty(options.SyntaxMappings.ImportMappings); |
| 32 | + Assert.Empty(options.SyntaxMappings.VoidMethodMappings); |
| 33 | + Assert.Empty(options.SyntaxMappings.NonVoidMethodMappings); |
| 34 | + Assert.Empty(options.SyntaxMappings.AnnotationMappings); |
| 35 | + } |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData("VoidMethodMappings:\n org.junit.Assert.assertTrue : Assert.True")] |
| 39 | + [InlineData("VoidMethodMappings:\n assertTrue : \"\"")] |
| 40 | + [InlineData("NonVoidMethodMappings:\n org.junit.Assert.assertTrue : Assert.True")] |
| 41 | + [InlineData("NonVoidMethodMappings:\n assertTrue : \"\"")] |
| 42 | + public void Validation_Exceptions(string mappingString) |
| 43 | + { |
| 44 | + Assert.Throws<SemanticErrorException>(() => SyntaxMapping.Deserialize(mappingString)); |
| 45 | + } |
| 46 | + |
| 47 | + // The use of mappings is tested using a typical JUnit4 test converted to Xunit: |
| 48 | + // - Multiple java imports: rewritten and removed (empty value) |
| 49 | + // - Multiple java methods: rewritten (void) and not rewritten (non void) |
| 50 | + // - Multiple Java method annotations: rewritten and removed (no mapping) |
| 51 | + // Not tested: |
| 52 | + // - Qualified java method/annotation names (this would require a more elaborated handling of the scope) |
| 53 | + // - No qualified CSharp methods (this would require CSharp static imports, that are not implemented) |
| 54 | + // - Annotations with parameters |
| 55 | + [Fact] |
| 56 | + public void Conversion_With_Import_Method_And_Annotation_Mappings() |
| 57 | + { |
| 58 | + const string javaCode = """ |
| 59 | + import static org.junit.Assert.assertEquals; |
| 60 | + import static org.junit.Assert.assertTrue; |
| 61 | + import org.junit.Test; |
| 62 | + public class MappingsTest { |
| 63 | + @Test @CustomJava @NotMapped |
| 64 | + public void testAsserts() { |
| 65 | + assertEquals("a", "a"); |
| 66 | + assertTrue(true); |
| 67 | + va.assertTrue(true); // non void is not mapped |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + """; |
| 72 | + var mappingsYaml = """ |
| 73 | + ImportMappings: |
| 74 | + org.junit.Test : Xunit |
| 75 | + #to remove static imports |
| 76 | + org.junit.Assert.assertEquals : "" |
| 77 | + org.junit.Assert.assertTrue : "" |
| 78 | + VoidMethodMappings: |
| 79 | + assertEquals : Assert.Equal |
| 80 | + assertTrue : Assert.True |
| 81 | + AnnotationMappings: |
| 82 | + Test : Fact |
| 83 | + CustomJava : CustomCs |
| 84 | + |
| 85 | + """; |
| 86 | + const string expectedCSharpCode = """ |
| 87 | + using Xunit; |
| 88 | +
|
| 89 | + public class MappingsTest |
| 90 | + { |
| 91 | + [Fact] |
| 92 | + [CustomCs] |
| 93 | + public virtual void TestAsserts() |
| 94 | + { |
| 95 | + Assert.Equal("a", "a"); |
| 96 | + Assert.True(true); |
| 97 | + va.AssertTrue(true); // non void is not mapped |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + """; |
| 102 | + |
| 103 | + var parsed = GetParsed(javaCode, mappingsYaml); |
| 104 | + Assert.Equal(expectedCSharpCode.ReplaceLineEndings(), parsed.ReplaceLineEndings()); |
| 105 | + } |
| 106 | + |
| 107 | + private static string GetParsed(string javaCode, string mappingsYaml) |
| 108 | + { |
| 109 | + var mappings = SyntaxMapping.Deserialize(mappingsYaml); |
| 110 | + var options = new JavaConversionOptions { IncludeNamespace = false, IncludeUsings = false, SyntaxMappings = mappings }; |
| 111 | + options.WarningEncountered += (_, eventArgs) |
| 112 | + => Console.WriteLine("Line {0}: {1}", eventArgs.JavaLineNumber, eventArgs.Message); |
| 113 | + var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? ""; |
| 114 | + return parsed; |
| 115 | + } |
| 116 | +} |
0 commit comments