Skip to content

Commit 8467749

Browse files
committed
Add a property to Test.Case to get the values of its arguments.
This PR adds an experimental property to `Test.Case` to allow callers to get the test case's arguments as a type-erased array. For example: ```swift if let testCase = Test.Case.current { for value in testCase.argumentValues { print("\(value): \(type(of: value))") } } ``` We already have an `arguments` property of a type that we do not intend to make API, so the new property is named `argumentValues` pending any of SPI renaming.
1 parent c996b0a commit 8467749

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Sources/Testing/Parameterization/Test.Case.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,46 @@ extension Test {
100100
}
101101
}
102102

103+
/// The arguments passed to this test case, if any.
104+
///
105+
/// The values in this array correspond to the arguments to the test
106+
/// function of which this instance is a test case.
107+
///
108+
/// ### Passing tuples to test functions
109+
///
110+
/// If your test function takes a tuple (including a key-value pair from a
111+
/// dictionary) as its input and maps it to more than one argument, the
112+
/// value of this property represents each member of the tuple as a separate
113+
/// argument. For example, given the following test function:
114+
///
115+
/// ```swift
116+
/// @Test(arguments: [(Food.burger, Temperature.hot), (.iceCream, .cold),
117+
/// (.burrito, .hot), (.popsicle, .cold)])
118+
/// func `Serving temperature for food`(food: Food, temp: Temperature) {
119+
/// // ...
120+
/// }
121+
/// ```
122+
///
123+
/// The value of this property will be an array with two elements. The first
124+
/// element in the array will be an instance of `Food` and the second
125+
/// element will be an instance of `Temperature`.
126+
///
127+
/// ### Test functions with no arguments
128+
///
129+
/// If your test function is not parameterized, the testing library assigns
130+
/// it a single test case and the value of this property for that test case
131+
/// is the empty array.
132+
@_spi(Experimental)
133+
@_unavailableInEmbedded
134+
public var argumentValues: [any Sendable] {
135+
switch _kind {
136+
case .nonParameterized:
137+
[]
138+
case let .parameterized(arguments, _, _):
139+
arguments.map(\.value)
140+
}
141+
}
142+
103143
/// A number used to distinguish this test case from others associated with
104144
/// the same parameterized test function whose arguments have the same ID.
105145
///

0 commit comments

Comments
 (0)