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
class BluetoothManager: NSObject { | |
private let centralManager: CentralManager | |
private let uuids: [CBUUID] = [.init(string: "EXAMPLE-UUID-1"), | |
.init(string: "EXAMPLE-UUID-2")] | |
init(centralManager: CentralManager) { | |
self.centralManager = centralManager | |
super.init() | |
centralManager.delegate = self |
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
extension BluetoothManager: CentralManagerDelegate { | |
func centralManagerDidUpdateState(_ central: CentralManager) { | |
// Make sure the central manager is powered on. | |
guard central.state == .poweredOn else { return } | |
// We only scan for the listed UUIDs. | |
central.scanForPeripherals(withServices: uuids, options: nil) | |
} | |
func centralManager(_ central: CentralManager, | |
didDiscover peripheral: Peripheral, |
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
/// Facade for the CBCentralManagerDelegate. | |
protocol CentralManagerDelegate: CBCentralManagerDelegate { | |
/// Function called when the central manager updates its state. | |
func centralManagerDidUpdateState(_ central: CentralManager) | |
/// Function called when the central manager discovers peripherals. | |
func centralManager(_ central: CentralManager, | |
didDiscover peripheral: Peripheral, | |
advertisementData: [String : Any], | |
rssi RSSI: NSNumber) |
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
/// A Facade for the CBCentralManager. | |
protocol CentralManager { | |
/// The delegate of the central manager. | |
var delegate: CBCentralManagerDelegate? { get set } | |
/// The state of the central manager. | |
var state: CBManagerState { get } | |
/// Call this function to scan for bluetooth peripherals. | |
func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?) | |
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
/// A Facade for the CBCentralManager. | |
protocol CentralManager { | |
/// The state of the central manager. | |
var state: CBManagerState { get } | |
/// Call this function to scan for bluetooth peripherals. | |
func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String : Any]?) | |
/// Connect to a peripheral. | |
func connect(_ peripheral: CBPeripheral, options: [String : Any]?) |
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
import CoreBluetooth | |
class BluetoothManager: NSObject { | |
private let centralManager = CBCentralManager() | |
private let uuids: [CBUUID] = [.init(string: "EXAMPLE-UUID-1"), | |
.init(string: "EXAMPLE-UUID-2")] | |
override init() { | |
super.init() |
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
class PresenterMock: Mock<PresenterMock.Functions>, Presenter { | |
enum Functions: FunctionName { | |
case present = "present(title:description:)" | |
} | |
func present(title: String, description: String) { log(parameters: (title, description)) } | |
} | |
/* In the ContentHandlerTests */ |
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
protocol Presenter { | |
/// Present the given content to the user. | |
func present(title: String, description: String) | |
} | |
class ContentHandler { | |
/// The presenter used to present content. | |
private let presenter: Presenter | |
init(presenter: Presenter) { |
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
func testCheckAnswerTrue() { | |
b.validateAnswerResponse = true | |
a.checkAnswer() | |
XCTAssertTrue(a.hasAnswer) | |
// The `validateAnswer()` function should be called exactly once. | |
let call = b.calls(with: .validateAnswer) | |
XCTAssertNotNil(call) | |
XCTAssertEqual(call?.numberOfCalls, 1) | |
} |
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
class BMock: Mock<BMock.Functions>, B { | |
enum Functions: FunctionName { | |
/// The `FunctionName` corresponding to the `validateAnswer()` function. | |
case validateAnswer = "validateAnswer()" | |
} | |
/// The mocked response being returned by the `validateAnswer()` function. | |
var validateAnswerResponse = true | |
func validateAnswer() -> Bool { log(return: validateAnswerResponse) } |
NewerOlder