Skip to content
Open
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
6 changes: 6 additions & 0 deletions .github/workflows/ci-swiftpm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
if: ${{ needs.filter.outputs.should_skip != 'true' }}
- run: swift test
if: ${{ needs.filter.outputs.should_skip != 'true' }}
- run: swift test --traits Include_Nimble
if: ${{ needs.filter.outputs.should_skip != 'true' }}

swiftpm_darwin_15:
name: SwiftPM, Darwin 15, Xcode ${{ matrix.xcode }}
Expand All @@ -55,6 +57,8 @@ jobs:
if: ${{ needs.filter.outputs.should_skip != 'true' }}
- run: swift test
if: ${{ needs.filter.outputs.should_skip != 'true' }}
- run: swift test --traits Include_Nimble
if: ${{ needs.filter.outputs.should_skip != 'true' }}

swiftpm_linux:
name: SwiftPM, Linux
Expand All @@ -75,6 +79,8 @@ jobs:
if: ${{ needs.filter.outputs.should_skip != 'true' }}
- run: swift test
if: ${{ needs.filter.outputs.should_skip != 'true' }}
- run: swift test --traits Include_Nimble
if: ${{ needs.filter.outputs.should_skip != 'true' }}

swiftpm_android:
name: SwiftPM, Android
Expand Down
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 35 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
// swift-tools-version: 6.0
// swift-tools-version: 6.1

import PackageDescription

let package = Package(
name: "swift-fakes",
platforms: [
.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .visionOS(.v1)
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.visionOS(.v1)
],
products: [
.library(
name: "Fakes",
targets: ["Fakes"]
),
],
traits: [
.init(
name: "Include_Nimble",
description: "Enable Nimble Integration",
enabledTraits: []
),
],
dependencies: [
.package(url: "https://github.com/Quick/Nimble.git", from: "14.0.0"),
.package(
url: "https://github.com/Quick/Nimble.git",
from: "14.0.0"
),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
.target(
name: "Fakes",
dependencies: ["Nimble"],
dependencies: [
.product(
name: "Nimble",
package: "Nimble",
condition: .when(
traits: ["Include_Nimble"]
)
)
],
resources: [
.copy("PrivacyInfo.xcprivacy")
]
),
.testTarget(
name: "FakesTests",
dependencies: ["Fakes", "Nimble"]
dependencies: [
"Fakes",
.product(
name: "Nimble",
package: "Nimble",
condition: .when(
traits: ["Include_Nimble"]
)
)
]
),
]
)
2 changes: 2 additions & 0 deletions Sources/Fakes/Spy/Spy+Nimble.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if Include_Nimble
import Nimble

// MARK: - Verifying any calls to the Spy.
Expand Down Expand Up @@ -303,3 +304,4 @@ private func _mostRecentlyBeCalled<Arguments, Returning>(
)
}
}
#endif
117 changes: 58 additions & 59 deletions Tests/FakesTests/DynamicResultTests.swift
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
import Fakes
import Nimble
import XCTest
import Testing

