Skip to content

Commit

Permalink
[J2KT] Add J2ObjC interop test to check that the names are compatible…
Browse files Browse the repository at this point in the history
… with J2KT.

PiperOrigin-RevId: 733066633
  • Loading branch information
Googler authored and copybara-github committed Mar 3, 2025
1 parent 228cce0 commit 56463fb
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#import <XCTest/XCTest.h>

#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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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, @"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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".
}
}

0 comments on commit 56463fb

Please sign in to comment.