-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Note that XCTAssertEqual doesn’t allow you to write `await` inside its arguments, hence the indirection to get the result of a couple of `async let`s. Hopefully we’ll be able to migrate to Swift Testing at some point, which will resolve this; see #21. I’ve also implemented MessageSubscription by wrapping Subscription.
- Loading branch information
1 parent
783865c
commit 202f70a
Showing
7 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@testable import AblyChat | ||
import AsyncAlgorithms | ||
import XCTest | ||
|
||
class SubscriptionTests: XCTestCase { | ||
func testWithMockAsyncSequence() async { | ||
let subscription = Subscription(mockAsyncSequence: ["First", "Second"].async) | ||
|
||
async let emittedElements = Array(subscription.prefix(2)) | ||
|
||
let awaitedEmittedElements = await emittedElements | ||
XCTAssertEqual(awaitedEmittedElements, ["First", "Second"]) | ||
} | ||
|
||
func testEmit() async { | ||
let subscription = Subscription<String>(bufferingPolicy: .unbounded) | ||
|
||
async let emittedElements = Array(subscription.prefix(2)) | ||
|
||
subscription.emit("First") | ||
subscription.emit("Second") | ||
|
||
let awaitedEmittedElements = await emittedElements | ||
XCTAssertEqual(awaitedEmittedElements, ["First", "Second"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
@testable import AblyChat | ||
import AsyncAlgorithms | ||
import XCTest | ||
|
||
private final class MockPaginatedResult<T>: PaginatedResult { | ||
var items: [T] { fatalError("Not implemented") } | ||
|
||
var hasNext: Bool { fatalError("Not implemented") } | ||
|
||
var isLast: Bool { fatalError("Not implemented") } | ||
|
||
var next: (any AblyChat.PaginatedResult<T>)? { fatalError("Not implemented") } | ||
|
||
var first: any AblyChat.PaginatedResult<T> { fatalError("Not implemented") } | ||
|
||
var current: any AblyChat.PaginatedResult<T> { fatalError("Not implemented") } | ||
|
||
init() {} | ||
} | ||
|
||
class MessageSubscriptionTests: XCTestCase { | ||
let messages = ["First", "Second"].map { | ||
Message(timeserial: "", clientID: "", roomID: "", text: $0, createdAt: .now, metadata: [:], headers: [:]) | ||
} | ||
|
||
func testWithMockAsyncSequence() async { | ||
let subscription = MessageSubscription(mockAsyncSequence: messages.async) { _ in fatalError("Not implemented") } | ||
|
||
async let emittedElements = Array(subscription.prefix(2)) | ||
|
||
let awaitedEmittedElements = await emittedElements | ||
XCTAssertEqual(awaitedEmittedElements.map(\.text), ["First", "Second"]) | ||
} | ||
|
||
func testEmit() async { | ||
let subscription = MessageSubscription(bufferingPolicy: .unbounded) | ||
|
||
async let emittedElements = Array(subscription.prefix(2)) | ||
|
||
subscription.emit(messages[0]) | ||
subscription.emit(messages[1]) | ||
|
||
let awaitedEmittedElements = await emittedElements | ||
XCTAssertEqual(awaitedEmittedElements.map(\.text), ["First", "Second"]) | ||
} | ||
|
||
func funcTestMockGetPreviousMessages() async throws { | ||
let mockPaginatedResult = MockPaginatedResult<Message>() | ||
let subscription = MessageSubscription(mockAsyncSequence: [].async) { _ in mockPaginatedResult } | ||
|
||
let result = try await subscription.getPreviousMessages(params: .init()) | ||
XCTAssertIdentical(result, mockPaginatedResult) | ||
} | ||
} |