Skip to content

Commit

Permalink
chore: add examples to README
Browse files Browse the repository at this point in the history
  • Loading branch information
open-schnick committed Nov 12, 2023
1 parent 6405861 commit ff47192
Showing 1 changed file with 65 additions and 7 deletions.
72 changes: 65 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ After that various assertions based on the type of the asserted value can be mad

### Basic value assertions

There are several assertions available for all types
There are several assertions available for all types:

#### Equality

[`AssertionBuilder::<T>.equals()`] and [`not_equals`] take anything that can be converted to the asserted type to improve ergonimics.
This is done by implementing the [Into] trait
[equals](struct.AssertionBuilder.html#method.equals) and [not_equals](struct.AssertionBuilder.html#method.not_equals) take anything that can be converted to the asserted type to improve ergonimics.
This is done by implementing the [Into] trait.

```rust
# use smoothy::assert_that;
Expand All @@ -28,7 +28,7 @@ assert_that(1).not_equals(2);
assert_that(String::from("Hello")).not_equals("Hello There");
```

Same for [try_equals] and [try_not_equals] but here the trait [TryInto] is used
Same for [try_equals](struct.AssertionBuilder.html#method.try_equals) and [try_not_equals](struct.AssertionBuilder.html#method.try_not_equals) but here the trait [TryInto] is used.

```rust
# use smoothy::assert_that;
Expand All @@ -37,16 +37,74 @@ assert_that(1u8).try_equals(1i8);

```rust
# use smoothy::assert_that;
assert_that(1u8).try_not_equals(2i8);
assert_that(1u8).not_try_equals(2i8);
```

### Results

Results can be asserted by calling `is_err` or `is_ok`. Futhermore the actual result or the error can be asserted
Results can be asserted by calling [is_err](struct.AssertionBuilder.html#method.is_err) or [is_ok](struct.AssertionBuilder.html#method.is_ok).
Futhermore their actual content can be asserted aswell.

#### Ok

Basic assertion that the result is [Ok]:

```rust
# use smoothy::assert_that;
let result: Result<u8, ()> = Ok(1);
assert_that(result).is_ok();
```

Asserting the [Ok]-value:

```rust
# use smoothy::assert_that;
let result: Result<u8, ()> = Ok(1);
assert_that(result).is_ok().and_value_equals(1);
```

#### Err

Basic assertion that the result is [Err]:

```rust
# use smoothy::assert_that;
let result: Result<(), String> = Err(String::from("Oh no!"));
assert_that(result).is_err();
```

When the [Err]-value implements [PartialEq] one can use [and_error_equals](struct.ErrAsserter.html#method.and_error_equals)

```rust
# use smoothy::assert_that;
#[derive(Debug, PartialEq)]
struct CustomError(String);

let result: Result<(), CustomError> = Err(CustomError(String::from("Oh no!")));
assert_that(result).is_err().and_error_equals(CustomError(String::from("Oh no!")));
```

Alternativly one can assert the error message (given the error implements [Display](std::fmt::Display)):

```rust
# use smoothy::assert_that;
# use std::fmt::{Display, Formatter};
#[derive(Debug)]
struct CustomError(String);
#
# impl Display for CustomError {
# fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
# write!(f, "{}", self.0)
# }
# }

let result: Result<(), CustomError> = Err(CustomError(String::from("Oh no!")));
assert_that(result).is_err().and_error_as_string_equals("Oh no!");
```

## TODO:

- `is` to assert equality in a type safe way (compared to the Into<T> stuff)
- `is` to assert equality in a type safe way (compared to the `Into<T>` stuff)
- vec support (length, contains)
- string support (length, contains, starts_with, ends_with)
- Documentation and testing :D (especially the differnence between `is` and `equals`)

0 comments on commit ff47192

Please sign in to comment.