Skip to content

Expand parameterized testing documentation to mention try/await support and showcase helper pattern #1133

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

Merged
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
30 changes: 22 additions & 8 deletions Sources/Testing/Test+Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ public macro Test<C>(
/// - collection: A collection of values to pass to the associated test
/// function.
///
/// During testing, the associated test function is called once for each element
/// in `collection`.
/// You can prefix the expression you pass to `collection` with `try` or `await`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly here, but do we need to repeat this for each overload or can we just document it in the article?

/// The testing library evaluates the expression lazily only if it determines
/// that the associated test will run. During testing, the testing library calls
/// the associated test function once for each element in `collection`.
///
/// @Comment {
/// - Bug: The testing library should support variadic generics.
Expand Down Expand Up @@ -270,7 +272,10 @@ extension Test {
/// - collection1: A collection of values to pass to `testFunction`.
/// - collection2: A second collection of values to pass to `testFunction`.
///
/// During testing, the associated test function is called once for each pair of
/// You can prefix the expressions you pass to `collection1` or `collection2`
/// with `try` or `await`. The testing library evaluates the expressions lazily
/// only if it determines that the associated test will run. During testing, the
/// testing library calls the associated test function once for each pair of
/// elements in `collection1` and `collection2`.
///
/// @Comment {
Expand Down Expand Up @@ -298,7 +303,10 @@ public macro Test<C1, C2>(
/// - collection1: A collection of values to pass to `testFunction`.
/// - collection2: A second collection of values to pass to `testFunction`.
///
/// During testing, the associated test function is called once for each pair of
/// You can prefix the expressions you pass to `collection1` or `collection2`
/// with `try` or `await`. The testing library evaluates the expressions lazily
/// only if it determines that the associated test will run. During testing, the
/// testing library calls the associated test function once for each pair of
/// elements in `collection1` and `collection2`.
///
/// @Comment {
Expand All @@ -324,8 +332,11 @@ public macro Test<C1, C2>(
/// - zippedCollections: Two zipped collections of values to pass to
/// `testFunction`.
///
/// During testing, the associated test function is called once for each element
/// in `zippedCollections`.
/// You can prefix the expression you pass to `zippedCollections` with `try` or
/// `await`. The testing library evaluates the expression lazily only if it
/// determines that the associated test will run. During testing, the testing
/// library calls the associated test function once for each element in
/// `zippedCollections`.
///
/// @Comment {
/// - Bug: The testing library should support variadic generics.
Expand All @@ -352,8 +363,11 @@ public macro Test<C1, C2>(
/// - zippedCollections: Two zipped collections of values to pass to
/// `testFunction`.
///
/// During testing, the associated test function is called once for each element
/// in `zippedCollections`.
/// You can prefix the expression you pass to `zippedCollections` with `try` or
/// `await`. The testing library evaluates the expression lazily only if it
/// determines that the associated test will run. During testing, the testing
/// library calls the associated test function once for each element in
/// `zippedCollections`.
///
/// @Comment {
/// - Bug: The testing library should support variadic generics.
Expand Down
31 changes: 31 additions & 0 deletions Sources/Testing/Testing.docc/ParameterizedTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,37 @@ func makeLargeOrder(count: Int) async throws {
- Note: Very large ranges such as `0 ..< .max` may take an excessive amount of
time to test, or may never complete due to resource constraints.

### Pass the same arguments to multiple test functions

If you want to pass the same collection of arguments to two or more
parameterized test functions, you can extract the arguments to a separate
function or property and pass it to each `@Test` attribute. For example:

```swift
extension Food {
static var bestSelling: [Food] {
get async throws { /* ... */ }
}
}

@Test(arguments: try await Food.bestSelling)
func `Order entree`(food: Food) {
let foodTruck = FoodTruck()
#expect(foodTruck.order(food))
}

@Test(arguments: try await Food.bestSelling)
func `Package leftovers`(food: Food) throws {
let foodTruck = FoodTruck()
let container = try #require(foodTruck.container(fitting: food))
try container.add(food)
}
```

> Tip: You can prefix expressions passed to `arguments:` with `try` or `await`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use tips otherwise in our documentation? I don't mind starting to use them, but I would like to get guidelines for when to use them instead of other callouts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamleeg Any advice?

> The testing library evaluates them lazily only if it determines that the
> associated test will run.

### Test with more than one collection

It's possible to test more than one collection. Consider the following test
Expand Down