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