Skip to content

Commit

Permalink
Use value for name #160 (#168)
Browse files Browse the repository at this point in the history
* WIP: enable check tests by partial name

* WIP: values tests name from content

* WIP: Single test check itself

* Now framework accept also contains rule

* Add ok_in and fail_in shortcut

* Fixed tests and operation characters traslated to _
  • Loading branch information
la10736 committed Nov 20, 2022
1 parent 077eeb7 commit 877eb94
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 140 deletions.
155 changes: 69 additions & 86 deletions rstest/tests/rstest/mod.rs
Expand Up @@ -100,6 +100,7 @@ fn use_mutable_fixture_in_parametric_argumnts() {
let (output, _) = run_test("use_mutable_fixture_in_parametric_argumnts.rs");

TestResults::new()
.with_contains(true)
.ok("use_mutate_fixture::case_1::b_1")
.assert(output);
}
Expand Down Expand Up @@ -171,22 +172,8 @@ mod dump_input_values {
.fail("cases_fail::case_2")
.fail("no_trace_cases_fail::case_1")
.fail("no_trace_cases_fail::case_2")
.fail("matrix_fail::u_1::s_1::t_1")
.fail("matrix_fail::u_1::s_1::t_2")
.fail("matrix_fail::u_1::s_2::t_1")
.fail("matrix_fail::u_1::s_2::t_2")
.fail("matrix_fail::u_2::s_1::t_1")
.fail("matrix_fail::u_2::s_1::t_2")
.fail("matrix_fail::u_1::s_2::t_1")
.fail("matrix_fail::u_2::s_2::t_2")
.fail("no_trace_matrix_fail::u_1::s_1::t_1")
.fail("no_trace_matrix_fail::u_1::s_1::t_2")
.fail("no_trace_matrix_fail::u_1::s_2::t_1")
.fail("no_trace_matrix_fail::u_1::s_2::t_2")
.fail("no_trace_matrix_fail::u_2::s_1::t_1")
.fail("no_trace_matrix_fail::u_2::s_1::t_2")
.fail("no_trace_matrix_fail::u_1::s_2::t_1")
.fail("no_trace_matrix_fail::u_2::s_2::t_2")
.fail_with("matrix_fail::u_1", false, 8)
.fail_with("matrix_fail::u_2", false, 8)
.assert(output);

assert_in!(out, "fu32 = 42");
Expand Down Expand Up @@ -257,7 +244,7 @@ mod dump_input_values {
TestResults::new()
.fail("simple")
.fail("cases::case_1")
.fail("matrix::a_1::b_1::dd_1")
.fail_in("matrix::a_1")
.assert(output);

assert_in!(out, "fu32 = 42");
Expand Down Expand Up @@ -745,10 +732,11 @@ mod matrix {
let (output, _) = run_test(res("simple.rs"));

TestResults::new()
.ok("strlen_test::expected_1::input_1")
.ok("strlen_test::expected_1::input_2")
.ok("strlen_test::expected_2::input_1")
.ok("strlen_test::expected_2::input_2")
.with_contains(true)
.ok("strlen_test::expected_1_4::input_1___ciao__")
.ok("strlen_test::expected_1_4::input_2___buzz__")
.ok("strlen_test::expected_2_2_3_2::input_1___ciao__")
.ok("strlen_test::expected_2_2_3_2::input_2___buzz__")
.assert(output);
}

Expand All @@ -757,34 +745,26 @@ mod matrix {
let (output, _) = run_test(res("partial.rs"));

TestResults::new()
.ok("default::a_1::b_1")
.ok("default::a_1::b_2")
.ok("default::a_2::b_1")
.ok("partial_2::a_2::b_2")
.ok("partial_attr_2::a_2::b_2")
.ok("complete::a_2::b_2")
.ok("complete_attr::a_2::b_2")
.fail("default::a_2::b_2")
.fail("partial_1::a_1::b_1")
.fail("partial_1::a_1::b_2")
.fail("partial_1::a_2::b_1")
.fail("partial_1::a_2::b_2")
.fail("partial_2::a_1::b_1")
.fail("partial_2::a_1::b_2")
.fail("partial_2::a_2::b_1")
.fail("complete::a_1::b_1")
.fail("complete::a_1::b_2")
.fail("complete::a_2::b_1")
.fail("partial_attr_1::a_1::b_1")
.fail("partial_attr_1::a_1::b_2")
.fail("partial_attr_1::a_2::b_1")
.fail("partial_attr_1::a_2::b_2")
.fail("partial_attr_2::a_1::b_1")
.fail("partial_attr_2::a_1::b_2")
.fail("partial_attr_2::a_2::b_1")
.fail("complete_attr::a_1::b_1")
.fail("complete_attr::a_1::b_2")
.fail("complete_attr::a_2::b_1")
.with_contains(true)
.ok_times("default::a_1", 2)
.ok("default::a_2")
.ok("partial_2::a_2")
.ok("partial_attr_2::a_2")
.ok("complete::a_2")
.ok("complete_attr::a_2")
.fail("default::a_2")
.fail_times("partial_1::a_1", 2)
.fail_times("partial_1::a_2", 2)
.fail_times("partial_2::a_1", 2)
.fail("partial_2::a_2")
.fail_times("complete::a_1", 2)
.fail("complete::a_2")
.fail_times("partial_attr_1::a_1", 2)
.fail_times("partial_attr_1::a_2", 2)
.fail_times("partial_attr_2::a_1", 2)
.fail("partial_attr_2::a_2")
.fail_times("complete_attr::a_1", 2)
.fail("complete_attr::a_2")
.assert(output);
}

Expand All @@ -796,10 +776,11 @@ mod matrix {
let output = prj.run_tests().unwrap();

TestResults::new()
.ok("my_async_test::first_1::second_1")
.fail("my_async_test::first_1::second_2")
.fail("my_async_test::first_2::second_1")
.ok("my_async_test::first_2::second_2")
.with_contains(true)
.ok("my_async_test::first_1")
.fail("my_async_test::first_1")
.fail("my_async_test::first_2")
.ok("my_async_test::first_2")
.assert(output);
}

Expand All @@ -811,14 +792,15 @@ mod matrix {
let output = prj.run_tests().unwrap();

TestResults::new()
.ok("sync::first_1::second_1")
.fail("sync::first_1::second_2")
.fail("sync::first_2::second_1")
.ok("sync::first_2::second_2")
.ok("fn_async::first_1::second_1")
.fail("fn_async::first_1::second_2")
.fail("fn_async::first_2::second_1")
.ok("fn_async::first_2::second_2")
.with_contains(true)
.ok("sync::first_1")
.fail("sync::first_1")
.fail("sync::first_2")
.ok("sync::first_2")
.ok("fn_async::first_1")
.fail("fn_async::first_1")
.fail("fn_async::first_2")
.ok("fn_async::first_2")
.assert(output);
}

Expand All @@ -827,18 +809,18 @@ mod matrix {
let (output, _) = run_test(res("use_attr.rs"));

TestResults::new()
.ok("both::expected_1::input_1")
.ok("both::expected_1::input_2")
.ok("both::expected_2::input_1")
.ok("both::expected_2::input_2")
.ok("first::input_1::expected_1")
.ok("first::input_2::expected_1")
.ok("first::input_1::expected_2")
.ok("first::input_2::expected_2")
.ok("second::expected_1::input_1")
.ok("second::expected_1::input_2")
.ok("second::expected_2::input_1")
.ok("second::expected_2::input_2")
.ok("both::expected_1_4::input_1___ciao__")
.ok("both::expected_1_4::input_2___buzz__")
.ok("both::expected_2_2_3_2::input_1___ciao__")
.ok("both::expected_2_2_3_2::input_2___buzz__")
.ok("first::input_1___ciao__::expected_1_4")
.ok("first::input_2___buzz__::expected_1_4")
.ok("first::input_1___ciao__::expected_2_2_3_2")
.ok("first::input_2___buzz__::expected_2_2_3_2")
.ok("second::expected_1_4::input_1___ciao__")
.ok("second::expected_1_4::input_2___buzz__")
.ok("second::expected_2_2_3_2::input_1___ciao__")
.ok("second::expected_2_2_3_2::input_2___buzz__")
.assert(output);
}
}
Expand All @@ -859,11 +841,11 @@ fn convert_string_literal() {
.ok("cases::case_4")
.fail("cases::case_5")
.fail("cases::case_6")
.ok("values::addr_1")
.ok("values::addr_2")
.fail("values::addr_3")
.fail("values::addr_4")
.ok("not_convert_byte_array::case_1::values_1")
.ok_in("values::addr_1")
.ok_in("values::addr_2")
.fail_in("values::addr_3")
.fail_in("values::addr_4")
.ok_in("not_convert_byte_array::case_1::values_1")
.ok("not_convert_impl::case_1")
.ok("not_convert_generics::case_1")
.ok("not_convert_generics::case_2")
Expand All @@ -877,14 +859,14 @@ fn happy_path() {
let (output, _) = run_test("happy_path.rs");

TestResults::new()
.ok("happy::case_1::expected_1::input_1")
.ok("happy::case_1::expected_1::input_2")
.ok("happy::case_1::expected_2::input_1")
.ok("happy::case_1::expected_2::input_2")
.ok("happy::case_2_second::expected_1::input_1")
.ok("happy::case_2_second::expected_1::input_2")
.ok("happy::case_2_second::expected_2::input_1")
.ok("happy::case_2_second::expected_2::input_2")
.ok("happy::case_1::expected_1_4::input_1___ciao__")
.ok("happy::case_1::expected_1_4::input_2___buzz__")
.ok("happy::case_1::expected_2_2_3_2::input_1___ciao__")
.ok("happy::case_1::expected_2_2_3_2::input_2___buzz__")
.ok("happy::case_2_second::expected_1_4::input_1___ciao__")
.ok("happy::case_2_second::expected_1_4::input_2___buzz__")
.ok("happy::case_2_second::expected_2_2_3_2::input_1___ciao__")
.ok("happy::case_2_second::expected_2_2_3_2::input_2___buzz__")
.assert(output);
}

Expand All @@ -905,6 +887,7 @@ fn ignore_underscore_args() {
let (output, _) = run_test("ignore_args.rs");

TestResults::new()
.with_contains(true)
.ok("test::case_1::_ignore3_1")
.ok("test::case_1::_ignore3_2")
.ok("test::case_1::_ignore3_3")
Expand Down
1 change: 1 addition & 0 deletions rstest_macros/Cargo.toml
Expand Up @@ -25,6 +25,7 @@ cfg-if = "1.0.0"
proc-macro2 = "1.0.27"
quote = "1.0.9"
syn = {version = "1.0.72", features = ["full", "parsing", "extra-traits", "visit", "visit-mut"]}
unicode-ident = "1.0.5"

[dev-dependencies]
actix-rt = "2.2.0"
Expand Down
53 changes: 51 additions & 2 deletions rstest_macros/src/render/mod.rs
Expand Up @@ -8,7 +8,8 @@ use syn::token::Async;
use proc_macro2::{Span, TokenStream};
use syn::{parse_quote, Attribute, Expr, FnArg, Ident, ItemFn, Path, ReturnType, Stmt};

use quote::{format_ident, quote};
use quote::{format_ident, quote, ToTokens};
use unicode_ident::is_xid_continue;

use crate::utils::attr_ends_with;
use crate::{
Expand Down Expand Up @@ -92,8 +93,9 @@ impl ValueList {
) -> impl Iterator<Item = (String, Box<(&'a dyn Resolver, (String, Expr))>)> + 'a {
let max_len = self.values.len();
self.values.iter().enumerate().map(move |(index, expr)| {
let sanitized_expr = sanitize_ident(expr);
let name = format!(
"{}_{:0len$}",
"{}_{:0len$}_{sanitized_expr:.64}",
self.arg,
index + 1,
len = max_len.display_len()
Expand Down Expand Up @@ -398,3 +400,50 @@ fn cases_data(
}
})
}

fn sanitize_ident(expr: &Expr) -> String {
expr.to_token_stream()
.to_string()
.chars()
.filter(|c| !c.is_whitespace())
.map(|c| match c {
'"' | '\'' => "__".to_owned(),
':' | '(' | ')' | '{' | '}' | '[' | ']' | ',' | '.' | '*' | '+' | '/' | '-' | '%'
| '^' | '!' | '&' | '|' => "_".to_owned(),
_ => c.to_string(),
})
.collect::<String>()
.chars()
.filter(|&c| is_xid_continue(c))
.collect()
}

#[cfg(test)]
mod tests {
use crate::test::ToAst;

use super::*;
use crate::test::{assert_eq, *};

#[rstest]
#[case("1", "1")]
#[case(r#""1""#, "__1__")]
#[case(r#"Some::SomeElse"#, "Some__SomeElse")]
#[case(r#""minnie".to_owned()"#, "__minnie___to_owned__")]
#[case(
r#"vec![1 , 2,
3]"#,
"vec__1_2_3_"
)]
#[case(
r#"some_macro!("first", {second}, [third])"#,
"some_macro____first____second___third__"
)]
#[case(r#"'x'"#, "__x__")]
#[case::ops(r#"a*b+c/d-e%f^g"#, "a_b_c_d_e_f_g")]
fn sanitaze_ident_name(#[case] expression: impl AsRef<str>, #[case] expected: impl AsRef<str>) {
let expression: Expr = expression.as_ref().ast();

assert_eq!(expected.as_ref(), sanitize_ident(&expression));
}
}
22 changes: 14 additions & 8 deletions rstest_macros/src/render/test.rs
Expand Up @@ -1244,7 +1244,10 @@ mod matrix_cases_should {
.map(|i| format!("{}_{}", names[0], i))
.collect::<Vec<_>>();

assert_eq!(expected, modules);
assert_eq!(expected.len(), modules.len());
for (e, m) in expected.into_iter().zip(modules.into_iter()) {
assert_in!(m, e);
}
}

#[test]
Expand Down Expand Up @@ -1289,7 +1292,10 @@ mod matrix_cases_should {
.map(|i| format!("{}_{}", names[1], i))
.collect::<Vec<_>>();

assert_eq!(expected, tests);
assert_eq!(expected.len(), tests.len());
for (e, m) in expected.into_iter().zip(tests.into_iter()) {
assert_in!(m, e);
}
}
}

Expand Down Expand Up @@ -1356,18 +1362,18 @@ mod matrix_cases_should {

let mods = tg.get_modules().names();

assert_eq!(mods[0], "first_001");
assert_eq!(mods[99], "first_100");
assert_in!(mods[0], "first_001");
assert_in!(mods[99], "first_100");

let mods = tg.get_modules()[0].get_modules().names();

assert_eq!(mods[0], "second_01");
assert_eq!(mods[9], "second_10");
assert_in!(mods[0], "second_01");
assert_in!(mods[9], "second_10");

let functions = tg.get_modules()[0].get_modules()[1].get_tests().names();

assert_eq!(functions[0], "third_1");
assert_eq!(functions[1], "third_2");
assert_in!(functions[0], "third_1");
assert_in!(functions[1], "third_2");
}
}

Expand Down
1 change: 1 addition & 0 deletions rstest_test/Cargo.toml
Expand Up @@ -19,4 +19,5 @@ toml_edit = "0.15.0"

[dev-dependencies]
lazy_static = "1.4.0"
rstest = {version = "0.15.0", default-features = false}
temp_testdir = "0.2.3"

0 comments on commit 877eb94

Please sign in to comment.