Skip to content

Commit

Permalink
Merge pull request #29 from skiptools/enums2
Browse files Browse the repository at this point in the history
Add additional enum tests
  • Loading branch information
aabewhite authored Nov 29, 2024
2 parents 7b26bc2 + d38745a commit 2b29e39
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Sources/SkipBridgeToKotlinSamples/Samples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down
14 changes: 14 additions & 0 deletions Sources/SkipBridgeToSwiftSamples/Samples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down

0 comments on commit 2b29e39

Please sign in to comment.