Skip to content

Commit

Permalink
Make static functions throw, like instance functions. (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Mar 13, 2024
1 parent ccf98af commit acd4048
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 158 deletions.
4 changes: 2 additions & 2 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ public init<Composable: ComposableImpl>(
{
w.write("return ");
}
w.write("try! _%.%Impl(%)\n",
w.write("try _%.%Impl(%)\n",
statics.swift_type_name(),
func_name,
bind<write_implementation_args>(method));
Expand Down Expand Up @@ -2293,7 +2293,7 @@ public init<Composable: ComposableImpl>(
}

write_documentation_comment(w, type, method.def.Name());
w.write("%func %(%)% {\n",
w.write("%func %(%) throws% {\n",
modifier_for(type, statics, method),
get_swift_name(method),
bind<write_function_params>(method, write_type_params::swift_allow_implicit_unwrap),
Expand Down
8 changes: 4 additions & 4 deletions tests/test_app/AggregationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ class AggregationTests : XCTestCase {

public func testComposedTypesAsInput() throws {
let appDerived = AppDerived()
StaticClass.takeBase(appDerived)
try StaticClass.takeBase(appDerived)
XCTAssertEqual(appDerived.count, 1, "derive from base count not expected")

let appDerived2 = AppDerived2()

StaticClass.takeBase(appDerived2)
try StaticClass.takeBase(appDerived2)
XCTAssertEqual(appDerived2.count, 1, "derived from derived count not expected")

let appDerived3 = AppDerived3()
StaticClass.takeBase(appDerived3)
try StaticClass.takeBase(appDerived3)
XCTAssertEqual(appDerived3.count, 1, "derived from multiple interfaces count not expected")
XCTAssertEqual(appDerived3.beforeCount, 1, "before count not expected")
}
Expand Down Expand Up @@ -134,7 +134,7 @@ class AggregationTests : XCTestCase {

public func testAggregatedObjectUnwrappedAsAny() throws {
let derived = AppDerived()
Class.takeBaseAndGiveToCallbackAsObject(derived) { sender in
try Class.takeBaseAndGiveToCallbackAsObject(derived) { sender in
let base = sender as? AppDerived
XCTAssertNotNil(derived)
XCTAssertIdentical(base, derived)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_app/AsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import Foundation

class AsyncTests : XCTestCase {
public func testAwaitAlreadyCompleted() throws {
let asyncOperation = AsyncMethods.getCompletedAsync(42)!
let asyncOperation = try AsyncMethods.getCompletedAsync(42)!
XCTAssertEqual(asyncOperation.status, .completed)
let result = try asyncBlock(timeout: 1) { try await asyncOperation.get() }
XCTAssertEqual(result, 42)
}

public func testAwaitAlreadyFailed() throws {
let asyncOperation = AsyncMethods.getCompletedWithErrorAsync(E_LAYOUTCYCLE)!
let asyncOperation = try AsyncMethods.getCompletedWithErrorAsync(E_LAYOUTCYCLE)!
XCTAssertEqual(asyncOperation.status, .error)
do {
_ = try asyncBlock(timeout: 1) { try await asyncOperation.get() }
Expand All @@ -23,15 +23,15 @@ class AsyncTests : XCTestCase {
}

public func testAwaitCompletionWithSuspension() throws {
let asyncOperation = AsyncMethods.getPendingAsync()!
let asyncOperation = try AsyncMethods.getPendingAsync()!
runAfter(delay: 0.05) { try? asyncOperation.complete(42) }
let result = try asyncBlock(timeout: 1) { try await asyncOperation.get() }
XCTAssertEqual(asyncOperation.status, .completed)
XCTAssertEqual(result, 42)
}

public func testAwaitFailureWithSuspension() throws {
let asyncOperation = AsyncMethods.getPendingAsync()!
let asyncOperation = try AsyncMethods.getPendingAsync()!
runAfter(delay: 0.05) { try? asyncOperation.completeWithError(E_LAYOUTCYCLE) }
do {
_ = try asyncBlock(timeout: 1) { try await asyncOperation.get() }
Expand Down
4 changes: 2 additions & 2 deletions tests/test_app/BufferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class BufferTests : XCTestCase {
XCTAssertEqual(buffer.capacity, 10)
XCTAssertEqual(buffer.data.count, 10)
for (i, byte) in buffer.data.enumerated() {
XCTAssertEqual(byte, BufferTester.getDataFrom(buffer, UInt32(i)))
XCTAssertEqual(byte, try BufferTester.getDataFrom(buffer, UInt32(i)))
}
}

Expand All @@ -67,7 +67,7 @@ class BufferTests : XCTestCase {
XCTAssertEqual(swiftBuffer.data, winrtBuffer.data)

for (i, byte) in swiftBuffer.data.enumerated() {
XCTAssertEqual(byte, BufferTester.getDataFrom(swiftBuffer, UInt32(i)))
XCTAssertEqual(byte, try BufferTester.getDataFrom(swiftBuffer, UInt32(i)))
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions tests/test_app/CollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CollectionTests : XCTestCase {
public func testVector_asInput() throws {
let array = ["Hello", "Goodbye", "Goodnight"]

let result = CollectionTester.inVector(array.toVector())
let result = try CollectionTester.inVector(array.toVector())
XCTAssertEqual(result, "Hello")
}

Expand Down Expand Up @@ -46,14 +46,14 @@ class CollectionTests : XCTestCase {
let person = Person(firstName: "John", lastName: "Doe", age: 42)
let array:[Any?] = [person, "Goodbye", 1]

CollectionTester.getObjectAt(array.toVector(), 0) {
try CollectionTester.getObjectAt(array.toVector(), 0) {
XCTAssertEqual($0 as! Person, person)
}
}

public func testMap_asInput() {
public func testMap_asInput() throws {
let dictionary = ["A": "Alpha"]
let value = CollectionTester.inMap(dictionary.toMap())
let value = try CollectionTester.inMap(dictionary.toMap())
XCTAssertEqual(value, "Alpha")
}

Expand All @@ -75,7 +75,7 @@ class CollectionTests : XCTestCase {
XCTAssert(map.insert("A", "Aleph")) // Returns true if replacing
XCTAssert(map.hasKey("A"))
XCTAssertEqual(map.lookup("A"), "Aleph")
let value = CollectionTester.inMap(map)
let value = try CollectionTester.inMap(map)
XCTAssertEqual(value, "Aleph")
}
}
Expand All @@ -91,4 +91,3 @@ var collectionTests: [XCTestCaseEntry] = [
("testVectorObject_toCallback", CollectionTests.testVectorObject_toCallback),
])
]

10 changes: 5 additions & 5 deletions tests/test_app/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ class EventTests : XCTestCase {
static_count+=1
})

Simple.fireStaticEvent()
try Simple.fireStaticEvent()
XCTAssertEqual(static_count, 1)
Simple.fireStaticEvent()
try Simple.fireStaticEvent()
XCTAssertEqual(static_count, 2)

// dispose of the handlers and make sure we
// aren't getting more events
disposable.dispose()

Simple.fireStaticEvent()
try Simple.fireStaticEvent()
XCTAssertEqual(static_count, 2)

disposable.append(Simple.staticEvent.addHandler { (_,_) in
static_count+=1
})

Simple.fireStaticEvent()
try Simple.fireStaticEvent()
XCTAssertEqual(static_count, 3)
}

Expand Down Expand Up @@ -136,4 +136,4 @@ var eventTests: [XCTestCaseEntry] = [
("SwiftImplementedEventWithWinRTListener", EventTests.testSwiftImplementedEventWithWinRTListener),
("SwiftImplementedEventWithSwiftListener", EventTests.testSwiftImplementedEventWithSwiftListener),
])
]
]
50 changes: 25 additions & 25 deletions tests/test_app/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ class SwiftWinRTTests : XCTestCase {
XCTAssertEqual(classy.enumProperty, Fruit.pineapple, "fruit should be Pineapple")
}

public func testStaticMethods() {
Class.staticTest()
public func testStaticMethods() throws {
try Class.staticTest()

var result = Class.staticTestReturn()
var result = try Class.staticTestReturn()
print("result: ", result);
XCTAssertEqual(result, 42);

Expand All @@ -137,11 +137,11 @@ class SwiftWinRTTests : XCTestCase {
XCTAssertEqual(result, 18);

print("calling static class methods")
let output = StaticClass.inEnum(Signed.first)
let output = try StaticClass.inEnum(Signed.first)
print("result: ", output)
XCTAssertEqual(output, "First")

let fruit = StaticClass.enumProperty
let fruit = try StaticClass.enumProperty
print("result: ", fruit)
XCTAssertEqual(fruit, Fruit.orange, "expected Orange!")

Expand All @@ -150,7 +150,7 @@ class SwiftWinRTTests : XCTestCase {

let putNonBlittableStruct = NonBlittableStruct(first: "From", second: "Swift!", third: 1, fourth: "Yay!")

let putStructResult = StaticClass.inNonBlittableStruct(putNonBlittableStruct)
let putStructResult = try StaticClass.inNonBlittableStruct(putNonBlittableStruct)
print("result: ", putStructResult)
XCTAssertEqual(putStructResult, "FromSwift!Yay!")
}
Expand Down Expand Up @@ -316,7 +316,7 @@ class SwiftWinRTTests : XCTestCase {
public func testNonDefaultMethods() throws {
Class.staticPropertyFloat = 4.0
XCTAssertEqual(Class.staticPropertyFloat, 4.0)
XCTAssertEqual(Class.staticTestReturnFloat(), 42.24)
XCTAssertEqual(try Class.staticTestReturnFloat(), 42.24)
let classy = Class()
classy.method()
}
Expand Down Expand Up @@ -392,24 +392,24 @@ class SwiftWinRTTests : XCTestCase {
_ = try classy.inString("\u{E909}")
}

public func testNullValues() {
XCTAssertTrue(NullValues.isObjectNull(nil))
XCTAssertTrue(NullValues.isInterfaceNull(nil))
XCTAssertTrue(NullValues.isGenericInterfaceNull(nil))
XCTAssertTrue(NullValues.isClassNull(nil))
XCTAssertTrue(NullValues.isDelegateNull(nil))

XCTAssertFalse(NullValues.isObjectNull(NoopClosable()))
XCTAssertFalse(NullValues.isInterfaceNull(NoopClosable()))
XCTAssertFalse(NullValues.isGenericInterfaceNull([""].toVector()))
XCTAssertFalse(NullValues.isClassNull(NoopClosable()))
XCTAssertFalse(NullValues.isDelegateNull({}))

XCTAssertNil(NullValues.getNullObject())
XCTAssertNil(NullValues.getNullInterface())
XCTAssertNil(NullValues.getNullGenericInterface())
XCTAssertNil(NullValues.getNullClass())
XCTAssertNil(NullValues.getNullDelegate())
public func testNullValues() throws {
XCTAssertTrue(try NullValues.isObjectNull(nil))
XCTAssertTrue(try NullValues.isInterfaceNull(nil))
XCTAssertTrue(try NullValues.isGenericInterfaceNull(nil))
XCTAssertTrue(try NullValues.isClassNull(nil))
XCTAssertTrue(try NullValues.isDelegateNull(nil))

XCTAssertFalse(try NullValues.isObjectNull(NoopClosable()))
XCTAssertFalse(try NullValues.isInterfaceNull(NoopClosable()))
XCTAssertFalse(try NullValues.isGenericInterfaceNull([""].toVector()))
XCTAssertFalse(try NullValues.isClassNull(NoopClosable()))
XCTAssertFalse(try NullValues.isDelegateNull({}))

XCTAssertNil(try NullValues.getNullObject())
XCTAssertNil(try NullValues.getNullInterface())
XCTAssertNil(try NullValues.getNullGenericInterface())
XCTAssertNil(try NullValues.getNullClass())
XCTAssertNil(try NullValues.getNullDelegate())
}

public func testCasing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public final class Uri : WinRTClass, IStringable {
}
private static let _IUriEscapeStatics: __ABI_Windows_Foundation.IUriEscapeStatics = try! RoGetActivationFactory("Windows.Foundation.Uri")
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.uri.unescapecomponent)
public static func unescapeComponent(_ toUnescape: String) -> String {
return try! _IUriEscapeStatics.UnescapeComponentImpl(toUnescape)
public static func unescapeComponent(_ toUnescape: String) throws -> String {
return try _IUriEscapeStatics.UnescapeComponentImpl(toUnescape)
}

/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.uri.escapecomponent)
public static func escapeComponent(_ toEscape: String) -> String {
return try! _IUriEscapeStatics.EscapeComponentImpl(toEscape)
public static func escapeComponent(_ toEscape: String) throws -> String {
return try _IUriEscapeStatics.EscapeComponentImpl(toEscape)
}

private static let _IUriRuntimeClassFactory: __ABI_Windows_Foundation.IUriRuntimeClassFactory = try! RoGetActivationFactory("Windows.Foundation.Uri")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public final class Buffer : WinRTClass, IBufferByteAccess, IBuffer {

private static let _IBufferStatics: __ABI_Windows_Storage_Streams.IBufferStatics = try! RoGetActivationFactory("Windows.Storage.Streams.Buffer")
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.storage.streams.buffer.createcopyfrommemorybuffer)
public static func createCopyFromMemoryBuffer(_ input: test_component.AnyIMemoryBuffer!) -> Buffer! {
return try! _IBufferStatics.CreateCopyFromMemoryBufferImpl(input)
public static func createCopyFromMemoryBuffer(_ input: test_component.AnyIMemoryBuffer!) throws -> Buffer! {
return try _IBufferStatics.CreateCopyFromMemoryBufferImpl(input)
}

/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.storage.streams.buffer.creatememorybufferoveribuffer)
public static func createMemoryBufferOverIBuffer(_ input: AnyIBuffer!) -> test_component.MemoryBuffer! {
return try! _IBufferStatics.CreateMemoryBufferOverIBufferImpl(input)
public static func createMemoryBufferOverIBuffer(_ input: AnyIBuffer!) throws -> test_component.MemoryBuffer! {
return try _IBufferStatics.CreateMemoryBufferOverIBufferImpl(input)
}

private lazy var _IBufferByteAccess: __ABI_.IBufferByteAccess! = getInterfaceForCaching()
Expand Down
Loading

0 comments on commit acd4048

Please sign in to comment.