Skip to content

Commit 7ebdc0f

Browse files
authored
Merge pull request #889 from Quick/wasm
Experimental SwiftWasm support
2 parents 96a3c42 + 35f9cc9 commit 7ebdc0f

16 files changed

+83
-8
lines changed

.github/workflows/wasm.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: SwiftWasm
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- .github/workflows/wasm.yml
7+
schedule:
8+
- cron: "0 0 * * 1"
9+
workflow_dispatch:
10+
11+
jobs:
12+
test:
13+
name: Test SwiftWasm
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: swiftwasm/[email protected]

Sources/Nimble/Adapters/NMBExpectation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
#if canImport(Darwin) && !SWIFT_PACKAGE
24
import class Foundation.NSObject
35
import typealias Foundation.TimeInterval
@@ -150,3 +152,5 @@ public class NMBExpectation: NSObject {
150152
}
151153

152154
#endif
155+
156+
#endif // #if !os(WASI)

Sources/Nimble/Adapters/NimbleEnvironment.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
#if !os(WASI)
12
import Dispatch
2-
import class Foundation.NSObject
33
import class Foundation.Thread
4+
#endif
5+
import class Foundation.NSObject
46

57
/// "Global" state of Nimble is stored here. Only DSL functions should access / be aware of this
68
/// class' existence
79
internal class NimbleEnvironment: NSObject {
10+
#if os(WASI)
11+
static var activeInstance: NimbleEnvironment = NimbleEnvironment()
12+
#else
813
static var activeInstance: NimbleEnvironment {
914
get {
1015
let env = Thread.current.threadDictionary["NimbleEnvironment"]
@@ -20,6 +25,7 @@ internal class NimbleEnvironment: NSObject {
2025
Thread.current.threadDictionary["NimbleEnvironment"] = newValue
2126
}
2227
}
28+
#endif
2329

2430
// swiftlint:disable:next todo
2531
// TODO: eventually migrate the global to this environment value
@@ -29,15 +35,19 @@ internal class NimbleEnvironment: NSObject {
2935
}
3036

3137
var suppressTVOSAssertionWarning: Bool = false
38+
#if !os(WASI)
3239
var awaiter: Awaiter
40+
#endif
3341

3442
override init() {
43+
#if !os(WASI)
3544
let timeoutQueue = DispatchQueue.global(qos: .userInitiated)
3645
awaiter = Awaiter(
3746
waitLock: AssertionWaitLock(),
3847
asyncQueue: .main,
3948
timeoutQueue: timeoutQueue
4049
)
50+
#endif
4151

4252
super.init()
4353
}

Sources/Nimble/DSL+Wait.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import Dispatch
24
import Foundation
35

@@ -117,3 +119,5 @@ internal func blockedRunLoopErrorMessageFor(_ fnName: String, leeway: DispatchTi
117119
public func waitUntil(timeout: DispatchTimeInterval = AsyncDefaults.timeout, file: FileString = #file, line: UInt = #line, action: @escaping (@escaping () -> Void) -> Void) {
118120
NMBWait.until(timeout: timeout, file: file, line: line, action: action)
119121
}
122+
123+
#endif // #if !os(WASI)

Sources/Nimble/Matchers/Async.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import Foundation
24
import Dispatch
35

@@ -180,3 +182,5 @@ extension Expectation {
180182
return toNever(predicate, until: until, pollInterval: pollInterval, description: description)
181183
}
182184
}
185+
186+
#endif // #if !os(WASI)

Sources/Nimble/Matchers/BeginWith.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public func beginWith(_ startingElement: Any) -> Predicate<NMBOrderedCollection>
1717
return Predicate.simple("begin with <\(startingElement)>") { actualExpression in
1818
guard let collection = try actualExpression.evaluate() else { return .fail }
1919
guard collection.count > 0 else { return .doesNotMatch }
20-
#if os(Linux)
20+
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
21+
let collectionValue = collection.object(at: 0) as AnyObject
22+
#else
2123
guard let collectionValue = collection.object(at: 0) as? NSObject else {
2224
return .fail
2325
}
24-
#else
25-
let collectionValue = collection.object(at: 0) as AnyObject
2626
#endif
2727
return PredicateStatus(bool: collectionValue.isEqual(startingElement))
2828
}

Sources/Nimble/Matchers/EndWith.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public func endWith(_ endingElement: Any) -> Predicate<NMBOrderedCollection> {
2525
guard let collection = try actualExpression.evaluate() else { return .fail }
2626

2727
guard collection.count > 0 else { return PredicateStatus(bool: false) }
28-
#if os(Linux)
28+
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
29+
let collectionValue = collection.object(at: collection.count - 1) as AnyObject
30+
#else
2931
guard let collectionValue = collection.object(at: collection.count - 1) as? NSObject else {
3032
return .fail
3133
}
32-
#else
33-
let collectionValue = collection.object(at: collection.count - 1) as AnyObject
3434
#endif
3535

3636
return PredicateStatus(bool: collectionValue.isEqual(endingElement))

Sources/Nimble/Matchers/MatcherProtocols.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ public protocol NMBDoubleConvertible {
4646
extension NSNumber: NMBDoubleConvertible {
4747
}
4848

49+
#if !os(WASI)
4950
private let dateFormatter: DateFormatter = {
5051
let formatter = DateFormatter()
5152
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSS"
5253
formatter.locale = Locale(identifier: "en_US_POSIX")
5354

5455
return formatter
5556
}()
57+
#endif
5658

5759
extension Date: NMBDoubleConvertible {
5860
public var doubleValue: CDouble {
@@ -66,6 +68,7 @@ extension NSDate: NMBDoubleConvertible {
6668
}
6769
}
6870

71+
#if !os(WASI)
6972
extension Date: TestOutputStringConvertible {
7073
public var testDescription: String {
7174
return dateFormatter.string(from: self)
@@ -77,6 +80,7 @@ extension NSDate: TestOutputStringConvertible {
7780
return dateFormatter.string(from: Date(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate))
7881
}
7982
}
83+
#endif
8084

8185
#if canImport(Darwin)
8286
/// Protocol for types to support beLessThan(), beLessThanOrEqualTo(),

Sources/Nimble/Matchers/PostNotification.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
#if canImport(Foundation)
24
import Foundation
35

@@ -97,4 +99,6 @@ public func postDistributedNotifications<Out>(
9799
}
98100
#endif
99101

100-
#endif
102+
#endif // #if canImport(Foundation)
103+
104+
#endif // #if !os(WASI)

Sources/Nimble/Utils/Await.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import CoreFoundation
24
import Dispatch
35
import Foundation
@@ -369,3 +371,5 @@ internal func pollBlock(
369371

370372
return result
371373
}
374+
375+
#endif // #if !os(WASI)

0 commit comments

Comments
 (0)