Skip to content

Commit

Permalink
Added a mock target and updated README (#8)
Browse files Browse the repository at this point in the history
* Added a mock target and updated README
  • Loading branch information
Henryforce authored Jan 28, 2024
1 parent c0112a9 commit f12d4b3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:
branches: [ main ]
env:
DEVELOPER_DIR: /Applications/Xcode_13.2.1.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_14.2.app/Contents/Developer

jobs:
build:
Expand Down
10 changes: 9 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import PackageDescription

let package = Package(
name: "AsyncWebSocketClient",
platforms: [.iOS(.v13), .macOS(.v10_15), .watchOS(.v6), .tvOS(.v13)],
platforms: [.iOS(.v13), .macOS(.v12), .watchOS(.v6), .tvOS(.v13)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "AsyncWebSocketClient",
targets: ["AsyncWebSocketClient"]),
.library(
name: "AsyncWebSocketClientMocks",
targets: ["AsyncWebSocketClientMocks"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -24,6 +27,11 @@ let package = Package(
dependencies: [
"AsyncTimeSequences",
]),
.target(
name: "AsyncWebSocketClientMocks",
dependencies: [
"AsyncWebSocketClient",
]),
.testTarget(
name: "AsyncWebSocketClientTests",
dependencies: [
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
(WORK IN PROGRESS)

# AsyncWebSocketClient

![badge-platforms][] [![badge-spm][]][spm]
Expand Down Expand Up @@ -38,7 +36,7 @@ try await client.sendJSONData(objectToEncode)

## How to test

(Coming soon)
AsyncWebSocketClient conforms to AsyncWebSocketClientProtocol and it is recommended to use dependency injection to make testing easier. For convenience, this library also includes a mock target `AsyncWebSocketClientMocks` which you can use if you just need a simple mock. You could optionally add your own mock class conforming to AsyncWebSocketClientProtocol.

## Installation

Expand Down
51 changes: 51 additions & 0 deletions Sources/AsyncWebSocketClientMocks/AsyncWebSocketClientMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// AsyncWebSocketClientMock.swift
//
//
// Created by Henry Javier Serrano Echeverria on 27/1/24.
//

import AsyncWebSocketClient
import Foundation

open class AsyncWebSocketClientMock: AsyncWebSocketClientProtocol {
public var connectWasCalledCount = 0
public func connect() async throws {
connectWasCalledCount += 1
}

public var disconnectWasCalledCount = 0
public func disconnect() async throws {
disconnectWasCalledCount += 1
}

public var sendWasCalledStack = [AsyncWebSocketData]()
public func send(_ data: AsyncWebSocketData) async throws {
sendWasCalledStack.append(data)
}

@Published public var streamSocketEvent = AsyncWebSocketEvent.socketOpened
public var listenStreamWasCalledCount = 0
public func listenStream() async -> AsyncStream<AsyncWebSocketEvent> {
listenStreamWasCalledCount += 1

let stream = $streamSocketEvent.values
return AsyncStream { continuation in
let cancellableTask = Task {
for await event in stream {
try Task.checkCancellation()
continuation.yield(event)
}
}
// Cancel the task that is listening to the stream socket event.
continuation.onTermination = { _ in
cancellableTask.cancel()
}
}
}

public init() {

}

}

0 comments on commit f12d4b3

Please sign in to comment.