Skip to content

Commit

Permalink
Merge pull request #5 from Kuniwak/test-doubles
Browse files Browse the repository at this point in the history
Add spy/stubs
  • Loading branch information
Kuniwak authored Jun 12, 2024
2 parents 4eab4c6 + 394dde0 commit ad7d755
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BLEModelStub"
BuildableName = "BLEModelStub"
BlueprintName = "BLEModelStub"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Kuniwak/core-bluetooth-testable.git",
"state" : {
"revision" : "5de9007a619459bce64b83e28a6315bad2dfcf4f",
"version" : "3.0.0"
"revision" : "d327cead5b8bfa2326321ce81c047735b7e635ae",
"version" : "4.0.0"
}
},
{
Expand Down
84 changes: 57 additions & 27 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ let package = Package(
name: "BLEModel",
targets: ["BLEModel"]
),
.library(
name: "BLEModelStub",
targets: ["BLEModelStub"]
),
],
dependencies: [
.package(url: "https://github.com/Kuniwak/swift-logger.git", .upToNextMajor(from: "1.1.0")),
.package(url: "https://github.com/Kuniwak/core-bluetooth-testable.git", .upToNextMajor(from: "3.0.0")),
.package(url: "https://github.com/Kuniwak/core-bluetooth-testable.git", .upToNextMajor(from: "4.0.0")),
.package(url: "https://github.com/Kuniwak/swift-ble-assigned-numbers.git", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/Kuniwak/MirrorDiffKit.git", .upToNextMajor(from: "6.0.0")),
.package(url: "https://github.com/cezheng/Fuzi.git", .upToNextMajor(from: "3.1.3")),
Expand All @@ -53,56 +57,66 @@ let package = Package(
.testTarget(
name: "BLEInternalTests",
dependencies: [
"BLEInternal",
.product(name: "Logger", package: "swift-logger"),
.product(name: "BLEAssignedNumbers", package: "swift-ble-assigned-numbers"),
.bleInternal,
.logger,
.bleAssignedNumbers,
]
),
.target(
name: "BLEMacro",
dependencies: [
"BLEInternal",
"Fuzi",
.bleInternal,
.fuzi,
]
),
.target(
name: "BLECommand",
dependencies: [
"BLEInternal",
.bleInternal,
]
),
.target(
name: "BLEModel",
dependencies: [
"BLEInternal",
.product(name: "Logger", package: "swift-logger"),
.product(name: "CoreBluetoothTestable", package: "core-bluetooth-testable")
.bleInternal,
.logger,
.coreBluetoothTestable,
]
),
.target(
name: "BLEModelStub",
dependencies: [
.bleModel,
.bleInternal,
.logger,
.coreBluetoothTestable,
.coreBluetoothStub,
]
),
.target(
name: "BLEMacroCompiler",
dependencies: [
"BLEInternal",
"BLEMacro",
"BLECommand",
.product(name: "BLEAssignedNumbers", package: "swift-ble-assigned-numbers"),
.bleInternal,
.bleMacro,
.bleCommand,
.bleAssignedNumbers,
]
),
.target(
name: "BLEInterpreter",
dependencies: [
"BLEInternal",
"BLECommand",
.product(name: "CoreBluetoothTestable", package: "core-bluetooth-testable")
.bleInternal,
.bleCommand,
.coreBluetoothTestable,
]
),
.testTarget(
name: "BLEMacroTests",
dependencies: [
"BLEInternal",
"BLEMacro",
"MirrorDiffKit",
.product(name: "BLEAssignedNumbers", package: "swift-ble-assigned-numbers"),
.bleInternal,
.bleMacro,
.mirrorDiffKit,
.bleAssignedNumbers,
],
resources: [
.copy("Fixtures"),
Expand All @@ -111,13 +125,29 @@ let package = Package(
.target(
name: "BLEMacroEasy",
dependencies: [
"BLEInternal",
"BLEMacro",
"BLEMacroCompiler",
"BLECommand",
"BLEInterpreter",
.product(name: "CoreBluetoothTestable", package: "core-bluetooth-testable")
.bleInternal,
.bleMacro,
.bleMacroCompiler,
.bleCommand,
.bleInterpreter,
.coreBluetoothTestable,
]
)
]
)


private extension Target.Dependency {
static let bleInternal: Self = "BLEInternal"
static let bleMacro: Self = "BLEMacro"
static let bleMacroCompiler: Self = "BLEMacroCompiler"
static let bleCommand: Self = "BLECommand"
static let bleInterpreter: Self = "BLEInterpreter"
static let bleModel: Self = "BLEModel"
static let fuzi: Self = "Fuzi"
static let mirrorDiffKit: Self = "MirrorDiffKit"
static let logger: Self = .product(name: "Logger", package: "swift-logger")
static let coreBluetoothTestable: Self = .product(name: "CoreBluetoothTestable", package: "core-bluetooth-testable")
static let coreBluetoothStub: Self = .product(name: "CoreBluetoothStub", package: "core-bluetooth-testable")
static let bleAssignedNumbers: Self = .product(name: "BLEAssignedNumbers", package: "swift-ble-assigned-numbers")
}
37 changes: 37 additions & 0 deletions Sources/BLEModelStub/SpyPeripheralsDiscoveryModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Combine
import BLEModel


public class SpyPeripheralsDiscoveryModel: PeripheralsDiscoveryModelProtocol {
public var state: PeripheralsDiscoveryModelState { inherited.state }
public var stateDidUpdate: AnyPublisher<PeripheralsDiscoveryModelState, Never> {
inherited.stateDidUpdate
}


public var inherited: any PeripheralsDiscoveryModelProtocol
public private(set) var callArgs = [CallArg]()


public init(inheriting inherited: any PeripheralsDiscoveryModelProtocol = StubPeripheralsDiscoveryModel()) {
self.inherited = inherited
}


public func scan() {
callArgs.append(.scan)
inherited.scan()
}


public func stopScan() {
callArgs.append(.stopScan)
inherited.stopScan()
}


public enum CallArg: Equatable {
case scan
case stopScan
}
}
33 changes: 33 additions & 0 deletions Sources/BLEModelStub/SpyPeripheralsNewDiscoveryModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Combine
import BLEModel


public class SpyPeripheralsNewDiscoveryModel: PeripheralsNewDiscoveryModelProtocol {
public var didDiscover: AnyPublisher<PeripheralDiscoveryEntry, Never> { inherited.didDiscover }

public var inherited: any PeripheralsNewDiscoveryModelProtocol
public private(set) var callArgs = [CallArg]()


public init(inheriting inherited: any PeripheralsNewDiscoveryModelProtocol = StubPeripheralsNewDiscoveryModel()) {
self.inherited = inherited
}


public func scan() {
callArgs.append(.scan)
inherited.scan()
}


public func stopScan() {
callArgs.append(.stopScan)
inherited.stopScan()
}


public enum CallArg: Equatable {
case scan
case stopScan
}
}
26 changes: 26 additions & 0 deletions Sources/BLEModelStub/StubPeripheralsDiscoveryModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Combine
import BLEModel


public class StubPeripheralsDiscoveryModel: PeripheralsDiscoveryModelProtocol {
public var state: PeripheralsDiscoveryModelState {
stateDidUpdateSubject.value
}
public let stateDidUpdate: AnyPublisher<PeripheralsDiscoveryModelState, Never>
public let stateDidUpdateSubject: CurrentValueSubject<PeripheralsDiscoveryModelState, Never>


public init(state: PeripheralsDiscoveryModelState = .notReady) {
let stateDidUpdateSubject = CurrentValueSubject<PeripheralsDiscoveryModelState, Never>(state)
self.stateDidUpdateSubject = stateDidUpdateSubject
self.stateDidUpdate = stateDidUpdateSubject.eraseToAnyPublisher()
}


public func scan() {
}


public func stopScan() {
}
}
23 changes: 23 additions & 0 deletions Sources/BLEModelStub/StubPeripheralsNewDiscoveryModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Combine
import BLEModel


public class StubPeripheralsNewDiscoveryModel: PeripheralsNewDiscoveryModelProtocol {
public let didDiscover: AnyPublisher<PeripheralDiscoveryEntry, Never>
public let didDiscoverSubject: PassthroughSubject<PeripheralDiscoveryEntry, Never>


public init() {
let didDiscoverSubject = PassthroughSubject<PeripheralDiscoveryEntry, Never>()
self.didDiscoverSubject = didDiscoverSubject
self.didDiscover = didDiscoverSubject.eraseToAnyPublisher()
}


public func scan() {
}


public func stopScan() {
}
}

0 comments on commit ad7d755

Please sign in to comment.