Skip to content

Commit bb20934

Browse files
authored
Update README.md for Swift Testing (#125)
1 parent f75383c commit bb20934

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ A collection of tools for debugging, diffing, and testing your application's dat
99
* [Motivation](#motivation)
1010
* [`customDump`](#customdump)
1111
* [`diff`](#diff)
12-
* [`XCTAssertNoDifference`](#xctassertnodifference)
13-
* [`XCTAssertDifference`](#xctassertdifference)
12+
* [`expectNoDifference`](#expectnodifference)
13+
* [`expectDifference`](#expectdifference)
1414
* [Customization](#customization)
1515
* [`CustomDumpStringConvertible`](#customdumpstringconvertible)
1616
* [`CustomDumpReflectable`](#customdumpreflectable)
@@ -251,43 +251,45 @@ For a real world use case we modified Apple's [Landmarks](https://developer.appl
251251
  ]
252252
```
253253

254-
### `XCTAssertNoDifference`
254+
### `expectNoDifference`
255255

256-
The `XCTAssertEqual` function from `XCTest` allows you to assert that two values are equal, and if they are not the test suite will fail with a message:
256+
XCTest's `XCTAssertEqual` and Swift Testing's `#expect(_ == _)` both allow you to assert that two values are equal, and if they are not the test suite will fail with a message:
257257

258258
```swift
259259
var other = user
260260
other.name += "!"
261261

262262
XCTAssertEqual(user, other)
263+
#expect(user == other)
263264
```
264265
```text
265266
XCTAssertEqual failed: ("User(favoriteNumbers: [42, 1729], id: 2, name: "Blob")") is not equal to ("User(favoriteNumbers: [42, 1729], id: 2, name: "Blob!")")
267+
Expectation failed: (user → User(favoriteNumbers: [42, 1729], id: 2, name: "Blob")) == (other → User(favoriteNumbers: [42, 1729], id: 2, name: "Blob!"))
266268
```
267269

268-
Unfortunately this failure message is quite difficult to visually parse and understand. It takes a few moments of hunting through the message to see that the only difference is the exclamation mark at the end of the name. The problem gets worse if the type is more complex, consisting of nested structures and large collections.
270+
Unfortunately these failure messages are quite difficult to visually parse and understand. It takes a few moments of hunting through the message to see that the only difference is the exclamation mark at the end of the name. The problem gets worse if the type is more complex, consisting of nested structures and large collections.
269271

270-
This library also ships with an `XCTAssertNoDifference` function to mitigate these problems. It works like `XCTAssertEqual` except the failure message uses a nicely formatted diff to show exactly what is different between the two values:
272+
This library also ships with an `expectNoDifference` function to mitigate these problems. It works like `XCTAssertEqual` and `#expect(_ == _)` except the failure message uses a nicely formatted diff to show exactly what is different between the two values:
271273

272274
```swift
273-
XCTAssertNoDifference(user, other)
275+
expectNoDifference(user, other)
274276
```
275-
```text
276-
XCTAssertNoDifference failed: …
277+
```diff
278+
expectNoDifference failed: …
277279

278-
User(
279-
favoriteNumbers: […],
280-
id: 2,
281-
name: "Blob"
282-
+ name: "Blob!"
283-
)
280+
User(
281+
favoriteNumbers: […],
282+
id: 2,
283+
- name: "Blob"
284+
+ name: "Blob!"
285+
)
284286

285287
(First: −, Second: +)
286288
```
287289

288-
### `XCTAssertDifference`
290+
### `expectDifference`
289291

290-
This function provides the inverse of `XCTAssertNoDifference`: it asserts that a value has a set of changes by evaluating a given expression before and after a given operation and then comparing the results.
292+
This function provides the inverse of `expectNoDifference`: it asserts that a value has a set of changes by evaluating a given expression before and after a given operation and then comparing the results.
291293

292294
For example, given a very simple counter structure, we can write a test against its incrementing functionality:
293295

@@ -302,7 +304,7 @@ struct Counter {
302304
}
303305

304306
var counter = Counter()
305-
XCTAssertDifference(counter) {
307+
expectDifference(counter) {
306308
counter.increment()
307309
} changes: {
308310
$0.count = 1
@@ -316,7 +318,7 @@ By omitting the operation you can write a "non-exhaustive" assertion against a v
316318

317319
```swift
318320
counter.increment()
319-
XCTAssertDifference(counter) {
321+
expectDifference(counter) {
320322
$0.count = 1
321323
// Don't need to further describe how `isOdd` has changed
322324
}

0 commit comments

Comments
 (0)