Skip to content

Commit

Permalink
Merge pull request #148 from PureSwift/feature/wasm
Browse files Browse the repository at this point in the history
Added WASM support
  • Loading branch information
colemancda authored Nov 17, 2022
2 parents e316b04 + fda397b commit 241f191
Show file tree
Hide file tree
Showing 26 changed files with 97 additions and 47 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/swift-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: SwiftWASM
on: [push]
jobs:
test:
name: Test
runs-on: ubuntu-latest
container: ghcr.io/swiftwasm/carton:latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Swift Version
run: swift --version
- name: Test
run: carton test
38 changes: 3 additions & 35 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jobs:
name: Build
strategy:
matrix:
swift: [5.5.3, 5.6.3, 5.7.1]
os: [ubuntu-latest, macos-latest]
swift: [5.6.3, 5.7.1]
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Swift
Expand All @@ -22,37 +22,5 @@ jobs:
run: swift build -c debug
- name: Build (Release)
run: swift build -c release

test-linux:
name: Test Linux
strategy:
matrix:
swift: [5.5.3, 5.6.3, 5.7.1]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Swift
uses: slashmo/[email protected]
with:
version: ${{ matrix.swift }}
- name: Checkout
uses: actions/checkout@v2
- name: Swift Version
run: swift --version
- name: Test (Debug)
run: swift test --configuration debug
- name: Test (Release)
run: swift test --configuration release -Xswiftc -enable-testing

test-macOS:
name: Test macOS
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Swift Version
run: swift --version
- name: Test (Debug)
run: swift test --configuration debug --enable-code-coverage
- name: Test (Release)
run: swift test --configuration release -Xswiftc -enable-testing --enable-code-coverage
run: swift test -c debug
15 changes: 12 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,18 @@ var package = Package(
name: "BluetoothTests",
dependencies: [
"Bluetooth",
"BluetoothGAP",
"BluetoothGATT",
"BluetoothHCI"
.target(
name: "BluetoothGAP",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows])
),
.target(
name: "BluetoothGATT",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows])
),
.target(
name: "BluetoothHCI",
condition: .when(platforms: [.macOS, .linux, .macCatalyst, .windows])
)
]
)
]
Expand Down
4 changes: 4 additions & 0 deletions Sources/Bluetooth/BluetoothUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ extension BluetoothUUID: CustomStringConvertible {

public var description: String {

#if os(WASI)
return rawValue
#else
if let name = self.name {
return "\(rawValue) (\(name))"
} else {
return rawValue
}
#endif
}
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/Bluetooth/CompanyIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public struct CompanyIdentifier: RawRepresentable, Equatable, Hashable {
}
}

#if !os(WASI)
public extension CompanyIdentifier {

/// Bluetooth Company name.
Expand All @@ -41,6 +42,7 @@ public extension CompanyIdentifier {
return Self.companyIdentifiers[rawValue]
}
}
#endif

// MARK: - ExpressibleByIntegerLiteral

