Skip to content

Commit

Permalink
fix tests, add XCTAssertNoThrowAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed May 18, 2022
1 parent cc886f0 commit ee955b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
13 changes: 13 additions & 0 deletions Tests/TestingUtils/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ extension XCTest {
errorHandler(error)
}
}

public func XCTAssertNoThrowAsync<T: Sendable>(
_ 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)
}
}
}
32 changes: 16 additions & 16 deletions Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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))
}
}
3 changes: 1 addition & 2 deletions Tests/WalletConnectTests/PairEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit ee955b4

Please sign in to comment.