-
Notifications
You must be signed in to change notification settings - Fork 962
/
NameTableTest.java
298 lines (270 loc) · 13.6 KB
/
NameTableTest.java
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright 2012 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.j2objc.util;
import com.google.devtools.j2objc.GenerationTest;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.AbstractTypeDeclaration;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.MethodDeclaration;
import com.google.devtools.j2objc.ast.TreeVisitor;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import java.io.IOException;
/**
* Unit tests for {@link NameTable}.
*
* @author Tom Ball
*/
public class NameTableTest extends GenerationTest {
// Verify class name without package is unchanged.
public void testGetFullNameNoPackage() {
String source = "public class SomeClass {}";
CompilationUnit unit = translateType("SomeClass", source);
NameTable nameTable = unit.getNameTable();
AbstractTypeDeclaration decl = unit.getTypes().get(0);
assertEquals("SomeClass", nameTable.getFullName(decl.getTypeBinding()));
}
// Verify class name with package is camel-cased.
public void testGetFullNameWithPackage() {
String source = "package foo.bar; public class SomeClass {}";
CompilationUnit unit = translateType("SomeClass", source);
NameTable nameTable = unit.getNameTable();
AbstractTypeDeclaration decl = unit.getTypes().get(0);
assertEquals("FooBarSomeClass", nameTable.getFullName(decl.getTypeBinding()));
}
// Verify inner class name with package is camel-cased.
public void testGetFullNameWithInnerClass() {
String source = "package foo.bar; public class SomeClass { static class Inner {}}";
CompilationUnit unit = translateType("SomeClass", source);
NameTable nameTable = unit.getNameTable();
AbstractTypeDeclaration decl = unit.getTypes().get(1);
assertEquals("FooBarSomeClass_Inner", nameTable.getFullName(decl.getTypeBinding()));
}
// Verify the name of an inner class of an enum.
public void testGetFullNameEnumWithInnerClasses() {
String source = "package foo.bar; "
+ "public enum SomeClass { A; static class Inner {} static enum Inner2 { B; }}";
CompilationUnit unit = translateType("SomeClass", source);
NameTable nameTable = unit.getNameTable();
AbstractTypeDeclaration decl = unit.getTypes().get(1);
// Outer type should not have "Enum" added to name.
assertEquals("FooBarSomeClass_Inner", nameTable.getFullName(decl.getTypeBinding()));
// Inner enum should have "Enum" added to name.
decl = unit.getTypes().get(2);
assertEquals("FooBarSomeClass_Inner2", nameTable.getFullName(decl.getTypeBinding()));
}
// Verify local class name.
public void testGetFullNameWithLocalClass() {
String source = "package foo.bar; class SomeClass { void test() { "
// Put each Foo in a separate scope, so leading index number changes.
// This matches JVM naming, once '$' is substituted for the '_' characters.
+ "{ class Foo {}} { class Foo {}}}}";
CompilationUnit unit = translateType("SomeClass", source);
NameTable nameTable = unit.getNameTable();
AbstractTypeDeclaration decl = unit.getTypes().get(1);
assertEquals("FooBarSomeClass_1Foo", nameTable.getFullName(decl.getTypeBinding()));
decl = unit.getTypes().get(2);
assertEquals("FooBarSomeClass_2Foo", nameTable.getFullName(decl.getTypeBinding()));
}
public void testTypeVariableWithTypeVariableBounds() {
String source = "class A<T> { <E extends T> void foo(E e) {} }";
CompilationUnit unit = translateType("A", source);
NameTable nameTable = unit.getNameTable();
final IMethodBinding[] methodBinding = new IMethodBinding[1];
unit.accept(new TreeVisitor() {
@Override public void endVisit(MethodDeclaration node) {
IMethodBinding binding = node.getMethodBinding();
if (binding.getName().equals("foo")) {
methodBinding[0] = binding;
}
}
});
assertNotNull(methodBinding[0]);
ITypeBinding paramType = methodBinding[0].getParameterTypes()[0];
assertEquals("id", nameTable.getObjCType(paramType));
}
public void testPrimitiveArrayParameterName() throws IOException {
String translation = translateSourceFile("public class A { "
+ "void foo(int[] value1) {}"
+ "void foo(Integer[] value2) {}"
+ "void foo(String[] value3) {}}", "A", "A.h");
assertTranslation(translation, "- (void)fooWithIntArray:(IOSIntArray *)value1");
assertTranslation(translation,
"- (void)fooWithJavaLangIntegerArray:(IOSObjectArray *)value2");
assertTranslation(translation,
"- (void)fooWithNSStringArray:(IOSObjectArray *)value3");
}
public void testMultiDimArrayName() throws IOException {
String translation = translateSourceFile("public class A { "
+ "void foo(int[] values) {}"
+ "void foo(int[][] values) {}"
+ "void foo(int[][][] values) {}}", "A", "A.h");
assertTranslation(translation, "- (void)fooWithIntArray:(IOSIntArray *)values");
assertTranslation(translation, "- (void)fooWithIntArray2:(IOSObjectArray *)values");
assertTranslation(translation, "- (void)fooWithIntArray3:(IOSObjectArray *)values");
}
public void testRenameClassAnnotation() throws IOException {
addSourceFile("package foo; "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"TestName\") "
+ "public class A { public static void test() {} "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"TheInner\") "
+ "public static class C { public static void test2() {} } }", "foo/A.java");
addSourceFile(
"public class B { void test() { foo.A.test(); foo.A.C.test2(); }}", "B.java");
String translation = translateSourceFile("foo.A", "foo/A.h");
assertTranslation(translation, "@interface TestName : NSObject");
assertTranslation(translation, "@interface TheInner : NSObject");
translation = translateSourceFile("B", "B.m");
assertTranslatedLines(translation, "TestName_test();", "TheInner_test2();");
}
public void testRenameMapping() throws IOException {
Options.getClassMappings().put("foo.bar.A", "Test2Name");
try {
addSourceFile("package foo.bar; public class A { static void test() {}}", "foo/bar/A.java");
addSourceFile(
"package foo.bar; public class B { void test() { A.test(); }}", "foo/bar/B.java");
String translation = translateSourceFile("foo.bar.A", "foo/bar/A.h");
assertTranslation(translation, "@interface Test2Name : NSObject");
translation = translateSourceFile("foo.bar.B", "foo/bar/B.m");
assertTranslation(translation, "Test2Name_test();");
} finally {
Options.getClassMappings().remove("foo.bar.A");
}
}
public void testRenameMethodAnnotation() throws IOException {
String objcName = "test:offset:";
addSourceFile("public class A { "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"" + objcName + "\") "
+ "void test(String s, int n) {}}", "A.java");
String translation = translateSourceFile("A", "A.h");
assertTranslatedLines(translation,
"- (void)test:(NSString *)s",
"offset:(jint)n;");
assertNotInTranslation(translation, "testWithNSString:");
translation = getTranslatedFile("A.m");
assertTranslatedLines(translation,
"- (void)test:(NSString *)s",
"offset:(jint)n {");
assertNotInTranslation(translation, "testWithNSString:");
// Test invocation of renamed method.
translation = translateSourceFile(
"class Test { void test(A a) { a.test(\"foo\", 4); } }", "Test", "Test.m");
assertTranslation(translation, "[((A *) nil_chk(a)) test:@\"foo\" offset:4];");
}
public void testRenameStaticMethod() throws IOException {
String translation = translateSourceFile("public class Test { "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"foo\") "
+ "static void test(String s, int n) {}}", "Test", "Test.h");
assertTranslatedLines(translation,
"+ (void)fooWithNSString:(NSString *)s",
" withInt:(jint)n;");
assertTranslation(translation, "FOUNDATION_EXPORT void Test_foo(NSString *s, jint n);");
translation = getTranslatedFile("Test.m");
assertTranslatedLines(translation,
"+ (void)fooWithNSString:(NSString *)s",
" withInt:(jint)n {",
" Test_foo(s, n);",
"}");
assertTranslatedLines(translation,
"t_foo(NSString *s, jint n) {",
" Test_initialize();",
"}");
}
public void testRenameConstructorAnnotation() throws IOException {
String objcName = "init:offset:";
addSourceFile("public class A { "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"" + objcName + "\") "
+ "A(String s, int n) {}}", "A.java");
String translation = translateSourceFile("A", "A.h");
assertTranslatedLines(translation,
"- (instancetype)init:(NSString *)s",
"offset:(jint)n;");
assertNotInTranslation(translation, "testWithNSString");
translation = getTranslatedFile("A.m");
assertTranslatedLines(translation,
"- (instancetype)init:(NSString *)s",
"offset:(jint)n {");
assertNotInTranslation(translation, "testWithNSString");
// Test invocation of renamed constructor.
translation = translateSourceFile(
"class Test { A test() { return new A(\"foo\", 5); } }", "Test", "Test.m");
assertTranslation(translation, "return create_A_init_offset_(@\"foo\", 5);");
}
public void testSuperMethodNotNamedWarning() throws IOException {
translateSourceFile("class A { void test(String s, int n) {}"
+ "static class B extends A { "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"test:(NSString *)s offset:(int)n\")"
+ "@Override void test(String s, int n) {}}}", "A", "A.m");
assertWarningCount(1);
}
public void testMethodConflictingName() throws IOException {
translateSourceFile("class A { "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"foo:bar:\")"
+ "void test(String s, int n) {}"
+ "static class B extends A { "
+ "@com.google.j2objc.annotations.ObjectiveCName(\"test:offset:\")"
+ "@Override void test(String s, int n) {}}}", "A", "A.m");
assertWarningCount(1);
}
// Verify enum constant names are not modified, even if they use a reserved word.
// This is necessary for compatibility with proto compiler output.
public void testGetReservedEnumConstantName() throws IOException {
String translation = translateSourceFile("enum E { HUGE }", "E", "E.h");
assertTranslation(translation, "HUGE");
assertNotInTranslation(translation, "HUGE_");
}
public void testRenamePackageAnnotation() throws IOException {
addSourcesToSourcepaths();
addSourceFile("@com.google.j2objc.annotations.ObjectiveCName(\"FB\") "
+ "package foo.bar;", "foo/bar/package-info.java");
addSourceFile("package foo.bar; public class Test {}", "foo/bar/Test.java");
String translation = translateSourceFile("foo.bar.Test", "foo/bar/Test.h");
assertTranslation(translation, "@interface FBTest : NSObject");
assertTranslation(translation, "J2OBJC_EMPTY_STATIC_INIT(FBTest)");
assertTranslation(translation, "@compatibility_alias FooBarTest FBTest;");
translation = getTranslatedFile("foo/bar/Test.m");
assertTranslation(translation, "#include \"foo/bar/Test.h\""); // should be full path.
assertTranslation(translation, "@implementation FBTest");
assertTranslation(translation, "J2ObjcClassInfo _FBTest = { 2, \"Test\", \"foo.bar\", ");
}
public void testRenamePackageAnnotationEnum() throws IOException {
addSourcesToSourcepaths();
addSourceFile("@com.google.j2objc.annotations.ObjectiveCName(\"FB\") "
+ "package foo.bar;", "foo/bar/package-info.java");
addSourceFile("package foo.bar; public enum Test { FOO, BAR }", "foo/bar/Test.java");
String translation = translateSourceFile("foo.bar.Test", "foo/bar/Test.h");
assertTranslatedLines(translation,
"typedef NS_ENUM(NSUInteger, FBTest_Enum) {",
" FBTest_Enum_FOO = 0,",
" FBTest_Enum_BAR = 1,",
"};");
assertTranslation(translation, "@interface FBTest : JavaLangEnum");
assertTranslation(translation, "FBTest_values()");
assertTranslation(translation, "+ (FBTest *)valueOfWithNSString:(NSString *)name;");
assertTranslation(translation, "FBTest *FBTest_valueOfWithNSString_");
assertTranslation(translation, "J2OBJC_STATIC_INIT(FBTest)");
assertTranslation(translation, "@compatibility_alias FooBarTest FBTest;");
translation = getTranslatedFile("foo/bar/Test.m");
assertTranslation(translation, "#include \"foo/bar/Test.h\""); // should be full path.
assertTranslation(translation, "@implementation FBTest");
assertTranslation(translation, "J2ObjcClassInfo _FBTest = { 2, \"Test\", \"foo.bar\", ");
// Make sure package-info class doesn't use prefix for its own type name.
translation = translateSourceFile("foo.bar.package-info", "foo/bar/package-info.m");
assertTranslation(translation, "@interface FooBarpackage_info");
assertTranslation(translation, "@implementation FooBarpackage_info");
assertNotInTranslation(translation, "FBpackage_info");
}
}