Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental SwiftWasm support #889

Merged
merged 4 commits into from May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/wasm.yml
@@ -0,0 +1,17 @@
name: SwiftWasm

on:
pull_request:
paths:
- .github/workflows/wasm.yml
schedule:
- cron: "0 0 * * 1"
workflow_dispatch:

jobs:
test:
name: Test SwiftWasm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: swiftwasm/swiftwasm-action@v5.3
4 changes: 4 additions & 0 deletions Sources/Nimble/Adapters/NMBExpectation.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

#if canImport(Darwin) && !SWIFT_PACKAGE
import class Foundation.NSObject
import typealias Foundation.TimeInterval
Expand Down Expand Up @@ -150,3 +152,5 @@ public class NMBExpectation: NSObject {
}

#endif

#endif // #if !os(WASI)
12 changes: 11 additions & 1 deletion Sources/Nimble/Adapters/NimbleEnvironment.swift
@@ -1,10 +1,15 @@
#if !os(WASI)
import Dispatch
import class Foundation.NSObject
import class Foundation.Thread
#endif
import class Foundation.NSObject

/// "Global" state of Nimble is stored here. Only DSL functions should access / be aware of this
/// class' existence
internal class NimbleEnvironment: NSObject {
#if os(WASI)
static var activeInstance: NimbleEnvironment = NimbleEnvironment()
#else
static var activeInstance: NimbleEnvironment {
get {
let env = Thread.current.threadDictionary["NimbleEnvironment"]
Expand All @@ -20,6 +25,7 @@ internal class NimbleEnvironment: NSObject {
Thread.current.threadDictionary["NimbleEnvironment"] = newValue
}
}
#endif

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

var suppressTVOSAssertionWarning: Bool = false
#if !os(WASI)
var awaiter: Awaiter
#endif

override init() {
#if !os(WASI)
let timeoutQueue = DispatchQueue.global(qos: .userInitiated)
awaiter = Awaiter(
waitLock: AssertionWaitLock(),
asyncQueue: .main,
timeoutQueue: timeoutQueue
)
#endif

super.init()
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Nimble/DSL+Wait.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

import Dispatch
import Foundation

Expand Down Expand Up @@ -117,3 +119,5 @@ internal func blockedRunLoopErrorMessageFor(_ fnName: String, leeway: DispatchTi
public func waitUntil(timeout: DispatchTimeInterval = AsyncDefaults.timeout, file: FileString = #file, line: UInt = #line, action: @escaping (@escaping () -> Void) -> Void) {
NMBWait.until(timeout: timeout, file: file, line: line, action: action)
}

#endif // #if !os(WASI)
4 changes: 4 additions & 0 deletions Sources/Nimble/Matchers/Async.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

import Foundation
import Dispatch

Expand Down Expand Up @@ -180,3 +182,5 @@ extension Expectation {
return toNever(predicate, until: until, pollInterval: pollInterval, description: description)
}
}

#endif // #if !os(WASI)
6 changes: 3 additions & 3 deletions Sources/Nimble/Matchers/BeginWith.swift
Expand Up @@ -17,12 +17,12 @@ public func beginWith(_ startingElement: Any) -> Predicate<NMBOrderedCollection>
return Predicate.simple("begin with <\(startingElement)>") { actualExpression in
guard let collection = try actualExpression.evaluate() else { return .fail }
guard collection.count > 0 else { return .doesNotMatch }
#if os(Linux)
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
let collectionValue = collection.object(at: 0) as AnyObject
#else
guard let collectionValue = collection.object(at: 0) as? NSObject else {
return .fail
}
#else
let collectionValue = collection.object(at: 0) as AnyObject
#endif
return PredicateStatus(bool: collectionValue.isEqual(startingElement))
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Nimble/Matchers/EndWith.swift
Expand Up @@ -25,12 +25,12 @@ public func endWith(_ endingElement: Any) -> Predicate<NMBOrderedCollection> {
guard let collection = try actualExpression.evaluate() else { return .fail }

guard collection.count > 0 else { return PredicateStatus(bool: false) }
#if os(Linux)
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
let collectionValue = collection.object(at: collection.count - 1) as AnyObject
#else
guard let collectionValue = collection.object(at: collection.count - 1) as? NSObject else {
return .fail
}
#else
let collectionValue = collection.object(at: collection.count - 1) as AnyObject
#endif

return PredicateStatus(bool: collectionValue.isEqual(endingElement))
Expand Down
4 changes: 4 additions & 0 deletions Sources/Nimble/Matchers/MatcherProtocols.swift
Expand Up @@ -46,13 +46,15 @@ public protocol NMBDoubleConvertible {
extension NSNumber: NMBDoubleConvertible {
}

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

return formatter
}()
#endif

extension Date: NMBDoubleConvertible {
public var doubleValue: CDouble {
Expand All @@ -66,6 +68,7 @@ extension NSDate: NMBDoubleConvertible {
}
}

#if !os(WASI)
extension Date: TestOutputStringConvertible {
public var testDescription: String {
return dateFormatter.string(from: self)
Expand All @@ -77,6 +80,7 @@ extension NSDate: TestOutputStringConvertible {
return dateFormatter.string(from: Date(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate))
}
}
#endif

#if canImport(Darwin)
/// Protocol for types to support beLessThan(), beLessThanOrEqualTo(),
Expand Down
6 changes: 5 additions & 1 deletion Sources/Nimble/Matchers/PostNotification.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

#if canImport(Foundation)
import Foundation

Expand Down Expand Up @@ -97,4 +99,6 @@ public func postDistributedNotifications<Out>(
}
#endif

#endif
#endif // #if canImport(Foundation)

#endif // #if !os(WASI)
4 changes: 4 additions & 0 deletions Sources/Nimble/Utils/Await.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

import CoreFoundation
import Dispatch
import Foundation
Expand Down Expand Up @@ -369,3 +371,5 @@ internal func pollBlock(

return result
}

#endif // #if !os(WASI)
4 changes: 4 additions & 0 deletions Sources/Nimble/Utils/DispatchTimeInterval.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

import Dispatch

#if canImport(CDispatch)
Expand Down Expand Up @@ -39,3 +41,5 @@ extension TimeInterval {
}
}
#endif

#endif // #if !os(WASI)
4 changes: 4 additions & 0 deletions Tests/NimbleTests/AsynchronousTest.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

import Dispatch
import CoreFoundation
import Foundation
Expand Down Expand Up @@ -283,3 +285,5 @@ final class AsyncTest: XCTestCase {
}
}
}

#endif // #if !os(WASI)
6 changes: 6 additions & 0 deletions Tests/NimbleTests/Helpers/utils.swift
@@ -1,4 +1,6 @@
#if !os(WASI)
import Dispatch
#endif
import Foundation
@testable import Nimble
import XCTest
Expand Down Expand Up @@ -64,12 +66,14 @@ func failsWithErrorMessageForNil(_ message: String, file: FileString = #file, li
failsWithErrorMessage("\(message) (use beNil() to match nils)", file: file, line: line, preferOriginalSourceLocation: preferOriginalSourceLocation, closure: closure)
}

#if !os(WASI)
func deferToMainQueue(action: @escaping () -> Void) {
DispatchQueue.main.async {
Thread.sleep(forTimeInterval: 0.01)
action()
}
}
#endif

#if canImport(Darwin) && !SWIFT_PACKAGE
public class NimbleHelper: NSObject {
Expand All @@ -87,6 +91,7 @@ public class NimbleHelper: NSObject {
}
#endif

#if !os(WASI)
extension Date {
init(dateTimeString: String) {
let dateFormatter = DateFormatter()
Expand All @@ -103,3 +108,4 @@ extension NSDate {
self.init(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate)
}
}
#endif
4 changes: 4 additions & 0 deletions Tests/NimbleTests/Matchers/BeCloseToTest.swift
Expand Up @@ -40,6 +40,7 @@ final class BeCloseToTest: XCTestCase {
}
}

#if !os(WASI)
func testBeCloseToWithDate() {
expect(Date(dateTimeString: "2015-08-26 11:43:00")).to(beCloseTo(Date(dateTimeString: "2015-08-26 11:43:05"), within: 10))

Expand All @@ -58,6 +59,7 @@ final class BeCloseToTest: XCTestCase {
expect(NSDate(dateTimeString: "2015-08-26 11:43:00")).toNot(beCloseTo(expectedDate, within: 0.006))
}
}
#endif

func testBeCloseToOperator() {
expect(1.2) ≈ 1.2001
Expand Down Expand Up @@ -92,6 +94,7 @@ final class BeCloseToTest: XCTestCase {
}
}

#if !os(WASI)
func testBeCloseToOperatorWithDate() {
expect(Date(dateTimeString: "2015-08-26 11:43:00")) ≈ Date(dateTimeString: "2015-08-26 11:43:00")

Expand Down Expand Up @@ -128,6 +131,7 @@ final class BeCloseToTest: XCTestCase {
expect(Date(dateTimeString: "2015-08-26 11:43:00")) == expectedDate ± 0.004
}
}
#endif // #if !os(WASI)

func testBeCloseToArray() {
expect([0.0, 1.1, 2.2]) ≈ [0.0001, 1.1001, 2.2001]
Expand Down
4 changes: 4 additions & 0 deletions Tests/NimbleTests/Matchers/PostNotificationTest.swift
@@ -1,3 +1,5 @@
#if !os(WASI)

import XCTest
import Nimble
import Foundation
Expand Down Expand Up @@ -79,3 +81,5 @@ final class PostNotificationTest: XCTestCase {
}
#endif
}

#endif // #if !os(WASI)
2 changes: 2 additions & 0 deletions Tests/NimbleTests/UserDescriptionTest.swift
Expand Up @@ -35,6 +35,7 @@ final class UserDescriptionTest: XCTestCase {
}
}

#if !os(WASI)
func testToEventuallyMatch_CustomFailureMessage() {
failsWithErrorMessage(
"""
Expand Down Expand Up @@ -67,5 +68,6 @@ final class UserDescriptionTest: XCTestCase {
expect { 1 }.toEventuallyNot(equal(1), description: "These are eventually equal!")
}
}
#endif // #if !os(WASI)

}