Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,33 @@ public func globalStringIdentity(string: String) -> String {
string
}

// ==== -----------------------------------------------------------------------
// MARK: Throwing functions

public struct SwiftExampleError: Error {
public let message: String
}

public func globalThrowingVoid(doThrow: Bool) throws {
if doThrow {
throw SwiftExampleError(message: "expected error in globalThrowingVoid")
}
}

public func globalThrowingReturn(doThrow: Bool) throws -> Int {
if doThrow {
throw SwiftExampleError(message: "expected error in globalThrowingReturn")
}
return 42
}

public func globalThrowingString(doThrow: Bool) throws -> String {
if doThrow {
throw SwiftExampleError(message: "expected error in globalThrowingString")
}
return "Hello from throwing Swift!"
}

// ==== -----------------------------------------------------------------------
// MARK: Overloaded functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.ffm.generated.SwiftJavaErrorException;

import java.util.concurrent.CountDownLatch;

Expand Down Expand Up @@ -95,4 +96,56 @@ public void apply() {
assertEquals(0, countDownLatch.getCount());
}

// ==== ----------------------------------------------------------------
// Throwing functions

@Test
void call_globalThrowingVoid_noThrow() throws SwiftJavaErrorException {
MySwiftLibrary.globalThrowingVoid(false);
}

@Test
void call_globalThrowingVoid_throws() {
assertThrows(SwiftJavaErrorException.class, () -> {
MySwiftLibrary.globalThrowingVoid(true);
});
}

@Test
void call_globalThrowingReturn_noThrow() throws SwiftJavaErrorException {
long result = MySwiftLibrary.globalThrowingReturn(false);
assertEquals(42, result);
}

@Test
void call_globalThrowingReturn_throws() {
assertThrows(SwiftJavaErrorException.class, () -> {
MySwiftLibrary.globalThrowingReturn(true);
});
}

@Test
void call_globalThrowingString_noThrow() throws SwiftJavaErrorException {
String result = MySwiftLibrary.globalThrowingString(false);
assertEquals("Hello from throwing Swift!", result);
}

@Test
void call_globalThrowingString_throws() {
assertThrows(SwiftJavaErrorException.class, () -> {
MySwiftLibrary.globalThrowingString(true);
});
}

@Test
void call_globalThrowingString_throws_checkMessage() {
SwiftJavaErrorException error = assertThrows(SwiftJavaErrorException.class, () -> {
MySwiftLibrary.globalThrowingString(true);
});
assertEquals(
"org.swift.swiftkit.ffm.generated.SwiftJavaErrorException: SwiftExampleError(message: \"expected error in globalThrowingString\")",
error.toString()
);
}

}
10 changes: 10 additions & 0 deletions Sources/CodePrinting/CodePrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public struct CodePrinter {
print("}", .sloc, function: function, file: file, line: line)
}

public mutating func printIfBlock(
_ condition: Any,
function: String = #function,
file: String = #fileID,
line: UInt = #line,
body: (inout CodePrinter) throws -> Void
) rethrows {
try printBraceBlock("if (\(condition))", function: function, file: file, line: line, body: body)
}

public mutating func printParts(
_ parts: String...,
terminator: PrinterTerminator = .newLine,
Expand Down
Loading
Loading