Repository navigation
Expand file tree
/
Copy pathConvertTypeTests.cs
More file actions
240 lines (198 loc) · 8.39 KB
/
Copy pathConvertTypeTests.cs
File metadata and controls
240 lines (198 loc) · 8.39 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using com.github.javaparser.ast.type;
namespace JavaToCSharp.Tests;
public class ConvertTypeTests
{
[Fact]
public void ConvertType_Int()
{
Assert.Equal("int", TypeHelper.ConvertType("int"));
}
[Fact]
public void ConvertType_String()
{
Assert.Equal("string", TypeHelper.ConvertType("String"));
}
[Fact]
public void ConvertType_Object()
{
Assert.Equal("object", TypeHelper.ConvertType("Object"));
}
[Fact]
public void ConvertType_IntArray_BracketsAfterType()
{
Assert.Equal("int[]", TypeHelper.ConvertType("int[]"));
}
[Theory]
[InlineData("String[]", "string[]")]
[InlineData("Integer[]", "int[]")]
[InlineData("String[][]", "string[][]")]
[InlineData("MyType<String>[]", "MyType<string>[]")]
[InlineData("List<String[]>", "IList<string[]>")]
[InlineData("MyType[]", "MyType[]")]
public void ConvertType_Arrays(string javaType, string expected)
{
Assert.Equal(expected, TypeHelper.ConvertType(javaType));
}
[Fact]
public void ConvertType_ArrayMethodSignature()
{
const string javaCode = """
public interface Lemmatizer {
String[] lemmatize(String[] toks, String[] tags);
}
""";
var options = new JavaConversionOptions
{
IncludeUsings = false,
IncludeNamespace = false,
};
var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? "";
Assert.Contains("string[] Lemmatize(string[] toks, string[] tags);", parsed);
}
[Theory]
// Collection interfaces keep their abstraction rather than binding to a concrete type (#134).
[InlineData("Map<String, Integer>", "IDictionary<string, int>")]
[InlineData("Set<String>", "ISet<string>")]
[InlineData("Collection<String>", "ICollection<string>")]
[InlineData("Iterable<String>", "IEnumerable<string>")]
[InlineData("SortedMap<String, Integer>", "IDictionary<string, int>")]
// ...while the concrete java implementations map to instantiable .NET types.
[InlineData("HashMap<String, Integer>", "Dictionary<string, int>")]
[InlineData("LinkedHashMap<String, Integer>", "Dictionary<string, int>")]
[InlineData("TreeMap<String, Integer>", "SortedDictionary<string, int>")]
[InlineData("TreeSet<String>", "SortedSet<string>")]
[InlineData("LinkedHashSet<String>", "HashSet<string>")]
public void ConvertType_Collections(string javaType, string expected)
{
Assert.Equal(expected, TypeHelper.ConvertType(javaType));
}
[Theory]
[InlineData("Character", "char")]
[InlineData("Double", "double")]
[InlineData("Short", "short")]
[InlineData("Byte", "sbyte")] // java's byte is signed
[InlineData("BigDecimal", "decimal")]
[InlineData("StringBuffer", "StringBuilder")]
public void ConvertType_SimpleTypes(string javaType, string expected)
{
Assert.Equal(expected, TypeHelper.ConvertType(javaType));
}
[Theory]
[InlineData("Throwable", "Exception")]
[InlineData("ClassCastException", "InvalidCastException")]
[InlineData("NumberFormatException", "FormatException")]
[InlineData("IndexOutOfBoundsException", "IndexOutOfRangeException")]
[InlineData("ArrayIndexOutOfBoundsException", "IndexOutOfRangeException")]
[InlineData("NoSuchElementException", "InvalidOperationException")]
[InlineData("OutOfMemoryError", "OutOfMemoryException")]
public void ConvertType_Exceptions(string javaType, string expected)
{
Assert.Equal(expected, TypeHelper.ConvertType(javaType));
}
[Fact]
public void ConvertType_MapDeclaration_AssignedFromHashMap()
{
const string javaCode = """
import java.util.*;
public class Holder {
private Map<String, Integer> counts = new HashMap<String, Integer>();
}
""";
var options = new JavaConversionOptions
{
IncludeUsings = false,
IncludeNamespace = false,
};
var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? "";
Assert.Contains("private IDictionary<string, int> counts = new Dictionary<string, int>();", parsed);
}
[Fact]
public void ConvertType_GenericSingleParameter()
{
Assert.Equal("MyType<string>", TypeHelper.ConvertType("MyType<String>"));
}
[Fact]
public void ConvertType_GenericMultipleParameters()
{
Assert.Equal("MyType<string, object>", TypeHelper.ConvertType("MyType<String, Object>"));
}
[Fact]
public void ConvertType_WithGenericWildcard_ShouldReplaceToken()
{
Assert.Equal("MyType<TWildcardTodo>", TypeHelper.ConvertType("MyType<?>"));
}
[Fact]
public void ConvertTypeSyntax_GivenNonArrayType_ShouldReturnCorrectSyntax()
{
var type = new PrimitiveType(PrimitiveType.Primitive.INT);
var syntax = TypeHelper.ConvertTypeSyntax(type, 0);
Assert.Equal("int", syntax.ToString());
}
[Fact]
public void ConvertTypeSyntax_GivenNonArrayTypeWithRank_ShouldReturnCorrectSyntax()
{
var type = new PrimitiveType(PrimitiveType.Primitive.INT);
var syntax = TypeHelper.ConvertTypeSyntax(type, 1);
Assert.Equal("int[]", syntax.ToString());
}
[Fact]
public void ConvertTypeSyntax_GivenNonArrayTypeWithMultidimensionalRank_ShouldReturnCorrectSyntax()
{
var type = new PrimitiveType(PrimitiveType.Primitive.INT);
var syntax = TypeHelper.ConvertTypeSyntax(type, 2);
Assert.Equal("int[,]", syntax.ToString());
}
[Fact]
public void ConvertTypeSyntax_GivenArrayTypeWithoutRank_ShouldReturnCorrectSyntax()
{
var type = new ArrayType(new PrimitiveType(PrimitiveType.Primitive.INT));
var syntax = TypeHelper.ConvertTypeSyntax(type, 0);
Assert.Equal("int[]", syntax.ToString());
}
[Fact]
public void ConvertTypeSyntax_GivenArrayTypeWithRank_ShouldReturnCorrectSyntax()
{
var type = new ArrayType(new PrimitiveType(PrimitiveType.Primitive.INT));
var syntax = TypeHelper.ConvertTypeSyntax(type, 1);
Assert.Equal("int[]", syntax.ToString());
}
[Fact]
public void ConvertTypeSyntax_GivenArrayTypeWithMultidimensionalRank_ShouldReturnCorrectSyntax()
{
var type = new ArrayType(new ArrayType(new PrimitiveType(PrimitiveType.Primitive.INT)));
var syntax = TypeHelper.ConvertTypeSyntax(type, 2);
Assert.Equal("int[,]", syntax.ToString());
}
[Fact]
public void ConvertTypeSyntax_GivenMismatchedRank_ShouldThrowException()
{
var type = new ArrayType(new ArrayType(new PrimitiveType(PrimitiveType.Primitive.INT)));
Assert.Throws<ArgumentException>(() => TypeHelper.ConvertTypeSyntax(type, 1));
}
[Theory]
[InlineData("GenericClass<T>", "GenericClass<T>")]
[InlineData("GenericClass<T extends BoundType<T>>", "GenericClass<T>\n where T : BoundType<T>")]
[InlineData("GenericClass<T extends BoundType>", "GenericClass<T>\n where T : BoundType")]
[InlineData("GenericClass<T, U>", "GenericClass<T, U>")]
[InlineData("GenericClass<T, U extends BoundType<U>>", "GenericClass<T, U>\n where U : BoundType<U>")]
[InlineData("GenericClass<T extends BoundType1<T>, U extends BoundType2<U>>", "GenericClass<T, U>\n where T : BoundType1<T> where U : BoundType2<U>")]
[InlineData("GenericClass<T extends BoundType1<T> & BoundType2<T>>", "GenericClass<T>\n where T : BoundType1<T>, BoundType2<T>")]
public void ConvertClassTypeBoundedParameters(string javaClass, string csharpClass)
{
string javaCode = $$"""
public class {{javaClass}} { }
""";
var options = new JavaConversionOptions
{
IncludeUsings = false,
IncludeNamespace = false,
};
var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? "";
string expected = $$"""
public class {{csharpClass}}
{
}
""";
Assert.Equal(expected.ReplaceLineEndings(), parsed.ReplaceLineEndings());
}
}