Skip to content

Commit

Permalink
#111 Documentation and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Apr 25, 2021
1 parent f2b0dd4 commit a8a110c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
@@ -1,9 +1,12 @@
# Changelog

## [0.7.1] Unreleased
## [0.8.0] Unreleased

### Add

- Magic Conversion: use literal string for define values where type implements
`FromStr` trait (See #111)

### Changed

### Fixed
Expand Down
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -117,6 +117,23 @@ fn it_works(a: u32, b: u32) {

See [`rstest_reuse`][reuse-crate-link] for more dettails.

### Magic Conversion

If you need a value where it's type implement `FromStr()` trait you can assign it
by just use literl string.

```rust
# use rstest::rstest;
# use std::net::SocketAddr;
#[rstest]
#[case("1.2.3.4:8080", 8080)]
#[case("127.0.0.1:9000", 9000)]
fn check_port(#[case] addr: SocketAddr, #[case] expected: u16) {
assert_eq!(expected, addr.port());
}
```
You can use this feature also in value list and in fixture default value.

### Async

`rstest` provides out of the box `async` support. Just mark your
Expand Down
26 changes: 17 additions & 9 deletions playground/src/main.rs
@@ -1,39 +1,38 @@
use rstest::*;
use rstest_reuse::{self, *};

// Here we define the template. This define
// Here we define the template. This define
// * The test set name to `two_simple_cases`
// * cases: here two cases that feed the `a`, `b` values
#[template]
#[rstest]
#[case(2, 2)]
#[case(2, 2)]
#[case(4/2, 2)]
fn two_simple_cases(#[case] a: u32, #[case] b: u32) {}

// Here we apply the `two_simple_cases` template: That is expanded in
// #[template]
// #[rstest(a, b,
// case(2, 2),
// #[rstest(a, b,
// case(2, 2),
// case(4/2, 2),
// )
// ]
// fn it_works(a: u32, b: u32) {
// assert!(a == b);
// }
#[apply(two_simple_cases)]
fn it_works(#[case] a: u32,#[case] b: u32) {
fn it_works(#[case] a: u32, #[case] b: u32) {
assert!(a == b);
}


// Here we reuse the `two_simple_cases` template to create two other tests
#[apply(two_simple_cases)]
#[should_panic]
fn it_fail(#[case] a: u32,#[case] b: u32) {
fn it_fail(#[case] a: u32, #[case] b: u32) {
assert!(a != b);
}

#[fixture(a=42)]
#[fixture(a = 42)]
fn f(a: u32) -> u32 {
a
}
Expand All @@ -46,4 +45,13 @@ fn fix(f: u32) -> u32 {
#[rstest]
fn aaa(fix: u32) {
assert_eq!(42, fix);
}
}

use std::net::SocketAddr;

#[rstest]
#[case("1.2.3.4:8080", 8080)]
#[case("127.0.0.1:9000", 9000)]
fn check_port(#[case] addr: SocketAddr, #[case] expected: u16) {
assert_eq!(expected, addr.port());
}
62 changes: 62 additions & 0 deletions src/lib.rs
Expand Up @@ -210,6 +210,22 @@
//!
//! This will generate a test for each combination of `state` and `event`.
//!
//! ## Magic Conversion
//!
//! If you need a value where it's type implement `FromStr()` trait you
//! can assign it by just use literl string.
//!
//! ```
//! # use rstest::rstest;
//! # use std::net::SocketAddr;
//! #[rstest]
//! #[case("1.2.3.4:8080", 8080)]
//! #[case("127.0.0.1:9000", 9000)]
//! fn check_port(#[case] addr: SocketAddr, #[case] expected: u16) {
//! assert_eq!(expected, addr.port());
//! }
//! ```
//! You can use this feature also in value list and in fixture default value.

#![cfg_attr(use_proc_macro_diagnostic, feature(proc_macro_diagnostic))]
extern crate proc_macro;
Expand Down Expand Up @@ -284,6 +300,23 @@ use quote::ToTokens;
/// ```
/// The `expression` could be any valid rust expression, even an `async` block if you need.
///
/// If the type implements `FromStr` trait you can define it with a literal string.
///
/// ```
/// # use rstest::*;
/// # use std::net::SocketAddr;
/// # struct DbConnection {}
/// #[fixture]
/// fn db_connection(
/// #[default = "127.0.0.1:9000"]
/// addr: SocketAddr
/// ) -> DbConnection {
/// // create connection
/// # DbConnection{}
/// }
/// ```
///
/// In this case is not so useful but
/// # Async
///
/// If you need you can write `async` fixtures to use in your `async` tests. Simply use `async`
Expand Down Expand Up @@ -582,6 +615,23 @@ pub fn fixture(
/// }
/// ```
///
/// ### Magic Conversion
///
/// You can use the magic conversion feature every time you would define a varible
/// where it's type define `FromStr` trat (so you can parse a string to build it).
///
/// ```
/// # use rstest::rstest;
/// # use std::path::PathBuf;
/// # fn count_words(path: PathBuf) -> usize {0}
/// #[rstest]
/// #[case("resources/empty", 0)]
/// #[case("resources/divine_commedy", 101.698)]
/// fn test_count_words(#[case] path: PathBuf, #[case] expected: usize) {
/// assert_eq!(expected, count_words(path))
/// }
/// ```
///
/// ### Optional case description
///
/// Optionally you can give a _description_ to every case simple by follow `case`
Expand Down Expand Up @@ -723,12 +773,24 @@ pub fn fixture(
/// test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
/// ```
///
/// Also value list implements the magic conversion feature: every time the value type
/// implements `FromStr` trait you can use a literal string to define it.
///
/// ## Use Parametrize definition in more tests
///
/// If you need to use a test list for more than one test you can use
/// [`rstest_reuse`](https://crates.io/crates/rstest_reuse) crate.
/// With this helper crate you can define a template and use it everywhere.
///
/// ```
/// # use rstest::rstest;
/// # use std::net::SocketAddr;
/// #[rstest]
/// fn given_port(#[values("1.2.3.4:8000", "4.3.2.1:8000", "127.0.0.1:8000")] addr: SocketAddr) {
/// assert_eq(8000, addr.port())
/// }
/// ```
///
/// ```rust,ignore
/// use rstest::rstest;
/// use rstest_reuse::{self, *};
Expand Down

0 comments on commit a8a110c

Please sign in to comment.