Skip to content

Commit

Permalink
require should allow you to discard the result
Browse files Browse the repository at this point in the history
Rename requireAsync to requirea - matching the example set forth by expecta.
Adds requires, which is requirea, but for specifying SyncRequirement.
  • Loading branch information
younata committed Dec 18, 2023
1 parent 5fdf899 commit d04e1c2
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 208 deletions.
8 changes: 4 additions & 4 deletions Nimble.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
891729D72B18431D005CC866 /* Requirement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891729D62B18431D005CC866 /* Requirement.swift */; };
891A04712AB0164500B46613 /* AsyncTimerSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891A04702AB0164500B46613 /* AsyncTimerSequence.swift */; };
8922828A2B2833B7002DA355 /* PollingTest+Require.swift in Sources */ = {isa = PBXBuildFile; fileRef = 892282892B2833B7002DA355 /* PollingTest+Require.swift */; };
8922828D2B283818002DA355 /* Polling+AsyncAwait+Require.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8922828B2B2837E1002DA355 /* Polling+AsyncAwait+Require.swift */; };
8922828D2B283818002DA355 /* Polling+Require.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8922828B2B2837E1002DA355 /* Polling+Require.swift */; };
8922828F2B283956002DA355 /* AsyncAwaitTest+Require.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8922828E2B283956002DA355 /* AsyncAwaitTest+Require.swift */; };
892FDF1329D3EA7700523A80 /* AsyncExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 892FDF1229D3EA7700523A80 /* AsyncExpression.swift */; };
896962412A5FABD000A7929D /* AsyncAllPass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 896962402A5FABD000A7929D /* AsyncAllPass.swift */; };
Expand Down Expand Up @@ -319,7 +319,7 @@
891729D62B18431D005CC866 /* Requirement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Requirement.swift; sourceTree = "<group>"; };
891A04702AB0164500B46613 /* AsyncTimerSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncTimerSequence.swift; sourceTree = "<group>"; };
892282892B2833B7002DA355 /* PollingTest+Require.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PollingTest+Require.swift"; sourceTree = "<group>"; };
8922828B2B2837E1002DA355 /* Polling+AsyncAwait+Require.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Polling+AsyncAwait+Require.swift"; sourceTree = "<group>"; };
8922828B2B2837E1002DA355 /* Polling+Require.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Polling+Require.swift"; sourceTree = "<group>"; };
8922828E2B283956002DA355 /* AsyncAwaitTest+Require.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AsyncAwaitTest+Require.swift"; sourceTree = "<group>"; };
892FDF1229D3EA7700523A80 /* AsyncExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncExpression.swift; sourceTree = "<group>"; };
896962402A5FABD000A7929D /* AsyncAllPass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncAllPass.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -466,7 +466,7 @@
1FD8CD0B1968AB07008ED995 /* FailureMessage.swift */,
1F1871E31CA89FB600A34BF2 /* Polling.swift */,
89F5E0852908E655001F9377 /* Polling+AsyncAwait.swift */,
8922828B2B2837E1002DA355 /* Polling+AsyncAwait+Require.swift */,
8922828B2B2837E1002DA355 /* Polling+Require.swift */,
891729D62B18431D005CC866 /* Requirement.swift */,
1F1A742D1940169200FFFC47 /* Info.plist */,
1FD8CD0C1968AB07008ED995 /* Matchers */,
Expand Down Expand Up @@ -987,7 +987,7 @@
891729D72B18431D005CC866 /* Requirement.swift in Sources */,
1FD8CD391968AB07008ED995 /* Expression.swift in Sources */,
891A04712AB0164500B46613 /* AsyncTimerSequence.swift in Sources */,
8922828D2B283818002DA355 /* Polling+AsyncAwait+Require.swift in Sources */,
8922828D2B283818002DA355 /* Polling+Require.swift in Sources */,
89EEF5A52A03293100988224 /* AsyncMatcher.swift in Sources */,
1FD8CD3B1968AB07008ED995 /* FailureMessage.swift in Sources */,
1FA0C3FF1E30B14500623165 /* Matcher.swift in Sources */,
Expand Down
103 changes: 87 additions & 16 deletions Sources/Nimble/DSL+Require.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Make a ``SyncRequirement`` on a given actual value. The value given is lazily evaluated.
@discardableResult
public func require<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) -> SyncRequirement<T> {
return SyncRequirement(
expression: Expression(
Expand All @@ -8,6 +9,7 @@ public func require<T>(file: FileString = #file, line: UInt = #line, _ expressio
}

/// Make a ``SyncRequirement`` on a given actual value. The closure is lazily invoked.
@discardableResult
public func require<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T)) -> SyncRequirement<T> {
return SyncRequirement(
expression: Expression(
Expand All @@ -17,6 +19,7 @@ public func require<T>(file: FileString = #file, line: UInt = #line, _ expressio
}

/// Make a ``SyncRequirement`` on a given actual value. The closure is lazily invoked.
@discardableResult
public func require<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) -> SyncRequirement<T> {
return SyncRequirement(
expression: Expression(
Expand All @@ -26,6 +29,7 @@ public func require<T>(file: FileString = #file, line: UInt = #line, _ expressio
}

/// Make a ``SyncRequirement`` on a given actual value. The closure is lazily invoked.
@discardableResult
public func require(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> Void)) -> SyncRequirement<Void> {
return SyncRequirement(
expression: Expression(
Expand All @@ -34,7 +38,52 @@ public func require(file: FileString = #file, line: UInt = #line, _ expression:
isClosure: true))
}

/// Make a ``SyncRequirement`` on a given actual value. The value given is lazily evaluated.
/// This is provided as an alternative to ``require``, for when you want to be specific about whether you're using ``SyncRequirement`` or ``AsyncRequirement``.
@discardableResult
public func requires<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) -> SyncRequirement<T> {
return SyncRequirement(
expression: Expression(
expression: expression,
location: SourceLocation(file: file, line: line),
isClosure: true))
}

/// Make a ``SyncRequirement`` on a given actual value. The closure is lazily invoked.
/// This is provided as an alternative to ``require``, for when you want to be specific about whether you're using ``SyncRequirement`` or ``AsyncRequirement``.
@discardableResult
public func requires<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T)) -> SyncRequirement<T> {
return SyncRequirement(
expression: Expression(
expression: expression(),
location: SourceLocation(file: file, line: line),
isClosure: true))
}

/// Make a ``SyncRequirement`` on a given actual value. The closure is lazily invoked.
/// This is provided as an alternative to ``require``, for when you want to be specific about whether you're using ``SyncRequirement`` or ``AsyncRequirement``.
@discardableResult
public func requires<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) -> SyncRequirement<T> {
return SyncRequirement(
expression: Expression(
expression: expression(),
location: SourceLocation(file: file, line: line),
isClosure: true))
}

/// Make a ``SyncRequirement`` on a given actual value. The closure is lazily invoked.
/// This is provided as an alternative to ``require``, for when you want to be specific about whether you're using ``SyncRequirement`` or ``AsyncRequirement``.
@discardableResult
public func requires(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> Void)) -> SyncRequirement<Void> {
return SyncRequirement(
expression: Expression(
expression: expression(),
location: SourceLocation(file: file, line: line),
isClosure: true))
}

/// Make an ``AsyncRequirement`` on a given actual value. The value given is lazily evaluated.
@discardableResult
public func require<T>(file: FileString = #file, line: UInt = #line, _ expression: @escaping () async throws -> T?) -> AsyncRequirement<T> {
return AsyncRequirement(
expression: AsyncExpression(
Expand All @@ -44,6 +93,7 @@ public func require<T>(file: FileString = #file, line: UInt = #line, _ expressio
}

/// Make an ``AsyncRequirement`` on a given actual value. The closure is lazily invoked.
@discardableResult
public func require<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T)) -> AsyncRequirement<T> {
return AsyncRequirement(
expression: AsyncExpression(
Expand All @@ -53,6 +103,7 @@ public func require<T>(file: FileString = #file, line: UInt = #line, _ expressio
}

/// Make an ``AsyncRequirement`` on a given actual value. The closure is lazily invoked.
@discardableResult
public func require<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T?)) -> AsyncRequirement<T> {
return AsyncRequirement(
expression: AsyncExpression(
Expand All @@ -62,8 +113,9 @@ public func require<T>(file: FileString = #file, line: UInt = #line, _ expressio
}

/// Make an ``AsyncRequirement`` on a given actual value. The value given is lazily evaluated.
/// This is provided to avoid confusion between `require -> SyncRequirement` and `erequire -> AsyncRequirement`.
public func requireAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async -> AsyncRequirement<T> {
/// This is provided to avoid confusion between `require -> SyncRequirement` and `require -> AsyncRequirement`.
@discardableResult
public func requirea<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async -> AsyncRequirement<T> {
return AsyncRequirement(
expression: AsyncExpression(
expression: expression,
Expand All @@ -73,7 +125,8 @@ public func requireAsync<T>(file: FileString = #file, line: UInt = #line, _ expr

/// Make an ``AsyncRequirement`` on a given actual value. The closure is lazily invoked.
/// This is provided to avoid confusion between `require -> SyncRequirement` and `require -> AsyncRequirement`
public func requireAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T)) async -> AsyncRequirement<T> {
@discardableResult
public func requirea<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T)) async -> AsyncRequirement<T> {
return AsyncRequirement(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -83,7 +136,8 @@ public func requireAsync<T>(file: FileString = #file, line: UInt = #line, _ expr

/// Make an ``AsyncRequirement`` on a given actual value. The closure is lazily invoked.
/// This is provided to avoid confusion between `require -> SyncRequirement` and `require -> AsyncRequirement`
public func requireAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async -> AsyncRequirement<T> {
@discardableResult
public func requirea<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async -> AsyncRequirement<T> {
return AsyncRequirement(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -95,39 +149,56 @@ public func requireAsync<T>(file: FileString = #file, line: UInt = #line, _ expr

/// 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).toNot(beNil())`
@discardableResult
public func unwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) throws -> T {
try require(file: file, line: line, expression()).toNot(beNil())
try requires(file: file, line: line, expression()).toNot(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).toNot(beNil())`
@discardableResult
public func unwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) throws -> T {
try require(file: file, line: line, expression()).toNot(beNil())
try requires(file: file, line: line, expression()).toNot(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).toNot(beNil())`
@discardableResult
public func unwraps<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () throws -> T?) throws -> T {
try requires(file: file, line: line, expression()).toNot(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).toNot(beNil())`
@discardableResult
public func unwraps<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() throws -> T?)) throws -> T {
try requires(file: file, line: line, expression()).toNot(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 `requireAsync(expression).toNot(beNil())`
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toNot(beNil())`
@discardableResult
public func unwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: @escaping () async throws -> T?) async throws -> T {
try await requireAsync(file: file, line: line, try await expression()).toNot(beNil())
try await requirea(file: file, line: line, try await expression()).toNot(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 `requireAsync(expression).toNot(beNil())`
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toNot(beNil())`
@discardableResult
public func unwrap<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T?)) async throws -> T {
try await requireAsync(file: file, line: line, expression()).toNot(beNil())
try await requirea(file: file, line: line, expression()).toNot(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 `requireAsync(expression).toNot(beNil())`
public func unwrapAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async throws -> T {
try await requireAsync(file: file, line: line, try await expression()).toNot(beNil())
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toNot(beNil())`
@discardableResult
public func unwrapa<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()).toNot(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 `requireAsync(expression).toNot(beNil())`
/// As you can tell, this is a much less verbose equivalent to `requirea(expression).toNot(beNil())`
@discardableResult
public func unwrapAsync<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async throws -> T {
try await requireAsync(file: file, line: line, expression()).toNot(beNil())
public func unwrapa<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async throws -> T {
try await requirea(file: file, line: line, expression()).toNot(beNil())
}
Loading

0 comments on commit d04e1c2

Please sign in to comment.