Skip to content

Commit b23d88b

Browse files
authored
Merge pull request #63 from NordicSemiconductor/develop
Version 0.13.0
2 parents e17e5a6 + 39e4d1d commit b23d88b

File tree

46 files changed

+2071
-677
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2071
-677
lines changed

CoreBluetoothMock.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'CoreBluetoothMock'
3-
s.version = '0.12.1'
3+
s.version = '0.13.0'
44
s.summary = 'Mocking library for CoreBluetooth.'
55

66
s.description = <<-DESC
@@ -18,12 +18,12 @@ device and test the app on simulator.
1818
s.osx.deployment_target = '10.13'
1919
s.tvos.deployment_target = '9.0'
2020
s.watchos.deployment_target = '2.0'
21-
s.swift_versions = ['4.2', '5.0', '5.1', '5.2', '5.3', '5.4']
21+
s.swift_versions = ['4.2', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5']
2222
s.pod_target_xcconfig = { 'BUILD_LIBRARY_FOR_DISTRIBUTION' => 'YES' }
2323

2424
s.source_files = 'CoreBluetoothMock/Classes/**/*'
2525

2626
# Regarding the lines below see: https://stackoverflow.com/a/63955114/2115352
27-
s.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
28-
s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
27+
# s.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
28+
# s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
2929
end

CoreBluetoothMock/Classes/CBMAttribute.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.

CoreBluetoothMock/Classes/CBMServiceTypes.swift renamed to CoreBluetoothMock/Classes/CBMAttributes.swift

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,32 @@
3030

3131
import CoreBluetooth
3232

33+
open class CBMAttribute: NSObject {
34+
35+
/// The Bluetooth UUID of the attribute.
36+
var uuid: CBMUUID {
37+
fatalError()
38+
}
39+
}
40+
3341
open class CBMService: CBMAttribute {
3442
internal let identifier: UUID
3543
private let _uuid: CBMUUID
3644

3745
internal var _includedServices: [CBMService]?
3846
internal var _characteristics: [CBMCharacteristic]?
3947

48+
#if swift(>=5.5)
49+
/// A back-pointer to the peripheral this service belongs to.
50+
open internal(set) weak var peripheral: CBMPeripheral?
51+
#else
4052
/// A back-pointer to the peripheral this service belongs to.
4153
open internal(set) unowned var peripheral: CBMPeripheral
54+
#endif
4255

4356
/// The type of the service (primary or secondary).
4457
open fileprivate(set) var isPrimary: Bool
4558

46-
/// The Bluetooth UUID of the attribute.
4759
open override var uuid: CBMUUID {
4860
return _uuid
4961
}
@@ -106,30 +118,19 @@ internal class CBMServiceNative: CBMService {
106118
}
107119

108120
open class CBMServiceMock: CBMService {
109-
110-
open override var includedServices: [CBMService]? {
111-
set { _includedServices = newValue }
112-
get { return _includedServices }
113-
}
114-
115-
open override var characteristics: [CBMCharacteristic]? {
116-
set { _characteristics = newValue }
117-
get { return _characteristics }
118-
}
119121

120122
/// Returns a service, initialized with a service type and UUID.
121123
/// - Parameters:
122124
/// - uuid: The Bluetooth UUID of the service.
123125
/// - isPrimary: The type of the service (primary or secondary).
126+
/// - includedServices: Optional list of included services.
124127
/// - characteristics: Optional list of characteristics.
125128
public init(type uuid: CBMUUID, primary isPrimary: Bool,
129+
includedService: CBMServiceMock...,
126130
characteristics: CBMCharacteristicMock...) {
127131
super.init(type: uuid, primary: isPrimary)
128-
self.characteristics = characteristics
129-
}
130-
131-
open func contains(_ characteristic: CBMCharacteristicMock) -> Bool {
132-
return _characteristics?.contains(characteristic) ?? false
132+
self._includedServices = includedService
133+
self._characteristics = characteristics
133134
}
134135

135136
open override func isEqual(_ object: Any?) -> Bool {
@@ -146,13 +147,23 @@ open class CBMCharacteristic: CBMAttribute {
146147

147148
internal var _descriptors: [CBMDescriptor]?
148149

149-
/// The Bluetooth UUID of the attribute.
150150
open override var uuid: CBMUUID {
151151
return _uuid
152152
}
153153

154+
#if swift(>=5.5)
155+
/// A back-pointer to the service this characteristic belongs to.
156+
open internal(set) weak var service: CBMService?
157+
#else
154158
/// A back-pointer to the service this characteristic belongs to.
155-
open internal(set) var service: CBMService
159+
open internal(set) unowned var service: CBMService
160+
#endif
161+
162+
/// Casts the sometimes weak, sometimes unowned service to
163+
/// always optional object.
164+
internal var optionalService: CBMService? {
165+
return service
166+
}
156167

157168
/// The properties of the characteristic.
158169
public let properties: CBMCharacteristicProperties
@@ -234,10 +245,6 @@ open class CBMCharacteristicMock: CBMCharacteristic {
234245
self.descriptors = descriptors
235246
}
236247

237-
open func contains(_ descriptor: CBMDescriptor) -> Bool {
238-
return _descriptors?.contains(descriptor) ?? false
239-
}
240-
241248
open override func isEqual(_ object: Any?) -> Bool {
242249
if let other = object as? CBMCharacteristicMock {
243250
return identifier == other.identifier
@@ -250,13 +257,23 @@ open class CBMDescriptor: CBMAttribute {
250257
internal let identifier: UUID
251258
private let _uuid: CBMUUID
252259

253-
/// The Bluetooth UUID of the attribute.
254260
open override var uuid: CBMUUID {
255261
return _uuid
256262
}
257263

264+
#if swift(>=5.5)
265+
/// A back-pointer to the characteristic this descriptor belongs to.
266+
open internal(set) weak var characteristic: CBMCharacteristic?
267+
#else
258268
/// A back-pointer to the characteristic this descriptor belongs to.
259-
open internal(set) var characteristic: CBMCharacteristic
269+
open internal(set) unowned var characteristic: CBMCharacteristic
270+
#endif
271+
272+
/// Casts the sometimes weak, sometimes unowned characteristic to
273+
/// always optional object.
274+
internal var optionalCharacteristic: CBMCharacteristic? {
275+
return characteristic
276+
}
260277

261278
/// The value of the descriptor.
262279
open internal(set) var value: Any?
@@ -321,6 +338,42 @@ open class CBMClientCharacteristicConfigurationDescriptorMock: CBMDescriptorMock
321338

322339
public typealias CBMCCCDescriptorMock = CBMClientCharacteristicConfigurationDescriptorMock
323340

341+
// MARK: - Utilities
342+
343+
internal extension Array where Element == CBMServiceMock {
344+
345+
func find(mockOf service: CBMService) -> CBMServiceMock? {
346+
return first { $0.identifier == service.identifier }
347+
}
348+
349+
func find(mockOf characteristic: CBMCharacteristic) -> CBMCharacteristicMock? {
350+
guard let service = characteristic.optionalService,
351+
let mockService = find(mockOf: service),
352+
let mockCharacteristic = mockService.characteristics?.first(where: {
353+
$0.identifier == characteristic.identifier
354+
}) else {
355+
return nil
356+
}
357+
return mockCharacteristic as? CBMCharacteristicMock
358+
}
359+
360+
func find(mockOf descriptor: CBMDescriptor) -> CBMDescriptorMock? {
361+
guard let characteristic = descriptor.optionalCharacteristic,
362+
let service = characteristic.optionalService,
363+
let mockService = find(mockOf: service),
364+
let mockCharacteristic = mockService.characteristics?.first(where: {
365+
$0.identifier == characteristic.identifier
366+
}),
367+
let mockDescriptor = mockCharacteristic.descriptors?.first(where: {
368+
$0.identifier == descriptor.identifier
369+
}) else {
370+
return nil
371+
}
372+
return mockDescriptor as? CBMDescriptorMock
373+
}
374+
375+
}
376+
324377
// MARK: - Mocking uninitialized objects
325378

326379
fileprivate let uninitializedPeriperheral = CBMPeripheralUninitialized()

0 commit comments

Comments
 (0)