Skip to content

Instantly share code, notes, and snippets.

@lucaswkuipers
Created February 22, 2024 07:00
Show Gist options
  • Save lucaswkuipers/b81d714179daf7903d6137379b6372c9 to your computer and use it in GitHub Desktop.
Save lucaswkuipers/b81d714179daf7903d6137379b6372c9 to your computer and use it in GitHub Desktop.
XCTestCase extension to express when a value is irrelevant to the test outcome.
import XCTest
/// An extension for `XCTestCase` to express when a value is not relevant to the test outcome.
///
/// By using the `arbitrary` method, you can indicate that a certain value provided to the test does not play a significant role in the test scenario. It helps to avoid the creation of distracting variables whose values are inconsequential, focusing on what really matters in the test. The term 'arbitrary' is chosen to signify that the value is placeholder and could be substituted for any other value without affecting the test's behavior.
///
/// ## Topics
///
/// - Reducing Noise in Tests
///
extension XCTestCase {
/// Produce an arbitrary value that does not affect the test outcome.
///
/// Use this method to indicate that the specific value is not important for your test.
/// This helps to de-emphasize the value, making tests easier to understand at a glance.
///
/// - Parameter value: The value to use as an arbitrary placeholder.
/// - Returns: The same value that was provided as the parameter.
///
/// ## Example
///
/// Instead of using a distracting constant or variable that suggests significance where there is none:
///
/// ```swift
/// let anyAge = 42
/// let person = Person(name: "Bob", age: anyAge)
///
/// person.rename(to: "Charlie")
///
/// XCTAssertEqual(person.name, "Charlie")
/// ```
///
/// Use `arbitrary` to clearly convey the insignificance of the value:
///
/// ```swift
/// let person = Person(name: "Lucas", age: arbitrary(42))
///
/// person.rename(to: "Carlos")
///
/// XCTAssertEqual(person.name, "Carlos")
/// ```
///
/// In the second example, `arbitrary(42)` makes it clear that the age value is not relevant to what is being tested.
///
func arbitrary<Value>(_ value: Value) -> Value {
value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment