Skip to content
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
61 changes: 27 additions & 34 deletions Tests/DependenciesTests/DependencyValuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ final class DependencyValuesTests: XCTestCase {
XCTExpectFailure {
$0.compactDescription.contains(
"""
@Dependency(\\.reuseClient)" has no live implementation, but was accessed from a live \
context.
@Dependency(\\.reuseClient)" has no live implementation, but was accessed from a \
live context.
"""
)
}
Expand Down Expand Up @@ -308,11 +308,9 @@ final class DependencyValuesTests: XCTestCase {
self.wait(for: [expectation], timeout: 1)
}

@MainActor
func testEscapingInFeatureModel_InstanceVariablePropagated() {
let expectation = self.expectation(description: "escape")

@MainActor
class FeatureModel /*: ObservableObject*/ {
@Dependency(\.fullDependency) var fullDependency
func doSomething(expectation: XCTestExpectation) {
Expand All @@ -333,8 +331,7 @@ final class DependencyValuesTests: XCTestCase {
self.wait(for: [expectation], timeout: 1)
}

@MainActor
func testEscapingInFeatureModel_NotPropagated() {
func testEscapingInFeatureModel_NotPropagated() async {
let expectation = self.expectation(description: "escape")

@MainActor
Expand All @@ -349,19 +346,19 @@ final class DependencyValuesTests: XCTestCase {
}
}

let model = withDependencies {
let model = await withDependencies {
$0.fullDependency.value = 42
} operation: {
FeatureModel()
await FeatureModel()
}

model.doSomething(expectation: expectation)
await model.doSomething(expectation: expectation)
self.wait(for: [expectation], timeout: 1)
XCTAssertEqual(model.value, 3)
let newValue = await model.value
XCTAssertEqual(newValue, 3)
}

@MainActor
func testEscapingInFeatureModelWithOverride() {
func testEscapingInFeatureModelWithOverride() async {
let expectation = self.expectation(description: "escape")

@MainActor
Expand All @@ -379,18 +376,17 @@ final class DependencyValuesTests: XCTestCase {
}
}

let model = FeatureModel()
let model = await FeatureModel()

withDependencies {
await withDependencies {
$0.fullDependency.value = 42
} operation: {
model.doSomething(expectation: expectation)
await model.doSomething(expectation: expectation)
}
self.wait(for: [expectation], timeout: 1)
}

@MainActor
func testEscapingInFeatureModelWithOverride_OverideEscaped() {
func testEscapingInFeatureModelWithOverride_OverrideEscaped() async {
let expectation = self.expectation(description: "escape")

@MainActor
Expand All @@ -413,19 +409,19 @@ final class DependencyValuesTests: XCTestCase {
}
}

let model = FeatureModel()
let model = await FeatureModel()

withDependencies {
await withDependencies {
$0.fullDependency.value = 42
} operation: {
model.doSomething(expectation: expectation)
await model.doSomething(expectation: expectation)
}
self.wait(for: [expectation], timeout: 1)
XCTAssertEqual(model.value, 999)
let newValue = await model.value
XCTAssertEqual(newValue, 999)
}

@MainActor
func testEscapingInFeatureModelWithOverride_NotPropagated() {
func testEscapingInFeatureModelWithOverride_NotPropagated() async {
let expectation = self.expectation(description: "escape")

@MainActor
Expand All @@ -440,34 +436,31 @@ final class DependencyValuesTests: XCTestCase {
}
}

let model = FeatureModel()
let model = await FeatureModel()

withDependencies {
await withDependencies {
$0.fullDependency.value = 42
} operation: {
model.doSomething(expectation: expectation)
await model.doSomething(expectation: expectation)
}
self.wait(for: [expectation], timeout: 1)
XCTAssertEqual(model.value, 3)
let newValue = await model.value
XCTAssertEqual(newValue, 3)
}

func testTaskPropagation() async throws {
let expectation = self.expectation(description: "escape")

withDependencies {
let task = withDependencies {
$0.date.now = Date(timeIntervalSinceReferenceDate: 1_234_567_890)
} operation: {
@Dependency(\.date.now) var now: Date
XCTAssertEqual(now.timeIntervalSinceReferenceDate, 1_234_567_890)
Task {
return Task {
XCTAssertEqual(now.timeIntervalSinceReferenceDate, 1_234_567_890)
@Dependency(\.date.now) var now: Date
XCTAssertEqual(now.timeIntervalSinceReferenceDate, 1_234_567_890)
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: 0)
await task.value
}

func testTaskGroupPropagation() async {
Expand Down
6 changes: 3 additions & 3 deletions Tests/DependenciesTests/FireAndForgetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class FireAndForgetTests: XCTestCase {
let didExecute = ActorIsolated(false)

await self.fireAndForget {
try await Task.sleep(nanoseconds: 100 * NSEC_PER_MSEC)
try await Task.sleep(nanoseconds: 100_000_000)
await didExecute.setValue(true)
}

Expand All @@ -23,14 +23,14 @@ final class FireAndForgetTests: XCTestCase {
let didExecute = ActorIsolated(false)

await self.fireAndForget {
try await Task.sleep(nanoseconds: 100 * NSEC_PER_MSEC)
try await Task.sleep(nanoseconds: 100_000_000)
await didExecute.setValue(true)
}

var value = await didExecute.value
XCTAssertEqual(value, false)

try await Task.sleep(nanoseconds: 200 * NSEC_PER_MSEC)
try await Task.sleep(nanoseconds: 200_000_000)
value = await didExecute.value
XCTAssertEqual(value, true)
}
Expand Down