Skip to content

Test bridging Java types to AnyDynamicObject #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/SkipBridge/AnyDynamicObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ open class AnyDynamicObject: JObjectProtocol, JConvertible {
}
}

/// Cast this instance to a generated `AnyDynamicObject` subclass type.
public func `as`<T: AnyDynamicObject>(_ type: T.Type) throws -> T {
return try T(for: object.ptr)
}

public subscript(dynamicMember member: String) -> Bool {
get {
jniContext {
Expand Down
10 changes: 10 additions & 0 deletions Sources/SkipBridgeToKotlinSamples/Samples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ public class SwiftClass {
}
}

extension SwiftClass {
public func swiftExtensionFunc(_ i: Int) -> Int {
return i
}
}

public final class SwiftSubclass: SwiftClass {
public var stringVar: String

Expand All @@ -150,6 +156,10 @@ public final class SwiftHelperClass: SwiftProtocol, Comparable, Identifiable {
public init() {
}

public init(string: String?) throws {
self.stringVar = string!
}

public func stringValue() -> String {
return stringVar
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/SkipBridgeToSwiftSamples/Samples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public var kotlinInnerClassVar = KotlinHelperClass.Inner()
public var kotlinSwiftClassVar = SwiftHelperClass()
public var kotlinAnyVar: Any = "a"
public var kotlinAnyHashableVar: AnyHashable = 1
#if os(Android) || ROBOLECTRIC
public var kotlinJavaTypeVar: java.util.Date = java.util.Date()
#endif

// MARK: Global optional vars

Expand Down Expand Up @@ -134,6 +137,12 @@ public class KotlinClass {
}
}

extension KotlinClass {
public func kotlinExtensionFunc(_ i: Int) -> Int {
return i
}
}

public final class KotlinSubclass: KotlinClass {
public var stringVar: String

Expand Down
27 changes: 27 additions & 0 deletions Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// as published by the Free Software Foundation https://fsf.org

import Foundation
import SkipBridge
import SkipBridgeToSwiftSamples
import SkipBridgeToSwiftSamplesHelpers

Expand Down Expand Up @@ -181,6 +182,27 @@ public func testSupport_kotlinAnyHashableVar_kotlinClass(value: String) -> Strin
return (kotlinAnyHashableVar as? KotlinHelperClass)?.stringVar
}

public func testSupport_kotlinJavaTypeVar() -> String? {
#if os(Android) || ROBOLECTRIC
guard kotlinJavaTypeVar is AnyDynamicObject else {
return "kotlinJavaTypeVar is AnyDynamicObject"
}
let time: Int64 = kotlinJavaTypeVar.time
guard time > 0 else {
return "time > 0"
}
let date = try! AnyDynamicObject(className: "java.util.Date", 999)
kotlinJavaTypeVar = date
let time2: Int64 = kotlinJavaTypeVar.time
guard time2 == 999 else {
return "time2 == 999"
}
return nil
#else
return nil
#endif
}

public func testSupport_kotlinOptionalBoolVar(value: Bool?) -> Bool? {
kotlinOptionalBoolVar = value
return kotlinOptionalBoolVar
Expand Down Expand Up @@ -334,6 +356,11 @@ public func testSupport_kotlinClassComparable(lhs: String, rhs: String) -> Bool
return lhsHelper < rhsHelper
}

public func testSupport_kotlinExtension(value: Int) -> Int {
let cls = KotlinClass()
return cls.kotlinExtensionFunc(value)
}

public func testSupport_kotlinSubclass() -> String? {
let sub = KotlinSubclass(string: "sub")
sub.intVar = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ final class BridgeToKotlinTests: XCTestCase {
XCTAssertFalse(lhs < rhs)
}

public func testSwiftExtension() {
let cls = SwiftClass()
XCTAssertEqual(100, cls.swiftExtensionFunc(100))
}

public func testSwiftSubclass() {
let sub = SwiftSubclass(string: "sub")
sub.intVar = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ final class BridgeToSwiftTests: XCTestCase {
XCTAssertEqual(testSupport_kotlinAnyHashableVar_kotlinClass(value: "ss"), "ss")
}

func testJavaTypeVar() {
XCTAssertNil(testSupport_kotlinJavaTypeVar())
}

func testAnyVarContainerValues() {
let anyArray = testSupport_kotlinAnyVar(value: ["a", 2, 3.0]) as? [Any]
guard let anyArray else {
Expand Down Expand Up @@ -192,7 +196,11 @@ final class BridgeToSwiftTests: XCTestCase {
XCTAssertFalse(testSupport_kotlinClassComparable(lhs: "bbb", rhs: "aaa"))
}

public func testKotlinSubclass() {
func testKotlinExtension() {
XCTAssertEqual(100, testSupport_kotlinExtension(value: 100))
}

func testKotlinSubclass() {
XCTAssertNil(testSupport_kotlinSubclass())
}

Expand Down
Loading