Skip to content

Commit cc7ccb4

Browse files
weijunkongcopybara-github
authored andcommitted
Add integration tests for KmpAdapter
PiperOrigin-RevId: 907186879
1 parent 2ce8b43 commit cc7ccb4

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

translator/src/main/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ private NativeTypeVisitor(
748748
public Void visitDeclared(DeclaredType type, StringBuilder builder) {
749749
builder.append(toNativeType(type, methodExecutable, referencedTypes));
750750
List<? extends TypeMirror> typeArguments = type.getTypeArguments();
751-
if (!typeArguments.isEmpty()) {
751+
if (options.asObjCGenericDecl() && !typeArguments.isEmpty()) {
752752
String typeArgsString = buildTypeArgumentString(typeArguments);
753753
builder.append("<").append(typeArgsString).append(">");
754754
}

translator/src/test/java/com/google/devtools/j2objc/translate/ObjectiveCKmpMethodTranslatorTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ObjectiveCKmpMethodTranslatorTest extends GenerationTest {
2424
@Override
2525
public void setUp() throws IOException {
2626
super.setUp();
27+
options.load(new String[] {"--objc-generics"});
2728
addSourceFile(
2829
"""
2930
package com.google.common.collect;
@@ -276,7 +277,8 @@ public List<List<String>> getMatrix() {
276277
assertInTranslation(
277278
testImplementation,
278279
"return (NSArray<NSArray<NSString *> *> *) [Adapter"
279-
+ " fromJavaUtilList_JavaUtilList_WithJavaUtilList:(id<JavaUtilList>) [self getMatrix]]");
280+
+ " fromJavaUtilList_JavaUtilList_WithJavaUtilList:(id<JavaUtilList>) [self"
281+
+ " getMatrix]]");
280282
}
281283

282284
/** Tests mixed nested collection conversion with @ObjectiveCKmpMethod. */
@@ -1061,7 +1063,6 @@ public ImmutableList<CustomClass> getItems() {
10611063
assertNotInTranslation(
10621064
concreteHeader, "- (void)setItems:(NSArray<MyPkgCustomClass *> *)items;");
10631065
assertNotInTranslation(concreteHeader, "- (NSArray<MyPkgCustomClass *> *)getItems;");
1064-
assertNotInTranslation(concreteHeader, "@class MyPkgCustomClass;");
10651066

10661067
String concreteImpl = translateSourceFile("ConcreteClass", "ConcreteClass.m");
10671068
assertNotInTranslation(concreteImpl, "- (void)setItems:(NSArray<MyPkgCustomClass *> *)items {");
@@ -1405,6 +1406,37 @@ void ConstTest_initWithList_(ConstTest *self_, NSArray<NSString *> *list) {
14051406
""");
14061407
}
14071408

1409+
public void testObjCGenerics() throws IOException {
1410+
1411+
addSourceFile(
1412+
"""
1413+
import com.google.j2objc.annotations.ObjectiveCKmpMethod;
1414+
import java.util.List;
1415+
public class GenericsOn {
1416+
@ObjectiveCKmpMethod(selector="setList:", adapter=Adapter.class)
1417+
public void setList(List<String> list) {}
1418+
}
1419+
""",
1420+
"GenericsOn.java");
1421+
String headerOn = translateSourceFile("GenericsOn", "GenericsOn.h");
1422+
assertInTranslation(headerOn, "- (void)setList:(NSArray<NSString *> *)list;");
1423+
1424+
options.setAsObjCGenericDecl(false);
1425+
addSourceFile(
1426+
"""
1427+
import com.google.j2objc.annotations.ObjectiveCKmpMethod;
1428+
import java.util.List;
1429+
public class GenericsOff {
1430+
@ObjectiveCKmpMethod(selector="setList:", adapter=Adapter.class)
1431+
public void setList(List<String> list) {}
1432+
}
1433+
""",
1434+
"GenericsOff.java");
1435+
String headerOff = translateSourceFile("GenericsOff", "GenericsOff.h");
1436+
assertInTranslation(headerOff, "- (void)setList:(NSArray *)list;");
1437+
options.setAsObjCGenericDecl(true);
1438+
}
1439+
14081440
public void testSetOfBooleanFails() throws IOException {
14091441
addSourceFile(
14101442
"""

0 commit comments

Comments
 (0)