Skip to content

Commit

Permalink
Correct a bunch of examples in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed May 21, 2018
1 parent 084cc22 commit d8ca2e5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 61 deletions.
20 changes: 8 additions & 12 deletions docs/user-guide/assertion-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ expect(subject)
Produces the output:

```
▼ Expect that "covfefe"
✓ is a java.lang.String
✗ has length 1
• found 7
Expect that "covfefe" (1 failure)
has length 1 : found 7
```

Notice that the `isUpperCase()` assertion is not applied as the earlier `hasLength(1)` assertion failed.
Expand All @@ -50,11 +48,9 @@ expect(subject) {
Produces the output:

```
▼ Expect that "covfefe"
✓ is a java.lang.String
✗ has length 1
• found 7
✗ is upper case
Expect that "covfefe" (2 failures)
has length 1 : found 7
is upper case
```

All assertions are applied and since two fail there are two errors logged.
Expand All @@ -74,9 +70,9 @@ expect(subject) {
Produces the output:

```
Expect that 1
is less than 1
is greater than 1
Expect that 1
is less than 1 : found 1
is greater than 1 : found 1
```

Note the `isA<Int>` assertion (that would have failed) was not evaluated since it was chained after `lessThan(1)` which failed.
Expand Down
17 changes: 6 additions & 11 deletions docs/user-guide/common-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ expect(subject).all {
This produces the output:

```
▼ Expect that [catflap, rubberplant, marzipan]
✗ all elements match:
▼ "catflap"
✓ starts with 'c'
✓ is lower case
▼ "rubberplant"
✗ starts with 'c'
✓ is lower case
▼ "marzipan"
✗ starts with 'c'
✓ is lower case
Expect that [catflap, rubberplant, marzipan] (1 failure)
all elements match: (2 failures)
Expect that "rubberplant" (1 failure)
starts with 'c'
Expect that "marzipan" (1 failure)
starts with 'c'
```

The results are broken down by individual elements in the collection so it's easy to see which failed.
Expand Down
46 changes: 20 additions & 26 deletions docs/user-guide/custom-assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Breaking this down:
If this assertion fails it will produce a message like:

```
Expect that 2018-05-01
is St. Tib's Day
Expect that 2018-05-01 (1 failure)
is St. Tib's Day
```

!!! info
Expand All @@ -53,17 +53,19 @@ fun Assertion<LocalDate>.isStTibsDay(): Assertion<LocalDate> =
assert("is St. Tib's Day") {
when (MonthDay.from(subject)) {
MonthDay.of(2, 29) -> pass()
else -> fail("in fact it is %s", subject)
else -> fail(
message = "in fact it is %s",
actual = subject
)
}
}
```

Now if the assertion fails there is a little more detail.

```
▼ Expect that 2018-05-01
✗ is St. Tib's Day
• in fact it is 2018-05-01
Expect that 2018-05-01
is St. Tib's Day : in fact it is 2018-05-01
```

In this case that's not terribly helpful but when dealing with properties, method return values, or the like it can save a lot of effort in identifying the precise cause of an error.
Expand Down Expand Up @@ -97,39 +99,31 @@ Imagine we're creating an assertion function that tests fails if any element of

```kotlin
fun <T: Iterable<E?>, E> Assertion<T>.containsNoNullElements(): Assertion<T> =
assert("does not contain any null elements") {
compose {
subject.forEach {
expect(it).isNotNull()
}
} results {
if (allPassed) pass() else fail()
compose("does not contain any null elements") {
subject.forEach {
expect(it).isNotNull()
}
} then {
if (allPassed) pass() else fail()
}
```

Breaking this down:

1. We declare the overall assertion function applies to an `Iterable` of a nullable element type `E`.
2. We use the `assert` method to create the overall assertion with a description as usual.
3. Inside the block passed to `compose` we make an assertion about each element of the subject.
4. Inside the results block we pass or fail the overall assertion depending on whether the nested assertions all passed.
2. We use the `compose` method instead of `assert`.
3. Inside the `compose` block we make assertions about each element of the iterable subject.
4. Inside the `then` block we pass or fail the overall assertion depending on whether the nested assertions all passed.

The receiver of the block passed to `result` has the properties `allFailed`, `anyFailed`, `allPassed` and `anyPassed` along with `pass()` and `fail()` functions similar to those used in simple assertions.

If the assertion failed we'll see something like this:

```
▼ Expect that [catflap, null, rubberplant, marzipan]
✗ does not contain any null elements:
▼ "catflap"
✓ is not null
▼ null
✗ is not null
▼ "rubberplant"
✓ is not null
▼ "marzipan"
✓ is not null
Expect that [catflap, null, rubberplant, marzipan] (1 failure)
does not contain any null elements (1 failure)
Expect that null (1 failure)
is not null
```

As well as the overall assertion failure message we get a detailed breakdown allowing us to easily find exactly where the problem is.
Expand Down
24 changes: 12 additions & 12 deletions docs/user-guide/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ This is useful for generating good quality assertion output with minimal effort.
For example, if the previous example fails it will format the error message like this:

```
Person[name: Ziggy, birthDate: 1972-06-16]
Ziggy
is equal to David
1972-06-16
1972
is equal to 1947
Person[name: Ziggy, birthDate: 1972-06-16] (2 failures)
Expect that "Ziggy" (1 failure)
is equal to "David" : found "Ziggy"
Expect that 1972-06-16 (1 failure)
Expect that 1972 (1 failure)
is equal to 1947 : found 1972
```

Using property references the output is more useful.
Expand All @@ -52,12 +52,12 @@ expect(subject) {
```

```
Person[name: Ziggy, birthDate: 1972-06-16]
.name Ziggy
is equal to David
.birthDate 1972-06-16
.year 1972
is equal to 1947
Person[name: Ziggy, birthDate: 1972-06-16] (2 failures)
.name "Ziggy" (1 failure)
is equal to "David" : found "Ziggy"
.birthDate 1972-06-16 (1 failure)
.year 1972 (1 failure)
is equal to 1947 : found 1972
```

## Creating re-usable mappings with extensions
Expand Down

0 comments on commit d8ca2e5

Please sign in to comment.