From d38745abe81324732517668e577932cfab2c869f Mon Sep 17 00:00:00 2001 From: Abe White Date: Wed, 27 Nov 2024 23:32:30 -0600 Subject: [PATCH] Add additional enum tests --- .../SkipBridgeToKotlinSamples/Samples.swift | 23 +++++++++++++++++++ .../SkipBridgeToSwiftSamples/Samples.swift | 14 +++++++++++ .../TestsSupport.swift | 8 ++++++- .../BridgeToKotlinSamplesTests.swift | 13 +++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Sources/SkipBridgeToKotlinSamples/Samples.swift b/Sources/SkipBridgeToKotlinSamples/Samples.swift index 38a2c24..5b8aed9 100644 --- a/Sources/SkipBridgeToKotlinSamples/Samples.swift +++ b/Sources/SkipBridgeToKotlinSamples/Samples.swift @@ -174,6 +174,29 @@ public struct SwiftStruct { } } +public enum SwiftEnum: String, CaseIterable { + case name + case age = "years" + + public func isName() -> Bool { + return self == .name + } + + public var intValue: Int { + switch self { + case .name: return 0 + case .age: return 1 + } + } + + public init(intValue: Int) { + switch intValue { + case 0: self = .name + default: self = .age + } + } +} + // MARK: Closures public var swiftClosure0Var: () -> Void = { print("original") } diff --git a/Sources/SkipBridgeToSwiftSamples/Samples.swift b/Sources/SkipBridgeToSwiftSamples/Samples.swift index 38d2c02..5823052 100644 --- a/Sources/SkipBridgeToSwiftSamples/Samples.swift +++ b/Sources/SkipBridgeToSwiftSamples/Samples.swift @@ -183,6 +183,20 @@ public enum KotlinEnum: String, CaseIterable { public func isName() -> Bool { return self == .name } + + public var intValue: Int { + switch self { + case .name: return 0 + case .age: return 1 + } + } + + public init(intValue: Int) { + switch intValue { + case 0: self = .name + default: self = .age + } + } } // MARK: Closures diff --git a/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift b/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift index cee99c8..d7c7f5f 100644 --- a/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift +++ b/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift @@ -440,8 +440,14 @@ public func testSupport_kotlinEnum() -> String? { guard e1.rawValue == "years" else { return "e1.rawValue == 'years'" } + guard e1.intValue == 1 else { + return "e1.intValue == 1" + } - let e2: KotlinEnum = .name + let e2 = KotlinEnum(intValue: 0) + guard e2 == .name else { + return "e2 == .name" + } guard e2.isName() else { return "e2.isName() == true" } diff --git a/Tests/SkipBridgeToKotlinSamplesTests/BridgeToKotlinSamplesTests.swift b/Tests/SkipBridgeToKotlinSamplesTests/BridgeToKotlinSamplesTests.swift index c5da57f..d5b5747 100644 --- a/Tests/SkipBridgeToKotlinSamplesTests/BridgeToKotlinSamplesTests.swift +++ b/Tests/SkipBridgeToKotlinSamplesTests/BridgeToKotlinSamplesTests.swift @@ -312,6 +312,19 @@ final class BridgeToKotlinTests: XCTestCase { XCTAssertEqual(obj.kotlinStructVar.intVar, 99) } + public func testEnum() { + let e1: SwiftEnum = .age + XCTAssertFalse(e1.isName()) + XCTAssertEqual(e1.rawValue, "years") + XCTAssertEqual(e1.intValue, 1) + + let e2 = SwiftEnum(intValue: 0) + XCTAssertTrue(e2 == .name) + XCTAssertTrue(e2.isName()) + + XCTAssertEqual(SwiftEnum.allCases.count, 2) + } + public func testClosure0Var() { swiftClosure0Var() swiftClosure0Var = { print("reassigned") }