Expand All @@ -56,6 +58,10 @@ extension CompanyIdentifier: ExpressibleByIntegerLiteral {
extension CompanyIdentifier: CustomStringConvertible {

public var description: String {
#if os(WASI)
return rawValue.description
#else
return name ?? rawValue.description
#endif
}
}
2 changes: 2 additions & 0 deletions Sources/Bluetooth/DefinedUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Alsey Coleman Miller on 1/5/18.
//

#if !os(WASI)
public extension BluetoothUUID {

/// Bluetooth UUID Definition name.
Expand Down Expand Up @@ -665,3 +666,4 @@ internal let definedUUIDs: [UInt16: String] = [
0xfffe: "Alliance for Wireless Power (A4WP)",
0xfffd: "Fast IDentity Online Alliance (FIDO)"
]
#endif
2 changes: 2 additions & 0 deletions Sources/Bluetooth/DefinedUUIDExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Generated by Alsey Coleman Miller on 5/29/20.
//

#if !os(WASI)
public extension BluetoothUUID {

/// SDP (`0x0001`)
Expand Down Expand Up @@ -3093,3 +3094,4 @@ public extension BluetoothUUID {
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Bluetooth
//

#if swift(<5.6) || !SWIFTPM_ENABLE_PLUGINS
#if (swift(<5.6) || !SWIFTPM_ENABLE_PLUGINS) && !os(WASI)
internal extension CompanyIdentifier {

static let companyIdentifiers: [UInt16: String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Bluetooth
//

#if swift(<5.6) || !SWIFTPM_ENABLE_PLUGINS
#if (swift(<5.6) || !SWIFTPM_ENABLE_PLUGINS) && !os(WASI)
public extension CompanyIdentifier {

/// Ericsson Technology Licensing (`0`)
Expand Down
2 changes: 2 additions & 0 deletions Sources/Bluetooth/iBeacon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import Foundation
@frozen
public struct AppleBeacon: Equatable, Hashable {

#if !os(WASI)
/// The company that created this specification.
public static var companyIdentifier: CompanyIdentifier { return .apple }
#endif

/// The unique ID of the beacons being targeted.
///
Expand Down
2 changes: 2 additions & 0 deletions Tests/BluetoothTests/AttributeProtocolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//

#if canImport(BluetoothGATT)
import XCTest
import Foundation
@testable import Bluetooth
Expand Down Expand Up @@ -982,3 +983,4 @@ final class AttributeProtocolTests: XCTestCase {
}
}
}
#endif
16 changes: 15 additions & 1 deletion Tests/BluetoothTests/BluetoothTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
import XCTest
import Foundation
@testable import Bluetooth
#if canImport(BluetoothHCI)
import BluetoothHCI
#endif
#if canImport(BluetoothGATT)
import BluetoothGATT
#endif

final class BluetoothTests: XCTestCase {

#if canImport(BluetoothHCI)
func testAdvertisingInterval() {

let value = AdvertisingInterval.default
Expand All @@ -38,6 +43,7 @@ final class BluetoothTests: XCTestCase {
XCTAssertEqual("\(AdvertisingInterval.max)", "10240ms")
XCTAssertEqual("\(AdvertisingInterval.default)", "1280ms")
}
#endif

func testSecurityLevel() {

Expand All @@ -51,11 +57,14 @@ final class BluetoothTests: XCTestCase {

let company: CompanyIdentifier = 76 // Apple, Inc.

#if !os(WASI)
XCTAssertEqual(company.description, "Apple, Inc.")
#endif
XCTAssertNotEqual(company.hashValue, 0)
XCTAssertNotEqual(company, 77)
}

#if canImport(BluetoothHCI)
func testChannelIdentifier() {

XCTAssertEqual(ChannelIdentifier.att, 4)
Expand Down Expand Up @@ -87,6 +96,7 @@ final class BluetoothTests: XCTestCase {
XCTAssertLessThan(HCIVersion.v4_0, .v4_2)
XCTAssertGreaterThan(HCIVersion.v5_0, .v4_2)
}
#endif

func testLowEnergyAdvertisingData() {

Expand All @@ -106,7 +116,8 @@ final class BluetoothTests: XCTestCase {
XCTAssertNotEqual(advertisingData.hashValue, 0)
}
}


#if canImport(BluetoothHCI)
func testLowEnergyAddressType() {

XCTAssertEqual(LowEnergyAddressType(), .public)
Expand Down Expand Up @@ -238,7 +249,9 @@ final class BluetoothTests: XCTestCase {
XCTAssertEqual(AdvertisingChannelHeader().rawValue, 0)
XCTAssertEqual(AdvertisingChannelHeader(), .undirectedAdvertising)
}
#endif

#if canImport(BluetoothGATT)
func testBitMaskOption() {

do {
Expand Down Expand Up @@ -301,6 +314,7 @@ final class BluetoothTests: XCTestCase {
XCTAssert(set == [.read, .write])
}
}
#endif

func testClassOfDevice() {

Expand Down
10 changes: 10 additions & 0 deletions Tests/BluetoothTests/BluetoothUUIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

import XCTest
import Foundation
#if canImport(BluetoothGATT)
import Bluetooth
@testable import BluetoothGATT
#else
@testable import Bluetooth
#endif

final class BluetoothUUIDTests: XCTestCase {

Expand Down Expand Up @@ -83,6 +87,7 @@ final class BluetoothUUIDTests: XCTestCase {
}
}

#if canImport(BluetoothGATT)
func testPrimaryServiceUUID() {

let uuidString = "2800" // big endian representation
Expand All @@ -96,6 +101,7 @@ final class BluetoothUUIDTests: XCTestCase {
XCTAssert(uuid.bigEndian.data == Data([0x28, 0x00]))
XCTAssertEqual(uuid, BluetoothUUID(data: uuid.data))
}
#endif

func test128BitUUID() {

Expand All @@ -119,6 +125,7 @@ final class BluetoothUUIDTests: XCTestCase {

}

#if !os(WASI)
func testDefinedUUID() {

let uuidString = "FEA9"
Expand All @@ -145,6 +152,7 @@ final class BluetoothUUIDTests: XCTestCase {

XCTAssertEqual(uuid, BluetoothUUID(data: uuid.data))
}
#endif

func test32BitUUID() {

Expand All @@ -154,7 +162,9 @@ final class BluetoothUUIDTests: XCTestCase {
guard let uuid = BluetoothUUID(rawValue: uuidString)
else { XCTFail("Could not parse UUID string"); return }

#if !os(WASI)
XCTAssertNil(uuid.name)
#endif
XCTAssertEqual(uuid.rawValue, uuidString)
XCTAssertEqual(uuid.description, uuidString)
XCTAssertEqual(uuid, uuid)
Expand Down
3 changes: 1 addition & 2 deletions Tests/BluetoothTests/DarwinTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

#if os(macOS)
import XCTest
import Foundation
@testable import Bluetooth
import BluetoothGAP

#if os(macOS)
import CoreBluetooth
import IOKit
import Darwin
Expand Down
2 changes: 2 additions & 0 deletions Tests/BluetoothTests/DefinedUUIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Generated by Alsey Coleman Miller on 5/29/20.
//

#if !os(WASI)
import XCTest
import Foundation
@testable import Bluetooth
Expand Down Expand Up @@ -4953,4 +4954,5 @@ final class DefinedUUIDTests: XCTestCase {
}

}
#endif
// swiftlint:enable type_body_length
2 changes: 2 additions & 0 deletions Tests/BluetoothTests/GAPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

#if canImport(BluetoothGAP)
import XCTest
import Foundation
import Bluetooth
Expand Down Expand Up @@ -978,3 +979,4 @@ internal extension GAPDataDecoder {
return try decoder.decode(data)
}
}
#endif
2 changes: 2 additions & 0 deletions Tests/BluetoothTests/GATTCharacteristicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

#if canImport(BluetoothGATT)
import XCTest
import Foundation
import Bluetooth
Expand Down Expand Up @@ -1449,3 +1450,4 @@ final class GATTCharacteristicTests: XCTestCase {
XCTAssertEqual(GATTObjectID(data: data), GATTObjectID(data: data))
}
}
#endif
Loading

0 comments on commit 241f191

Please sign in to comment.