Skip to content

Commit bbee426

Browse files
committed
fix docs about init and add tests
1 parent b4706f3 commit bbee426

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/MySwiftClass.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public class MySwiftClass {
5959
self.y = 5
6060
}
6161

62+
convenience public init(throwing: Bool) throws {
63+
if throwing {
64+
throw MySwiftError.swiftError
65+
} else {
66+
self.init()
67+
}
68+
}
69+
6270
deinit {
6371
p("deinit called!")
6472
}

Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/MySwiftStruct.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public struct MySwiftStruct {
2121
self.len = len
2222
}
2323

24+
public init?(doInit: Bool) {
25+
if doInit {
26+
self.init(cap: 10, len: 10)
27+
} else {
28+
return nil
29+
}
30+
}
31+
2432
public func getCapacity() -> Int64 {
2533
self.cap
2634
}

Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/MySwiftClassTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ void init_withParameters() {
4040
}
4141
}
4242

43+
@Test
44+
void init_throwing() {
45+
try (var arena = SwiftArena.ofConfined()) {
46+
Exception exception = assertThrows(Exception.class, () -> MySwiftClass.init(true, arena));
47+
assertEquals("swiftError", exception.getMessage());
48+
49+
MySwiftClass c = assertDoesNotThrow(() -> MySwiftClass.init(false, arena));
50+
assertNotNull(c);
51+
}
52+
}
53+
4354
@Test
4455
void sum() {
4556
try (var arena = SwiftArena.ofConfined()) {

Samples/SwiftJavaExtractJNISampleApp/src/test/java/com/example/swift/MySwiftStructTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
1919
import org.swift.swiftkit.core.SwiftArena;
2020

21+
import java.util.Optional;
22+
2123
import static org.junit.jupiter.api.Assertions.*;
2224

2325
public class MySwiftStructTest {
@@ -30,6 +32,17 @@ void init() {
3032
}
3133
}
3234

35+
@Test
36+
void init_optional() {
37+
try (var arena = SwiftArena.ofConfined()) {
38+
assertEquals(Optional.empty(), MySwiftStruct.init(false, arena));
39+
40+
Optional<MySwiftStruct> optionalStruct = MySwiftStruct.init(true, arena);
41+
assertTrue(optionalStruct.isPresent());
42+
assertEquals(10, optionalStruct.get().getLen());
43+
}
44+
}
45+
3346
@Test
3447
void getAndSetLen() {
3548
try (var arena = SwiftArena.ofConfined()) {

Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ SwiftJava's `swift-java jextract` tool automates generating Java bindings from S
4747
| Swift Feature | FFM | JNI |
4848
|--------------------------------------------------------------------------------------|----------|-----|
4949
| Initializers: `class`, `struct` |||
50-
| Optional Initializers / Throwing Initializers || |
50+
| Optional Initializers / Throwing Initializers || |
5151
| Deinitializers: `class`, `struct` |||
5252
| `enum` |||
5353
| `actor` |||

0 commit comments

Comments
 (0)