Skip to content

Commit

Permalink
backfill tests for pollUnwrap
Browse files Browse the repository at this point in the history
Also actually mark the non-polling require dsl as discardableResult
  • Loading branch information
younata committed Dec 18, 2023
1 parent cf008d2 commit 40c06e9
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 85 deletions.
26 changes: 20 additions & 6 deletions Sources/Nimble/Polling+Require.swift
Original file line number Diff line number Diff line change
Expand Up @@ -706,42 +706,56 @@ extension AsyncRequirement {
/// Makes sure that the expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `require(expression).toEventuallyNot(beNil())`
@discardableResult
public func unwrapEventually<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) throws -> T {
public func pollUnwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) throws -> T {
try require(file: file, line: line, expression()).toEventuallyNot(beNil())
}

/// Makes sure that the expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `require(expression).toEventuallyNot(beNil())`
@discardableResult
public func unwrapEventually<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) throws -> T {
public func pollUnwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) throws -> T {
try require(file: file, line: line, expression()).toEventuallyNot(beNil())
}

/// Makes sure that the expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `require(expression).toEventuallyNot(beNil())`
@discardableResult
public func pollUnwraps<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) throws -> T {
try require(file: file, line: line, expression()).toEventuallyNot(beNil())
}

/// Makes sure that the expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `require(expression).toEventuallyNot(beNil())`
@discardableResult
public func pollUnwraps<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) throws -> T {
try require(file: file, line: line, expression()).toEventuallyNot(beNil())
}

/// Makes sure that the async expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toEventuallyNot(beNil())`
@discardableResult
public func unwrapEventually<T>(file: FileString = #file, line: UInt = #line, _ expression: @escaping () async throws -> T?) async throws -> T {
public func pollUnwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: @escaping () async throws -> T?) async throws -> T {
try await requirea(file: file, line: line, try await expression()).toEventuallyNot(beNil())
}

/// Makes sure that the async expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toEventuallyNot(beNil())`
@discardableResult
public func unwrapEventually<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T?)) async throws -> T {
public func pollUnwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T?)) async throws -> T {
try await requirea(file: file, line: line, expression()).toEventuallyNot(beNil())
}

/// Makes sure that the async expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toEventuallyNot(beNil())`
@discardableResult
public func unwrapEventuallyAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async throws -> T {
public func pollUnwrapa<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async throws -> T {
try await requirea(file: file, line: line, try await expression()).toEventuallyNot(beNil())
}

/// Makes sure that the async expression evaluates to a non-nil value, otherwise throw an error.
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toEventuallyNot(beNil())`
@discardableResult
public func unwrapEventuallyAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async throws -> T {
public func pollUnwrapa<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async throws -> T {
try await requirea(file: file, line: line, expression()).toEventuallyNot(beNil())
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/Nimble/Requirement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ public struct SyncRequirement<Value> {
}

/// Tests the actual value using a matcher to match.
@discardableResult
public func to(_ matcher: Matcher<Value>, description: String? = nil) throws -> Value {
let (pass, msg, result) = executeRequire(expression, .toMatch, matcher, to: "to", description: description)
return try verify(pass, msg, result)
}

/// Tests the actual value using a matcher to not match.
@discardableResult
public func toNot(_ matcher: Matcher<Value>, description: String? = nil) throws -> Value {
let (pass, msg, result) = executeRequire(expression, .toNotMatch, matcher, to: "to not", description: description)
return try verify(pass, msg, result)
Expand All @@ -110,18 +112,21 @@ public struct SyncRequirement<Value> {
/// Tests the actual value using a matcher to not match.
///
/// Alias to toNot().
@discardableResult
public func notTo(_ matcher: Matcher<Value>, description: String? = nil) throws -> Value {
try toNot(matcher, description: description)
}

// MARK: - AsyncMatchers
/// Tests the actual value using a matcher to match.
@discardableResult
public func to(_ matcher: AsyncMatcher<Value>, description: String? = nil) async throws -> Value {
let (pass, msg, result) = await executeRequire(expression.toAsyncExpression(), .toMatch, matcher, to: "to", description: description)
return try verify(pass, msg, result)
}

/// Tests the actual value using a matcher to not match.
@discardableResult
public func toNot(_ matcher: AsyncMatcher<Value>, description: String? = nil) async throws -> Value {
let (pass, msg, result) = await executeRequire(expression.toAsyncExpression(), .toNotMatch, matcher, to: "to not", description: description)
return try verify(pass, msg, result)
Expand All @@ -130,6 +135,7 @@ public struct SyncRequirement<Value> {
/// Tests the actual value using a matcher to not match.
///
/// Alias to toNot().
@discardableResult
public func notTo(_ matcher: AsyncMatcher<Value>, description: String? = nil) async throws -> Value {
try await toNot(matcher, description: description)
}
Expand All @@ -154,12 +160,14 @@ public struct AsyncRequirement<Value> {
}

/// Tests the actual value using a matcher to match.
@discardableResult
public func to(_ matcher: Matcher<Value>, description: String? = nil) async throws -> Value {
let (pass, msg, result) = executeRequire(await expression.toSynchronousExpression(), .toMatch, matcher, to: "to", description: description)
return try verify(pass, msg, result)
}

/// Tests the actual value using a matcher to not match.
@discardableResult
public func toNot(_ matcher: Matcher<Value>, description: String? = nil) async throws -> Value {
let (pass, msg, result) = executeRequire(await expression.toSynchronousExpression(), .toNotMatch, matcher, to: "to not", description: description)
return try verify(pass, msg, result)
Expand All @@ -168,18 +176,21 @@ public struct AsyncRequirement<Value> {
/// Tests the actual value using a matcher to not match.
///
/// Alias to toNot().
@discardableResult
public func notTo(_ matcher: Matcher<Value>, description: String? = nil) async throws -> Value {
try await toNot(matcher, description: description)
}

// MARK: - AsyncMatchers
/// Tests the actual value using a matcher to match.
@discardableResult
public func to(_ matcher: AsyncMatcher<Value>, description: String? = nil) async throws -> Value {
let (pass, msg, result) = await executeRequire(expression, .toMatch, matcher, to: "to", description: description)
return try verify(pass, msg, result)
}

/// Tests the actual value using a matcher to not match.
@discardableResult
public func toNot(_ matcher: AsyncMatcher<Value>, description: String? = nil) async throws -> Value {
let (pass, msg, result) = await executeRequire(expression, .toNotMatch, matcher, to: "to not", description: description)
return try verify(pass, msg, result)
Expand All @@ -188,6 +199,7 @@ public struct AsyncRequirement<Value> {
/// Tests the actual value using a matcher to not match.
///
/// Alias to toNot().
@discardableResult
public func notTo(_ matcher: AsyncMatcher<Value>, description: String? = nil) async throws -> Value {
try await toNot(matcher, description: description)
}
Expand Down
Loading

0 comments on commit 40c06e9

Please sign in to comment.