Skip to content

Commit

Permalink
Updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 14, 2024
1 parent c4fe9e8 commit f860151
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 100 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ var package = Package(
package: "Bluetooth",
condition: .when(platforms: [.macOS, .linux])
)
]
],
swiftSettings: [.swiftLanguageMode(.v5)]
)
]
)
Expand Down
54 changes: 27 additions & 27 deletions Tests/GATTTests/GATTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import BluetoothHCI

final class GATTTests: XCTestCase {

typealias TestPeripheral = GATTPeripheral<TestHostController, TestL2CAPSocket>
typealias TestPeripheral = GATTPeripheral<TestHostController, TestL2CAPServer>
typealias TestCentral = GATTCentral<TestHostController, TestL2CAPSocket>

func testScanData() {
Expand Down Expand Up @@ -150,7 +150,7 @@ final class GATTTests: XCTestCase {
(ATTReadByGroupTypeResponse(attributeData: [
ATTReadByGroupTypeResponse.AttributeData(attributeHandle: 0x001,
endGroupHandle: 0x0004,
value: BluetoothUUID.batteryService.littleEndian.data)
value: Data(BluetoothUUID.batteryService.littleEndian))
])!,
[0x11, 0x06, 0x01, 0x00, 0x04, 0x00, 0x0F, 0x18]),
/**
Expand Down Expand Up @@ -181,17 +181,17 @@ final class GATTTests: XCTestCase {
let batteryLevel = GATTBatteryLevel(level: .min)

let characteristics = [
GATTAttribute.Characteristic(
GATTAttribute<Data>.Characteristic(
uuid: type(of: batteryLevel).uuid,
value: batteryLevel.data,
permissions: [.read],
properties: [.read, .notify],
descriptors: [GATTClientCharacteristicConfiguration().descriptor])
descriptors: [.init(GATTClientCharacteristicConfiguration(), permissions: [.read, .write])])
]

let service = GATTAttribute.Service(
let service = GATTAttribute<Data>.Service(
uuid: .batteryService,
primary: true,
isPrimary: true,
characteristics: characteristics
)

Expand Down Expand Up @@ -235,7 +235,7 @@ final class GATTTests: XCTestCase {
let batteryLevel = GATTBatteryLevel(level: .max)

let characteristics = [
GATTAttribute.Characteristic(
GATTAttribute<Data>.Characteristic(
uuid: type(of: batteryLevel).uuid,
value: batteryLevel.data,
permissions: [.read, .write],
Expand All @@ -244,9 +244,9 @@ final class GATTTests: XCTestCase {
)
]

let service = GATTAttribute.Service(
let service = GATTAttribute<Data>.Service(
uuid: .batteryService,
primary: true,
isPrimary: true,
characteristics: characteristics
)

Expand Down Expand Up @@ -313,18 +313,18 @@ final class GATTTests: XCTestCase {
let batteryLevel = GATTBatteryLevel(level: .max)

let characteristics = [
GATTAttribute.Characteristic(
GATTAttribute<Data>.Characteristic(
uuid: type(of: batteryLevel).uuid,
value: batteryLevel.data,
permissions: [.read],
properties: [.read, .notify],
descriptors: [GATTClientCharacteristicConfiguration().descriptor]
descriptors: [.init(GATTClientCharacteristicConfiguration(), permissions: [.read, .write])]
)
]

let service = GATTAttribute.Service(
let service = GATTAttribute<Data>.Service(
uuid: .batteryService,
primary: true,
isPrimary: true,
characteristics: characteristics
)

Expand Down Expand Up @@ -378,18 +378,18 @@ final class GATTTests: XCTestCase {
let batteryLevel = GATTBatteryLevel(level: .max)

let characteristics = [
GATTAttribute.Characteristic(
GATTAttribute<Data>.Characteristic(
uuid: type(of: batteryLevel).uuid,
value: batteryLevel.data,
permissions: [.read],
properties: [.read, .indicate],
descriptors: [GATTClientCharacteristicConfiguration().descriptor]
descriptors: [.init(GATTClientCharacteristicConfiguration(), permissions: [.read, .write])]
)
]

let service = GATTAttribute.Service(
let service = GATTAttribute<Data>.Service(
uuid: .batteryService,
primary: true,
isPrimary: true,
characteristics: characteristics
)

Expand Down Expand Up @@ -440,28 +440,28 @@ final class GATTTests: XCTestCase {
func testDescriptors() async throws {

let descriptors = [
GATTClientCharacteristicConfiguration().descriptor,
.init(GATTClientCharacteristicConfiguration(), permissions: [.read, .write]),
//GATTUserDescription(userDescription: "Characteristic").descriptor,
GATTAttribute.Descriptor(uuid: BluetoothUUID(),
GATTAttribute<Data>.Descriptor(uuid: BluetoothUUID(),
value: Data("UInt128 Descriptor".utf8),
permissions: [.read, .write]),
GATTAttribute.Descriptor(uuid: .savantSystems,
GATTAttribute<Data>.Descriptor(uuid: .savantSystems,
value: Data("Savant".utf8),
permissions: [.read]),
GATTAttribute.Descriptor(uuid: .savantSystems2,
GATTAttribute<Data>.Descriptor(uuid: .savantSystems2,
value: Data("Savant2".utf8),
permissions: [.write])
]

let characteristic = GATTAttribute.Characteristic(uuid: BluetoothUUID(),
let characteristic = GATTAttribute<Data>.Characteristic(uuid: BluetoothUUID(),
value: Data(),
permissions: [.read],
properties: [.read],
descriptors: descriptors)

let service = GATTAttribute.Service(
let service = GATTAttribute<Data>.Service(
uuid: BluetoothUUID(),
primary: true,
isPrimary: true,
characteristics: [characteristic]
)

Expand Down Expand Up @@ -540,7 +540,7 @@ extension GATTTests {
let peripheral = TestPeripheral(
hostController: serverHostController,
options: serverOptions,
socket: TestL2CAPSocket.self
socket: TestL2CAPServer.self
)
peripheral.log = { print("Peripheral:", $0) }
try await server(peripheral)
Expand Down Expand Up @@ -578,10 +578,10 @@ extension GATTTests {
// decode and compare
for (testPDU, testData) in testPDUs {

guard let decodedPDU = type(of: testPDU).init(data: Data(testData))
guard let decodedPDU = type(of: testPDU).init(data: testData)
else { XCTFail("Could not decode \(type(of: testPDU))"); return }

XCTAssertEqual(decodedPDU.data, Data(testData), file: file, line: line)
XCTAssertEqual(Data(decodedPDU), Data(testData), file: file, line: line)
}
}

Expand Down
11 changes: 7 additions & 4 deletions Tests/GATTTests/TestHostController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Bluetooth
import BluetoothHCI

final class TestHostController: BluetoothHostControllerInterface {

typealias Data = Foundation.Data

/// All controllers on the host.
static var controllers: [TestHostController] { return [TestHostController(address: .min)] }
Expand Down Expand Up @@ -83,7 +85,7 @@ final class TestHostController: BluetoothHostControllerInterface {
/// Sends a command to the device and waits for a response with return parameter values.
func deviceRequest <Return: HCICommandReturnParameter> (_ commandReturnType : Return.Type, timeout: HCICommandTimeout) throws -> Return {
if commandReturnType == HCIReadDeviceAddress.self {
return HCIReadDeviceAddress(data: self.address.littleEndian.data)! as! Return
return HCIReadDeviceAddress(data: Data(self.address.littleEndian))! as! Return
}
fatalError("\(commandReturnType) not mocked")
}
Expand All @@ -96,10 +98,9 @@ final class TestHostController: BluetoothHostControllerInterface {
}

/// Polls and waits for events.
/// Polls and waits for events.
func recieve<Event>(_ eventType: Event.Type) async throws -> Event where Event : HCIEventParameter, Event.HCIEventType == HCIGeneralEvent {
func receive<Event>(_ eventType: Event.Type) async throws -> Event where Event : BluetoothHCI.HCIEventParameter, Event.HCIEventType == BluetoothHCI.HCIGeneralEvent {

guard eventType == HCILowEnergyMetaEvent.self
guard eventType == HCILowEnergyMetaEvent<Data>.self
else { fatalError("Invalid event parameter type") }

while self.advertisingReports.isEmpty {
Expand All @@ -120,6 +121,8 @@ final class TestHostController: BluetoothHostControllerInterface {
assert(eventHeader?.event.rawValue == Event.event.rawValue)
return eventParameter
}


}

internal extension Array {
Expand Down
Loading

0 comments on commit f860151

Please sign in to comment.