final class DynamicResultTests: XCTestCase {
func testSingleStaticValue() {
struct DynamicResultTests {
@Test func testSingleStaticValue() {
let subject = DynamicResult<Int, Int>(1)

expect(subject.call(1)).to(equal(1))
expect(subject.call(1)).to(equal(1))
expect(subject.call(1)).to(equal(1))
#expect(subject.call(1) == 1)
#expect(subject.call(1) == 1)
#expect(subject.call(1) == 1)
}

func testMultipleStaticValues() {
@Test func testMultipleStaticValues() {
let subject = DynamicResult<Void, Int>(1, 2, 3)

expect(subject.call()).to(equal(1))
expect(subject.call()).to(equal(2))
expect(subject.call()).to(equal(3))
#expect(subject.call() == 1)
#expect(subject.call() == 2)
#expect(subject.call() == 3)
// After the last call, we continue to return the last stub in the list
expect(subject.call()).to(equal(3))
expect(subject.call()).to(equal(3))
#expect(subject.call() == 3)
#expect(subject.call() == 3)
}

func testClosure() {
@Test func testClosure() {
let subject = DynamicResult<Int, Int>({ $0 + 1 })

expect(subject.call(1)).to(equal(2))
expect(subject.call(2)).to(equal(3))
expect(subject.call(3)).to(equal(4))
#expect(subject.call(1) == 2)
#expect(subject.call(2) == 3)
#expect(subject.call(3) == 4)
}

func testStubs() {
@Test func testStubs() {
let subject = DynamicResult<Int, Int>(
.value(1),
.closure({ $0 * 3}),
.value(4)
)

expect(subject.call(1)).to(equal(1))
expect(subject.call(2)).to(equal(6))
expect(subject.call(3)).to(equal(4))
expect(subject.call(3)).to(equal(4))
#expect(subject.call(1) == 1)
#expect(subject.call(2) == 6)
#expect(subject.call(3) == 4)
#expect(subject.call(3) == 4)
}

// MARK: - Replacement Tests

func testReplacementStaticValue() {
@Test func testReplacementStaticValue() {
let subject = DynamicResult<Void, Int>(1)

subject.replace(2, 3, 4, 6)

expect(subject.call()).to(equal(2))
expect(subject.call()).to(equal(3))
expect(subject.call()).to(equal(4))
expect(subject.call()).to(equal(6))
#expect(subject.call() == 2)
#expect(subject.call() == 3)
#expect(subject.call() == 4)
#expect(subject.call() == 6)
// After the last call, we continue to return the last stub in the list
expect(subject.call()).to(equal(6))
#expect(subject.call() == 6)
}

func testReplacementClosure() {
@Test func testReplacementClosure() {
let subject = DynamicResult<Int, Int>(1)

subject.replace({ $0 + 4})

expect(subject.call(1)).to(equal(5))
expect(subject.call(2)).to(equal(6))
expect(subject.call(3)).to(equal(7))
#expect(subject.call(1) == 5)
#expect(subject.call(2) == 6)
#expect(subject.call(3) == 7)
}

func testReplacementStubs() {
@Test func testReplacementStubs() {
let subject = DynamicResult<Int, Int>(1)

subject.replace(
Expand All @@ -77,42 +76,42 @@ final class DynamicResultTests: XCTestCase {
.value(4)
)

expect(subject.call(1)).to(equal(2))
expect(subject.call(2)).to(equal(6))
expect(subject.call(3)).to(equal(4))
expect(subject.call(3)).to(equal(4))
#expect(subject.call(1) == 2)
#expect(subject.call(2) == 6)
#expect(subject.call(3) == 4)
#expect(subject.call(3) == 4)
}

// MARK: - Appending Tests

func testAppendingStaticValue() {
@Test func testAppendingStaticValue() {
let subject = DynamicResult<Void, Int>(1, 2, 3)

subject.append(4, 5, 6)

expect(subject.call()).to(equal(1))
expect(subject.call()).to(equal(2))
expect(subject.call()).to(equal(3))
expect(subject.call()).to(equal(4))
expect(subject.call()).to(equal(5))
expect(subject.call()).to(equal(6))
#expect(subject.call() == 1)
#expect(subject.call() == 2)
#expect(subject.call() == 3)
#expect(subject.call() == 4)
#expect(subject.call() == 5)
#expect(subject.call() == 6)
// After the last call, we continue to return the last stub in the list
expect(subject.call()).to(equal(6))
#expect(subject.call() == 6)
}

func testAppendingClosure() {
@Test func testAppendingClosure() {
let subject = DynamicResult<Int, Int>(1, 2, 3)

subject.append({ $0 + 10})

expect(subject.call(1)).to(equal(1))
expect(subject.call(2)).to(equal(2))
expect(subject.call(3)).to(equal(3))
expect(subject.call(4)).to(equal(14))
expect(subject.call(5)).to(equal(15))
#expect(subject.call(1) == 1)
#expect(subject.call(2) == 2)
#expect(subject.call(3) == 3)
#expect(subject.call(4) == 14)
#expect(subject.call(5) == 15)
}

func testAppendingStubs() {
@Test func testAppendingStubs() {
let subject = DynamicResult<Int, Int>(1, 2, 3)

subject.append(
Expand All @@ -121,12 +120,12 @@ final class DynamicResultTests: XCTestCase {
.value(6)
)

expect(subject.call(1)).to(equal(1))
expect(subject.call(2)).to(equal(2))
expect(subject.call(3)).to(equal(3))
expect(subject.call(4)).to(equal(4))
expect(subject.call(5)).to(equal(15))
expect(subject.call(6)).to(equal(6))
expect(subject.call(7)).to(equal(6))
#expect(subject.call(1) == 1)
#expect(subject.call(2) == 2)
#expect(subject.call(3) == 3)
#expect(subject.call(4) == 4)
#expect(subject.call(5) == 15)
#expect(subject.call(6) == 6)
#expect(subject.call(7) == 6)
}
}
28 changes: 15 additions & 13 deletions Tests/FakesTests/PendableTests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Fakes
import Nimble
import XCTest
import Testing

final class PendableTests: XCTestCase {
func testSingleCall() async throws {
struct PendableTests {
@Test func testSingleCall() async throws {
let subject = Pendable<Int>.pending(fallback: 0)

async let result = subject.call()
Expand All @@ -13,10 +12,10 @@ final class PendableTests: XCTestCase {
subject.resolve(with: 2)

let value = await result
expect(value).to(equal(2))
#expect(value == 2)
}

func testMultipleCalls() async throws {
@Test func testMultipleCalls() async throws {
let subject = Pendable<Int>.pending(fallback: 0)

async let result = withTaskGroup(of: Int.self, returning: [Int].self) { taskGroup in
Expand All @@ -36,17 +35,20 @@ final class PendableTests: XCTestCase {
subject.resolve(with: 3)

let value = await result
expect(value).to(equal(Array(repeating: 3, count: 100)))
#expect(value == Array(repeating: 3, count: 100))
}

func testAutoresolve() async {
@Test func testAutoresolve() async throws {
let subject = Pendable<Int>.pending(fallback: 3)
let spy = Spy<Int, Void>()

await waitUntil(timeout: .milliseconds(500)) { done in
Task<Void, Never> {
_ = await subject.call(fallbackDelay: 0.1)
done()
}
Task<Void, Never> {
let value = await subject.call(fallbackDelay: 0.1)
spy(value)
}

try await Task.sleep(for: .milliseconds(500))

#expect(spy.wasCalled(with: 3))
}
}
Loading
Loading