From 56463fbad8b564ace29f8fbfd98be9a797b5378d Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 3 Mar 2025 14:41:23 -0800 Subject: [PATCH] [J2KT] Add J2ObjC interop test to check that the names are compatible with J2KT. PiperOrigin-RevId: 733066633 --- .../j2ktiosinterop/J2ObjCObjCInteropTest.m | 104 ++++++++++++++++++ .../J2ObjCSwiftInteropTest.swift | 83 ++++++++++++++ ...bjCInteropTest.m => J2ktObjCInteropTest.m} | 13 ++- ...pTest.swift => J2ktSwiftInteropTest.swift} | 7 +- .../integration/java/j2ktiosinterop/Main.java | 2 +- 5 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCObjCInteropTest.m create mode 100644 transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCSwiftInteropTest.swift rename transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/{ObjCInteropTest.m => J2ktObjCInteropTest.m} (87%) rename transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/{SwiftInteropTest.swift => J2ktSwiftInteropTest.swift} (90%) diff --git a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCObjCInteropTest.m b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCObjCInteropTest.m new file mode 100644 index 0000000000..525f6a3dec --- /dev/null +++ b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCObjCInteropTest.m @@ -0,0 +1,104 @@ +#import + +#import "transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/CustomNames.h" +#import "transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/DefaultNames.h" +#import "transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/EnumNames.h" + +/** J2ObjC interop test for ObjC. */ +@interface J2ObjCObjCInteropTest : XCTestCase +@end + +@implementation J2ObjCObjCInteropTest + +- (void)testDefaultNames { + J2ktiosinteropDefaultNames *obj; + obj = [[J2ktiosinteropDefaultNames alloc] init]; + obj = [[J2ktiosinteropDefaultNames alloc] initWithInt:1]; + obj = [[J2ktiosinteropDefaultNames alloc] initWithInt:1 withNSString:@""]; + + obj = create_J2ktiosinteropDefaultNames_init(); + obj = create_J2ktiosinteropDefaultNames_initWithInt_(1); + obj = create_J2ktiosinteropDefaultNames_initWithInt_withNSString_(1, @""); + + obj = new_J2ktiosinteropDefaultNames_init(); + obj = new_J2ktiosinteropDefaultNames_initWithInt_(1); + obj = new_J2ktiosinteropDefaultNames_initWithInt_withNSString_(1, @""); + + [obj method]; + [obj methodWithBoolean:YES]; + [obj methodWithChar:'a']; + [obj methodWithByte:1]; + [obj methodWithShort:1]; + [obj methodWithInt:1]; + [obj methodWithLong:1]; + [obj methodWithFloat:1]; + [obj methodWithDouble:1]; + [obj methodWithId:NULL]; + [obj methodWithNSString:NULL]; + [obj methodWithNSStringArray:NULL]; + [obj methodWithNSStringArray2:NULL]; + [obj methodWithNSCopying:NULL]; + [obj methodWithNSNumber:NULL]; + [obj methodWithIOSClass:NULL]; + [obj methodWithJavaLangIterable:NULL]; + [obj methodWithInt:1 withNSString:NULL]; + + [obj genericMethodWithId:NULL]; + [obj genericMethodWithNSString:NULL]; + + obj->field_ = obj->field_ + 1; + + J2ktiosinteropDefaultNames_staticMethod(); + J2ktiosinteropDefaultNames_staticMethodWithInt_(1); + J2ktiosinteropDefaultNames_staticMethodWithInt_withNSString_(1, @""); +} + +- (void)testCustomNames { + Custom *obj; + obj = [[Custom alloc] initWithIndex:1]; + obj = [[Custom alloc] initWithIndex:1 name:@""]; + + obj = [[Custom alloc] init]; + obj = [[Custom alloc] init2WithLong:1]; + obj = [[Custom alloc] init3WithLong:1 withNSString:@""]; + + obj = create_Custom_initWithIndex_(1); + obj = create_Custom_initWithIndex_name_(1, @""); + + obj = create_Custom_init(); + obj = create_Custom_init2(1); + obj = create_Custom_init3(1, @""); + + obj = new_Custom_initWithIndex_(1); + obj = new_Custom_initWithIndex_name_(1, @""); + + obj = new_Custom_init(); + obj = new_Custom_init2(1); + obj = new_Custom_init3(1, @""); + + [obj custom]; + [obj customWithIndex:1]; + [obj customWithIndex:1 name:@""]; + + [obj customWithLong:1]; + [obj customWithLong:1 withNSString:@""]; + + Custom_staticCustom(); + Custom_staticCustomWithIndex_(1); + Custom_staticCustomWithIndex_name_(1, @""); + + Custom_staticCustom2(1); + Custom_staticCustom3(2, @""); +} + +- (void)testEnumNames { + J2ktiosinteropEnumNames *e; + e = J2ktiosinteropEnumNames_get_ONE(); + e = J2ktiosinteropEnumNames_get_TWO(); + + J2ktiosinteropEnumNames_Enum e2; + e2 = J2ktiosinteropEnumNames_Enum_ONE; + e2 = J2ktiosinteropEnumNames_Enum_TWO; +} + +@end diff --git a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCSwiftInteropTest.swift b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCSwiftInteropTest.swift new file mode 100644 index 0000000000..98a1873755 --- /dev/null +++ b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ObjCSwiftInteropTest.swift @@ -0,0 +1,83 @@ +/* + * Copyright 2024 Google Inc. + * + * 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. + */ +import XCTest +import third_party_java_src_j2cl_transpiler_javatests_com_google_j2cl_integration_java_j2ktiosinterop_j2objc + +/// J2ObjC interop test for Swift. +final class J2ObjCSwiftInteropTest: XCTestCase { + func testDefaultNames() { + var obj: J2ktiosinteropDefaultNames + + obj = J2ktiosinteropDefaultNames() + obj = J2ktiosinteropDefaultNames(int: 1) + obj = J2ktiosinteropDefaultNames(int: 1, with: "") + + obj.method() + obj.method(withBoolean: true as jboolean) + obj.method(withChar: 65 as jchar) + obj.method(withByte: 1 as jbyte) + obj.method(withShort: 1 as jshort) + obj.method(with:1 as jint) + obj.method(withLong: 1 as jlong) + obj.method(with:1.0 as jfloat) + obj.method(with:1.0 as jdouble) + obj.method(withId: nil) + obj.method(with: nil as String?) + obj.method(withNSStringArray: nil) + obj.method(withNSStringArray2: nil) + //obj.methodWith(nil as NSCopying?) + obj.method(with: 1 as NSNumber) + obj.method(with: nil as IOSClass?) + obj.method(with: nil as JavaLangIterable?) + obj.method(with: 1, with: nil) + + obj.genericMethod(withId: nil as Any?) + obj.genericMethod(with: nil as String?) + + // Not exposed in Swift + //obj.field_ = obj.field_ + } + + func testCustomNames() { + var obj: Custom + + obj = Custom(index: 1 as jint) + obj = Custom(index: 1 as jint, name: "") + + obj = Custom() + // Not exposed on Swift + //obj = Custom(long: 1 as jlong) + //obj = Custom(long: 1 as jlong, withNSString: "") + + obj.custom() + obj.custom(with: 1 as jint) + obj.custom(with: 1 as jint, name: "") + + obj.custom(withLong: 1 as jlong) + obj.custom(withLong: 1 as jlong, with: "") + } + + func testEnumNames() { + var e: J2ktiosinteropEnumNames + e = J2ktiosinteropEnumNames_get_ONE() + e = J2ktiosinteropEnumNames_get_TWO() + + var e2: J2ktiosinteropEnumNames_Enum + // Not exposed on Swift + //e2 = J2ktiosinteropEnumNames_Enum_ONE; + //e2 = J2ktiosinteropEnumNames_Enum_TWO; + } +} diff --git a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/ObjCInteropTest.m b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ktObjCInteropTest.m similarity index 87% rename from transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/ObjCInteropTest.m rename to transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ktObjCInteropTest.m index b37eb5720e..bdf630bdee 100644 --- a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/ObjCInteropTest.m +++ b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ktObjCInteropTest.m @@ -4,10 +4,11 @@ #import "transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/DefaultNames.h" #import "transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/EnumNames.h" -@interface ObjCInteropTest : XCTestCase +/** J2KT interop test for ObjC. */ +@interface J2ktObjCInteropTest : XCTestCase @end -@implementation ObjCInteropTest +@implementation J2ktObjCInteropTest - (void)testDefaultNames { J2ktJ2ktiosinteropDefaultNames *obj; @@ -65,21 +66,27 @@ - (void)testCustomNames { obj = [[J2ktCustom alloc] initWithIndex:1 name:@""]; obj = [[J2ktCustom alloc] init]; + // TODO(b/400390599): Should be `init2WithLong:1` obj = [[J2ktCustom alloc] initWithLong:1]; + // TODO(b/400390599): Should be `init3WithLong:withNSString` obj = [[J2ktCustom alloc] initWithLong:1 withNSString:@""]; obj = create_Custom_initWithIndex_(1); obj = create_Custom_initWithIndex_name_(1, @""); obj = create_Custom_init(); + // TODO(b/400390599): Should be `create_Custom_init2` obj = create_Custom_initWithLong_(1); + // TODO(b/400390599): Should be `create_Custom_init3` obj = create_Custom_initWithLong_withNSString_(1, @""); obj = new_Custom_initWithIndex_(1); obj = new_Custom_initWithIndex_name_(1, @""); obj = new_Custom_init(); + // TODO(b/400390599): Should be `new_Custom_init2` obj = new_Custom_initWithLong_(1); + // TODO(b/400390599): Should be `new_Custom_init3` obj = new_Custom_initWithLong_withNSString_(1, @""); [obj custom]; @@ -100,7 +107,9 @@ - (void)testCustomNames { Custom_staticCustomWithIndex_(1); Custom_staticCustomWithIndex_name_(1, @""); + // TODO(b/400390599): Should be `Custom_staticCustom2` Custom_staticCustom2WithLong_(1); + // TODO(b/400390599): Should be `Custom_staticCustom3` Custom_staticCustom3WithLong_withNSString_(2, @""); } diff --git a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/SwiftInteropTest.swift b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ktSwiftInteropTest.swift similarity index 90% rename from transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/SwiftInteropTest.swift rename to transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ktSwiftInteropTest.swift index c14e61294c..dc20c19cb0 100644 --- a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/SwiftInteropTest.swift +++ b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/J2ktSwiftInteropTest.swift @@ -14,14 +14,17 @@ * the License. */ import XCTest -import third_party_java_src_j2cl_transpiler_javatests_com_google_j2cl_integration_java_j2ktiosinterop_SwiftInteropFramework +import third_party_java_src_j2cl_transpiler_javatests_com_google_j2cl_integration_java_j2ktiosinterop_J2ktSwiftInteropFramework -final class SwiftInteropTest: XCTestCase { +/// J2KT interop test for Swift. +final class J2ktSwiftInteropTest: XCTestCase { func testDefaultNames() { var obj: J2ktiosinteropDefaultNames obj = J2ktiosinteropDefaultNames() + // TODO(b/374280337): Should be J2ktiosinteropDefaultNames(int:) obj = J2ktiosinteropDefaultNames(Int: 1) + // TODO(b/374280337): Should be J2ktiosinteropDefaultNames(int:with:) obj = J2ktiosinteropDefaultNames(Int: 1, withNSString: "") obj.method() diff --git a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/Main.java b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/Main.java index e83c9b3df0..8ddedf2509 100644 --- a/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/Main.java +++ b/transpiler/javatests/com/google/j2cl/integration/java/j2ktiosinterop/Main.java @@ -17,6 +17,6 @@ public class Main { public static void main(String... args) { - // The actual interop tests are "ObjCInteropTest.m" and "SwiftInteropTest.swift". + // The actual interop tests are "J2ktObjCInteropTest.m" and "J2ktSwiftInteropTest.swift". } }