diff --git a/Tests/TestingUtils/XCTest.swift b/Tests/TestingUtils/XCTest.swift index 890b2110c..e3048f849 100644 --- a/Tests/TestingUtils/XCTest.swift +++ b/Tests/TestingUtils/XCTest.swift @@ -17,4 +17,17 @@ extension XCTest { errorHandler(error) } } + + public func XCTAssertNoThrowAsync( + _ expression: @autoclosure () async throws -> T, + _ message: @autoclosure () -> String = "", + file: StaticString = #filePath, + line: UInt = #line + ) async { + do { + _ = try await expression() + } catch { + XCTFail(message(), file: file, line: line) + } + } } diff --git a/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift b/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift index 1eca9b489..a7756b17b 100644 --- a/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift +++ b/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift @@ -38,16 +38,16 @@ class ControllerSessionStateMachineTests: XCTestCase { // XCTAssertEqual(namespacesToUpdate, updatedSession?.namespaces) // } - func testUpdateNamespacesErrorSessionNotFound() { - XCTAssertThrowsError(try sut.update(topic: "", namespaces: SessionNamespace.stubDictionary())) { error in + func testUpdateNamespacesErrorSessionNotFound() async { + await XCTAssertThrowsErrorAsync( try await sut.update(topic: "", namespaces: SessionNamespace.stubDictionary())) { error in XCTAssertTrue(error.isNoSessionMatchingTopicError) } } - func testUpdateNamespacesErrorSessionNotAcknowledged() { + func testUpdateNamespacesErrorSessionNotAcknowledged() async { let session = WCSession.stub(acknowledged: false) storageMock.setSession(session) - XCTAssertThrowsError(try sut.update(topic: session.topic, namespaces: SessionNamespace.stubDictionary())) { error in + await XCTAssertThrowsErrorAsync( try await sut.update(topic: session.topic, namespaces: SessionNamespace.stubDictionary())) { error in XCTAssertTrue(error.isSessionNotAcknowledgedError) } } @@ -60,55 +60,55 @@ class ControllerSessionStateMachineTests: XCTestCase { // } // } - func testUpdateNamespacesErrorCalledByNonController() { + func testUpdateNamespacesErrorCalledByNonController() async { let session = WCSession.stub(isSelfController: false) storageMock.setSession(session) - XCTAssertThrowsError(try sut.update(topic: session.topic, namespaces: SessionNamespace.stubDictionary())) { error in + await XCTAssertThrowsErrorAsync( try await sut.update(topic: session.topic, namespaces: SessionNamespace.stubDictionary())) { error in XCTAssertTrue(error.isUnauthorizedNonControllerCallError) } } // MARK: - Update Expiry - func testUpdateExpirySuccess() { + func testUpdateExpirySuccess() async { let tomorrow = TimeTraveler.dateByAdding(days: 1) let session = WCSession.stub(isSelfController: true, expiryDate: tomorrow) storageMock.setSession(session) let twoDays = 2*Time.day - XCTAssertNoThrow(try sut.extend(topic: session.topic, by: Int64(twoDays))) + await XCTAssertNoThrowAsync(try await sut.extend(topic: session.topic, by: Int64(twoDays))) let extendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}! XCTAssertEqual(extendedSession.expiryDate.timeIntervalSinceReferenceDate, TimeTraveler.dateByAdding(days: 2).timeIntervalSinceReferenceDate, accuracy: 1) } - func testUpdateExpirySessionNotSettled() { + func testUpdateExpirySessionNotSettled() async { let tomorrow = TimeTraveler.dateByAdding(days: 1) let session = WCSession.stub(isSelfController: false, expiryDate: tomorrow, acknowledged: false) storageMock.setSession(session) let twoDays = 2*Time.day - XCTAssertThrowsError(try sut.extend(topic: session.topic, by: Int64(twoDays))) + await XCTAssertThrowsErrorAsync(try await sut.extend(topic: session.topic, by: Int64(twoDays))) } - func testUpdateExpiryOnNonControllerClient() { + func testUpdateExpiryOnNonControllerClient() async { let tomorrow = TimeTraveler.dateByAdding(days: 1) let session = WCSession.stub(isSelfController: false, expiryDate: tomorrow) storageMock.setSession(session) let twoDays = 2*Time.day - XCTAssertThrowsError(try sut.extend(topic: session.topic, by: Int64(twoDays))) + await XCTAssertThrowsErrorAsync( try await sut.extend(topic: session.topic, by: Int64(twoDays))) } - func testUpdateExpiryTtlTooHigh() { + func testUpdateExpiryTtlTooHigh() async { let tomorrow = TimeTraveler.dateByAdding(days: 1) let session = WCSession.stub(isSelfController: true, expiryDate: tomorrow) storageMock.setSession(session) let tenDays = 10*Time.day - XCTAssertThrowsError(try sut.extend(topic: session.topic, by: Int64(tenDays))) + await XCTAssertThrowsErrorAsync( try await sut.extend(topic: session.topic, by: Int64(tenDays))) } - func testUpdateExpiryTtlTooLow() { + func testUpdateExpiryTtlTooLow() async { let dayAfterTommorow = TimeTraveler.dateByAdding(days: 2) let session = WCSession.stub(isSelfController: true, expiryDate: dayAfterTommorow) storageMock.setSession(session) let oneDay = Int64(1*Time.day) - XCTAssertThrowsError(try sut.extend(topic: session.topic, by: oneDay)) + await XCTAssertThrowsErrorAsync( try await sut.extend(topic: session.topic, by: oneDay)) } } diff --git a/Tests/WalletConnectTests/PairEngineTests.swift b/Tests/WalletConnectTests/PairEngineTests.swift index e819141ea..d22f98a47 100644 --- a/Tests/WalletConnectTests/PairEngineTests.swift +++ b/Tests/WalletConnectTests/PairEngineTests.swift @@ -42,9 +42,8 @@ final class PairEngineTests: XCTestCase { func testPairMultipleTimesOnSameURIThrows() async { let uri = WalletConnectURI.stub() for i in 1...10 { - usleep(100) if i == 1 { - XCTAssertNoThrow(Task{try await engine.pair(uri)}) + await XCTAssertNoThrowAsync(try await engine.pair(uri)) } else { await XCTAssertThrowsErrorAsync(try await engine.pair(uri)) }