Skip to content

Commit

Permalink
#102 improving ClassOfDevice and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos21 committed Aug 1, 2018
1 parent f554877 commit 22f3beb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Sources/ClassOfDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ public struct ClassOfDevice {

internal static let length = 3

public let formatType: FormatType
public var formatType: FormatType

public let majorServiceClass: BitMaskOptionSet<MajorServiceClass>
public var majorServiceClass: BitMaskOptionSet<MajorServiceClass>

public let majorDeviceClass: MajorDeviceClass
public var majorDeviceClass: MajorDeviceClass

public init(formatType: FormatType,
majorServiceClass: BitMaskOptionSet<MajorServiceClass>,
majorDeviceClass: MajorDeviceClass) {

self.formatType = formatType
self.majorServiceClass = majorServiceClass
self.majorDeviceClass = majorDeviceClass
}

public init?(data: Data) {

Expand Down Expand Up @@ -90,7 +99,17 @@ public struct ClassOfDevice {
}
}

public extension ClassOfDevice {
extension ClassOfDevice: Equatable {

public static func == (lhs: ClassOfDevice, rhs: ClassOfDevice) -> Bool {

return lhs.formatType == rhs.formatType
&& lhs.majorServiceClass == rhs.majorServiceClass
&& lhs.majorDeviceClass == rhs.majorDeviceClass
}
}

extension ClassOfDevice {

public struct FormatType: RawRepresentable {

Expand Down Expand Up @@ -315,6 +334,14 @@ public extension ClassOfDevice {
}
}

extension ClassOfDevice.MajorDeviceClass: Equatable {

public static func == (lhs: ClassOfDevice.MajorDeviceClass, rhs: ClassOfDevice.MajorDeviceClass) -> Bool {

return lhs.type == rhs.type && lhs.minorClassValue == rhs.minorClassValue
}
}

public extension ClassOfDevice {

typealias Computer = MinorDeviceClass.Computer
Expand Down
13 changes: 13 additions & 0 deletions Tests/BluetoothTests/BluetoothTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ final class BluetoothTests: XCTestCase {

XCTAssertEqual(classOfDevice.data, data)
XCTAssertEqual(classOfDevice.formatType, ClassOfDevice.FormatType(rawValue: 0b01))

guard let formatType = ClassOfDevice.FormatType(rawValue: 0b01)
else { XCTFail("Could not init formatType"); return }

let majorServiceClass = BitMaskOptionSet<ClassOfDevice.MajorServiceClass>(rawValue: 0b11000010000)
let majorDeviceClass = ClassOfDevice.MajorDeviceClass.computer(.laptop)
let classOfDeviceManual = ClassOfDevice(formatType: formatType,
majorServiceClass: majorServiceClass,
majorDeviceClass: majorDeviceClass)

XCTAssertEqual(classOfDevice.formatType, formatType)
XCTAssertEqual(classOfDevice.majorDeviceClass, majorDeviceClass)
XCTAssertEqual(classOfDevice, classOfDeviceManual)
}

do {
Expand Down

0 comments on commit 22f3beb

Please sign in to comment.