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

[1/y] Add backwards compatibility with Swift 5.5 #283

Merged
merged 1 commit into from
Jan 28, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
name: CI

on: [push, pull_request]
env:
DEVELOPER_DIR: /Applications/Xcode_13.2.app/Contents/Developer

jobs:
test-e2e:
name: Test E2E
name: Test E2E (Xcode ${{ matrix.xcode }})
runs-on: macOS-latest
strategy:
matrix:
xcode:
- '13.1' # Swift 5.5
- '13.2' # Swift 5.5.2
steps:
- uses: actions/checkout@v2
- name: Set Up Project
run: Sources/MockingbirdAutomationCli/buildAndRun.sh configure load --overwrite
- name: Test
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
run: Sources/MockingbirdAutomationCli/buildAndRun.sh test e2e

test-example-project:
Expand All @@ -30,6 +35,7 @@ jobs:
- name: Test
env:
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
DEVELOPER_DIR: /Applications/Xcode_13.2.app/Contents/Developer
run: Sources/MockingbirdAutomationCli/buildAndRun.sh test example ${{ matrix.type }}

test-cli-launcher:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- 'release-*'
tags:
- '*'

env:
DEVELOPER_DIR: /Applications/Xcode_13.2.app/Contents/Developer

Expand Down
16 changes: 9 additions & 7 deletions Sources/MockingbirdFramework/Mocking/MockingContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import Foundation
return try thunk(invocation)
}

/// Invoke a non-throwing thunk.
func didInvoke<T, I: Invocation>(_ invocation: I, evaluating thunk: (I) -> T) -> T {
// Ensures that the thunk is evaluated prior to recording the invocation.
defer { didInvoke(invocation) }
return thunk(invocation)
}

#if swift(>=5.5.2)
/// Invoke an async thunk that can throw.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func didInvoke<T, I: Invocation>(_ invocation: I,
Expand All @@ -29,20 +37,14 @@ import Foundation
return try await thunk(invocation)
}

/// Invoke a non-throwing thunk.
func didInvoke<T, I: Invocation>(_ invocation: I, evaluating thunk: (I) -> T) -> T {
// Ensures that the thunk is evaluated prior to recording the invocation.
defer { didInvoke(invocation) }
return thunk(invocation)
}

/// Invoke an async non-throwing thunk.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func didInvoke<T, I: Invocation>(_ invocation: I, evaluating thunk: (I) async -> T) async -> T {
// Ensures that the thunk is evaluated prior to recording the invocation.
defer { didInvoke(invocation) }
return await thunk(invocation)
}
#endif

/// Invoke a thunk from Objective-C.
@objc public func objcDidInvoke(_ invocation: ObjCInvocation,
Expand Down
11 changes: 7 additions & 4 deletions Sources/MockingbirdFramework/Stubbing/Stubbing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,16 @@ public class StubbingManager<DeclarationType: Declaration, InvocationType, Retur
/// - Returns: The current stubbing manager which can be used to chain additional stubs.
@discardableResult
public func willReturn(_ value: ReturnType) -> Self {
#if swift(>=5.5.2)
if isAsync {
if isThrowing {
return addImplementation({ () async throws in value })
} else {
return addImplementation({ () async in value })
}
} else {
return addImplementation({ value })
}
#endif
return addImplementation({ value })
}

/// Stub a mocked method or property with an implementation provider.
Expand Down Expand Up @@ -497,15 +498,17 @@ public func ~> <DeclarationType: Declaration, InvocationType, ReturnType>(
manager: StaticStubbingManager<DeclarationType, InvocationType, ReturnType>,
implementation: @escaping @autoclosure () -> ReturnType
) {
#if swift(>=5.5.2)
if manager.isAsync {
if manager.isThrowing {
manager.addImplementation({ () async throws in implementation() })
} else {
manager.addImplementation({ () async in implementation() })
}
} else {
manager.addImplementation({ implementation() })
return
}
#endif
manager.addImplementation({ implementation() })
}

/// Stub a mocked method or property with a closure implementation.
Expand Down
2 changes: 2 additions & 0 deletions Sources/MockingbirdTestsHost/AsyncMethods.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

#if swift(>=5.5.2)
protocol AsyncProtocol {
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func asyncMethodVoid() async
Expand All @@ -19,3 +20,4 @@ protocol AsyncProtocol {
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func asyncClosureThrowingMethod(block: () async throws -> Bool) async throws -> Bool
}
#endif
2 changes: 2 additions & 0 deletions Tests/MockingbirdTests/Framework/StubbingAsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Mockingbird
@testable import MockingbirdTestsHost
import XCTest

#if swift(>=5.5.2)
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension XCTest {
func XCTAssertThrowsAsyncError<T: Sendable>(
Expand Down Expand Up @@ -109,3 +110,4 @@ class StubbingAsyncTests: BaseTestCase {
}

}
#endif