Skip to content

Commit

Permalink
#160 Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Nov 27, 2022
1 parent d49f995 commit 1405163
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -319,7 +319,7 @@ test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fini
```

Note that the names of the values _try_ to convert the input expression in a
rust valid name to help you to identify what tests fail.
Rust valid identifier name to help you to find wich tests fail.

## More

Expand Down
59 changes: 7 additions & 52 deletions playground/src/main.rs
@@ -1,57 +1,12 @@
use rstest::*;
use rstest_reuse::{self, *};

// 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(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),
// 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) {
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) {
assert!(a != b);
}

#[fixture(a = 42)]
fn f(a: u32) -> u32 {
a
fn valid_user(name: &str, age: u8) -> bool {
true
}

#[fixture(f(42))]
fn fix(f: u32) -> u32 {
f
}

#[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());
fn should_accept_all_corner_cases(
#[values("J", "A", "A________________________________________21")] name: &str,
#[values(14, 100)] age: u8,
) {
assert!(valid_user(name, age))
}
20 changes: 11 additions & 9 deletions rstest_macros/src/lib.rs
Expand Up @@ -635,16 +635,18 @@ pub fn fixture(
/// where `cargo test` output is
///
/// ```text
/// running 6 tests
/// test should_accept_all_corner_cases::name_1::age_1 ... ok
/// test should_accept_all_corner_cases::name_3::age_1 ... ok
/// test should_accept_all_corner_cases::name_3::age_2 ... ok
/// test should_accept_all_corner_cases::name_2::age_1 ... ok
/// test should_accept_all_corner_cases::name_2::age_2 ... ok
/// test should_accept_all_corner_cases::name_1::age_2 ... ok
/// test should_accept_all_corner_cases::name_1___J__::age_2_100 ... ok
/// test should_accept_all_corner_cases::name_2___A__::age_1_14 ... ok
/// test should_accept_all_corner_cases::name_2___A__::age_2_100 ... ok
/// test should_accept_all_corner_cases::name_3___A________________________________________21__::age_2_100 ... ok
/// test should_accept_all_corner_cases::name_3___A________________________________________21__::age_1_14 ... ok
/// test should_accept_all_corner_cases::name_1___J__::age_1_14 ... ok
///
/// test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
/// test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
/// ```
/// Note that the test names contains the given expression sanitized into
/// a valid Rust identifier name. This should help to identify wich case fails.
///
///
/// 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.
Expand Down Expand Up @@ -843,7 +845,7 @@ pub fn fixture(
/// fn should_be_invalid_query_error(
/// repository: impl Repository,
/// #[case] user: User,
/// #[values(" ", "^%$#@!", "....")]
/// #[values(" ", "^%$some#@invalid!chars", ".n.o.d.o.t.s.")] query: &str,
/// query: &str
/// ) {
/// repository.find_items(&user, query).unwrap();
Expand Down

0 comments on commit 1405163

Please sign in to comment.