Function XCTAssertDifference(_:_:operation:changes:file:line:)
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
public func XCTAssertDifference<T>(
_ expression: @autoclosure () throws -> T,
_ message: @autoclosure () -> String = "",
operation: () throws -> Void = {},
changes updateExpectingResult: (inout T) throws -> Void,
file: StaticString = #filePath,
line: UInt = #line
) where T: Equatable
Asserts that a value has a set of changes.
This function evaluates a given expression before and after a given operation and then compares
the results. The comparison is done by invoking the changes
closure with a mutable version of
the initial value, and then asserting that the modifications made match the final value using
XCTAssertNoDifference
.
For example, given a very simple counter structure, we can write a test against its incrementing functionality: `
struct Counter {
var count = 0
var isOdd = false
mutating func increment() {
self.count += 1
self.isOdd.toggle()
}
}
var counter = Counter()
XCTAssertDifference(counter) {
counter.increment()
} changes: {
$0.count = 1
$0.isOdd = true
}
If the changes
does not exhaustively describe all changed fields, the assertion will fail.
By omitting the operation you can write a "non-exhaustive" assertion against a value by
describing just the fields you want to assert against in the changes
closure:
counter.increment()
XCTAssertDifference(counter) {
$0.count = 1
// Don't need to further describe how `isOdd` has changed
}
Parameters
Name | Type | Description |
---|---|---|
expression | @autoclosure () throws -> T |
An expression that is evaluated before and after |
message | @autoclosure () -> String |
An optional description of a failure. |
operation | () throws -> Void |
An optional operation that is performed in between an initial and final evaluation of |
updateExpectingResult | (inout T) throws -> Void |
A closure that asserts how the expression changed by supplying a mutable version of the initial value. This value must be modified to match the final value. |