diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c8991..e31dc4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Add +- Rename fixture (See #107 and #108) + ### Changed ### Fixed diff --git a/src/lib.rs b/src/lib.rs index 86a9138..8209adc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -348,6 +348,24 @@ use quote::ToTokens; /// If you need, you can use `#[future]` attribute also with an implicit lifetime reference /// because the macro will replace the implicit lifetime with an explicit one. /// +/// # Rename +/// +/// Sometimes you want to have long and descriptive name for your fixture but you prefer to use a much +/// shorter name for argument that represent it in your fixture or test. You can rename the fixture +/// using `#[from(short_name)]` attribute like following example: +/// +/// ``` +/// use rstest::*; +/// +/// #[fixture] +/// fn long_and_boring_descriptive_name() -> i32 { 42 } +/// +/// #[rstest] +/// fn the_test(#[from(long_and_boring_descriptive_name)] short: i32) { +/// assert_eq!(42, short) +/// } +/// ``` +/// /// # Partial Injection /// /// You can also partialy inject fixture dependency using `#[with(v1, v2, ..)]` attribute: @@ -376,7 +394,6 @@ use quote::ToTokens; /// attribute will inject `v1, ..., vn` expression as fixture arguments: all remaining arguments /// will be resolved as fixtures. /// -/// /// Sometimes the return type cannot be infered so you must define it: For the few times you may /// need to do it, you can use the `#[default(type)]`, `#[partial_n(type)]` function attribute /// to define it: @@ -428,6 +445,19 @@ use quote::ToTokens; /// #[fixture(twenty_one=21, two=2)] /// fn injected(twenty_one: i32, two: i32) -> i32 { twenty_one * two } /// ``` +/// +/// ## Rename +/// ``` +/// # use rstest::*; +/// #[fixture] +/// fn long_and_boring_descriptive_name() -> i32 { 42 } +/// +/// #[rstest(long_and_boring_descriptive_name as short)] +/// fn the_test(short: i32) { +/// assert_eq!(42, short) +/// } +/// ``` +/// /// ## Partial Injection /// ``` /// # use rstest::*; @@ -547,6 +577,22 @@ pub fn fixture( /// } /// ``` /// +/// If you want to use long and descriptive names for your fixture but prefer to use +/// shorter names inside your tests you use rename feature described in +/// [fixture rename](attr.fixture.html#rename): +/// +/// ``` +/// use rstest::*; +/// +/// #[fixture] +/// fn long_and_boring_descriptive_name() -> i32 { 42 } +/// +/// #[rstest] +/// fn the_test(#[from(long_and_boring_descriptive_name)] short: i32) { +/// assert_eq!(42, short) +/// } +/// ``` +/// /// Sometimes is useful to have some parametes in your fixtures but your test would /// override the fixture's default values in some cases. Like in /// [fixture partial injection](attr.fixture.html#partial-injection) you use `#[with]` @@ -1020,7 +1066,8 @@ pub fn fixture( /// - `arg_i` could be one of the follow /// - `ident` that match to one of function arguments for parametrized cases /// - `case[::description](v1, ..., vl)` a test case -/// - `fixture(v1, ..., vl)` where fixture is one of function arguments +/// - `fixture(v1, ..., vl) [as argument_name]` where fixture is the injected +/// fixture and argument_name (default use fixture) is one of function arguments /// that and `v1, ..., vl` is a partial list of fixture's arguments /// - `ident => [v1, ..., vl]` where `ident` is one of function arguments and /// `v1, ..., vl` is a list of values for ident @@ -1045,6 +1092,18 @@ pub fn fixture( /// } /// ``` /// +/// ## Fixture Rename +/// ``` +/// # use rstest::*; +/// #[fixture] +/// fn long_and_boring_descriptive_name() -> i32 { 42 } +/// +/// #[rstest(long_and_boring_descriptive_name as short)] +/// fn the_test(short: i32) { +/// assert_eq!(42, short) +/// } +/// ``` +/// /// ## Parametrized /// /// ``` diff --git a/src/parse/fixture.rs b/src/parse/fixture.rs index e76fd53..4ee3a5b 100644 --- a/src/parse/fixture.rs +++ b/src/parse/fixture.rs @@ -9,10 +9,10 @@ use syn::{ use super::{ extract_argument_attrs, extract_default_return_type, extract_defaults, extract_fixtures, extract_partials_return_type, parse_vector_trailing_till_double_comma, Attributes, - ExtendWithFunctionAttrs, Fixture, Positional, + ExtendWithFunctionAttrs, Fixture, }; -use crate::parse::Attribute; use crate::{error::ErrorsVec, refident::RefIdent, utils::attr_is}; +use crate::{parse::Attribute, utils::attr_in}; use proc_macro2::TokenStream; use quote::{format_ident, ToTokens}; @@ -76,6 +76,34 @@ impl ExtendWithFunctionAttrs for FixtureInfo { } } +fn parse_attribute_args_just_once<'a, T: Parse>( + attributes: impl Iterator, + name: &str, +) -> (Option, Vec) { + let mut errors = Vec::new(); + let val = attributes + .filter(|&a| attr_is(a, name)) + .map(|a| (a, a.parse_args::())) + .fold(None, |first, (a, res)| match (first, res) { + (None, Ok(parsed)) => Some(parsed), + (first, Err(err)) => { + errors.push(err); + first + } + (first, _) => { + errors.push(syn::Error::new_spanned( + a, + format!( + "You cannot use '{}' attribute more than once for the same argument", + name + ), + )); + first + } + }); + (val, errors) +} + /// Simple struct used to visit function attributes and extract Fixtures and /// eventualy parsing errors #[derive(Default)] @@ -83,17 +111,23 @@ pub(crate) struct FixturesFunctionExtractor(pub(crate) Vec, pub(crate) impl VisitMut for FixturesFunctionExtractor { fn visit_fn_arg_mut(&mut self, node: &mut FnArg) { - for r in extract_argument_attrs( - node, - |a| attr_is(a, "with"), - |a, name| { - a.parse_args::() - .map(|p| Fixture::new(name.clone(), p)) - }, - ) { - match r { - Ok(fixture) => self.0.push(fixture), - Err(err) => self.1.push(err), + if let FnArg::Typed(ref mut arg) = node { + let name = match arg.pat.as_ref() { + syn::Pat::Ident(ident) => ident.ident.clone(), + _ => return, + }; + let (extracted, remain): (Vec<_>, Vec<_>) = std::mem::take(&mut arg.attrs) + .into_iter() + .partition(|attr| attr_in(attr, &["with", "from"])); + arg.attrs = remain; + + let (pos, errors) = parse_attribute_args_just_once(extracted.iter(), "with"); + self.1.extend(errors.into_iter()); + let (resolve, errors) = parse_attribute_args_just_once(extracted.iter(), "from"); + self.1.extend(errors.into_iter()); + if pos.is_some() || resolve.is_some() { + self.0 + .push(Fixture::new(name, resolve, pos.unwrap_or_default())) } } } @@ -282,8 +316,8 @@ mod should { let expected = FixtureInfo { data: vec![ - fixture("my_fixture", vec!["42", r#""other""#]).into(), - fixture("other", vec!["vec![42]"]).into(), + fixture("my_fixture", &["42", r#""other""#]).into(), + fixture("other", &["vec![42]"]).into(), arg_value("value", "42").into(), arg_value("other_value", "vec![1.0]").into(), ] @@ -332,7 +366,7 @@ mod should { let data = parse_fixture(r#"my_fixture(42, "other")"#); let expected = FixtureInfo { - data: vec![fixture("my_fixture", vec!["42", r#""other""#]).into()].into(), + data: vec![fixture("my_fixture", &["42", r#""other""#]).into()].into(), ..Default::default() }; @@ -377,8 +411,8 @@ mod extend { let expected = FixtureInfo { data: vec![ - fixture("f1", vec!["2"]).into(), - fixture("f2", vec!["vec![1,2]", r#""s""#]).into(), + fixture("f1", &["2"]).into(), + fixture("f2", &["vec![1,2]", r#""s""#]).into(), ] .into(), ..Default::default() @@ -388,6 +422,36 @@ mod extend { assert_eq!(expected, info); } + #[test] + fn rename_with_attributes() { + let mut item_fn = r#" + fn test_fn( + #[from(long_fixture_name)] + #[with(42, "other")] short: u32, + #[from(simple)] + s: &str, + no_change: i32) { + } + "# + .ast(); + + let expected = FixtureInfo { + data: vec![ + fixture("short", &["42", r#""other""#]) + .with_resolve("long_fixture_name") + .into(), + fixture("s", &[]).with_resolve("simple").into(), + ] + .into(), + ..Default::default() + }; + + let mut data = FixtureInfo::default(); + data.extend_with_function_attrs(&mut item_fn).unwrap(); + + assert_eq!(expected, data); + } + #[test] fn use_default_values_attributes() { let to_parse = r#" @@ -502,6 +566,36 @@ mod extend { assert_eq!(1, errors.len()); } + #[test] + fn with_used_more_than_once() { + let mut item_fn: ItemFn = r#" + fn my_fix(#[with(1)] #[with(2)] fixture1: &str, #[with(1)] #[with(2)] #[with(3)] fixture2: &str) {} + "# + .ast(); + + let errors = FixtureInfo::default() + .extend_with_function_attrs(&mut item_fn) + .err() + .unwrap_or_default(); + + assert_eq!(3, errors.len()); + } + + #[test] + fn from_used_more_than_once() { + let mut item_fn: ItemFn = r#" + fn my_fix(#[from(a)] #[from(b)] fixture1: &str, #[from(c)] #[from(d)] #[from(e)] fixture2: &str) {} + "# + .ast(); + + let errors = FixtureInfo::default() + .extend_with_function_attrs(&mut item_fn) + .err() + .unwrap_or_default(); + + assert_eq!(3, errors.len()); + } + #[test] fn if_default_is_defined_more_than_once() { let mut item_fn: ItemFn = r#" diff --git a/src/parse/mod.rs b/src/parse/mod.rs index ed9247d..44d9a77 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -3,7 +3,7 @@ use syn::{ parse::{Parse, ParseStream}, parse_quote, punctuated::Punctuated, - token, + token::{self, Paren}, visit_mut::VisitMut, FnArg, Ident, ItemFn, Token, }; @@ -27,10 +27,10 @@ pub(crate) mod macros; pub(crate) mod expressions; pub(crate) mod fixture; +pub(crate) mod future; pub(crate) mod rstest; pub(crate) mod testcase; pub(crate) mod vlist; -pub(crate) mod future; pub(crate) trait ExtendWithFunctionAttrs { fn extend_with_function_attrs( @@ -117,7 +117,7 @@ pub(crate) fn drain_stream(input: ParseStream) { }); } -#[derive(PartialEq, Debug, Clone)] +#[derive(PartialEq, Debug, Clone, Default)] pub(crate) struct Positional(pub(crate) Vec); impl Parse for Positional { @@ -133,22 +133,44 @@ impl Parse for Positional { #[derive(PartialEq, Debug, Clone)] pub(crate) struct Fixture { pub(crate) name: Ident, + pub(crate) resolve: Option, pub(crate) positional: Positional, } impl Fixture { - pub(crate) fn new(name: Ident, positional: Positional) -> Self { - Self { name, positional } + pub(crate) fn new(name: Ident, resolve: Option, positional: Positional) -> Self { + Self { + name, + resolve, + positional, + } } } impl Parse for Fixture { fn parse(input: ParseStream) -> syn::Result { - let name = input.parse()?; - let content; - let _ = syn::parenthesized!(content in input); - let positional = content.parse()?; - Ok(Self::new(name, positional)) + let resolve = input.parse()?; + if input.peek(Paren) || input.peek(Token![as]) { + let positional = if input.peek(Paren) { + let content; + let _ = syn::parenthesized!(content in input); + content.parse()? + } else { + Default::default() + }; + + if input.peek(Token![as]) { + let _: Token![as] = input.parse()?; + Ok(Self::new(input.parse()?, Some(resolve), positional)) + } else { + Ok(Self::new(resolve, None, positional)) + } + } else { + Err(syn::Error::new( + input.span(), + "fixture need arguments or 'as new_name' format", + )) + } } } diff --git a/src/parse/rstest.rs b/src/parse/rstest.rs index e925f2e..40bb78d 100644 --- a/src/parse/rstest.rs +++ b/src/parse/rstest.rs @@ -5,7 +5,7 @@ use syn::{ use super::testcase::TestCase; use super::{ - extract_case_args, extract_cases, extract_fixtures, extract_excluded_trace, extract_value_list, + extract_case_args, extract_cases, extract_excluded_trace, extract_fixtures, extract_value_list, parse_vector_trailing_till_double_comma, Attribute, Attributes, ExtendWithFunctionAttrs, Fixture, }; @@ -280,7 +280,7 @@ mod test { let fixtures = parse_rstest_data("my_fixture(42)"); let expected = RsTestData { - items: vec![Fixture::new(ident("my_fixture"), vec![expr("42")].into()).into()], + items: vec![fixture("my_fixture", &["42"]).into()], }; assert_eq!(expected, fixtures); @@ -304,8 +304,8 @@ mod test { let expected = RsTestInfo { data: vec![ - fixture("my_fixture", vec!["42", r#""other""#]).into(), - fixture("other", vec!["vec![42]"]).into(), + fixture("my_fixture", &["42", r#""other""#]).into(), + fixture("other", &["vec![42]"]).into(), ] .into(), attributes: Attributes { @@ -320,28 +320,85 @@ mod test { assert_eq!(expected, data); } - #[test] - fn fixtures_defined_via_with_attributes() { - let mut item_fn = r#" - fn test_fn(#[with(42, "other")] my_fixture: u32, #[with(vec![42])] other: &str) { + mod fixture_extraction { + use super::{assert_eq, *}; + + #[test] + fn rename() { + let data = parse_rstest( + r#"long_fixture_name(42, "other") as short, simple as s, no_change()"#, + ); + + let expected = RsTestInfo { + data: vec![ + fixture("short", &["42", r#""other""#]) + .with_resolve("long_fixture_name") + .into(), + fixture("s", &[]).with_resolve("simple").into(), + fixture("no_change", &[]).into(), + ] + .into(), + ..Default::default() + }; + + assert_eq!(expected, data); } - "# - .ast(); - let expected = RsTestInfo { - data: vec![ - fixture("my_fixture", vec!["42", r#""other""#]).into(), - fixture("other", vec!["vec![42]"]).into(), - ] - .into(), - ..Default::default() - }; + #[test] + fn rename_with_attributes() { + let mut item_fn = r#" + fn test_fn( + #[from(long_fixture_name)] + #[with(42, "other")] short: u32, + #[from(simple)] + s: &str, + no_change: i32) { + } + "# + .ast(); - let mut data = RsTestInfo::default(); + let expected = RsTestInfo { + data: vec![ + fixture("short", &["42", r#""other""#]) + .with_resolve("long_fixture_name") + .into(), + fixture("s", &[]).with_resolve("simple").into() + ] + .into(), + ..Default::default() + }; - data.extend_with_function_attrs(&mut item_fn).unwrap(); + let mut data = RsTestInfo::default(); - assert_eq!(expected, data); + data.extend_with_function_attrs(&mut item_fn).unwrap(); + + assert_eq!(expected, data); + } + + + #[test] + fn defined_via_with_attributes() { + let mut item_fn = r#" + fn test_fn(#[with(42, "other")] my_fixture: u32, #[with(vec![42])] other: &str) { + } + "# + .ast(); + + let expected = RsTestInfo { + data: vec![ + fixture("my_fixture", &["42", r#""other""#]).into(), + fixture("other", &["vec![42]"]).into(), + ] + .into(), + ..Default::default() + }; + + let mut data = RsTestInfo::default(); + + data.extend_with_function_attrs(&mut item_fn).unwrap(); + + assert_eq!(expected, data); + } } #[test] @@ -367,7 +424,7 @@ mod test { let data = parse_rstest(r#"my_fixture(42, "other")"#); let expected = RsTestInfo { - data: vec![fixture("my_fixture", vec!["42", r#""other""#]).into()].into(), + data: vec![fixture("my_fixture", &["42", r#""other""#]).into()].into(), ..Default::default() }; @@ -437,7 +494,7 @@ mod test { let fixtures = data.fixtures().cloned().collect::>(); assert_eq!( - vec![fixture("my_fixture", vec!["42", r#""foo""#])], + vec![fixture("my_fixture", &["42", r#""foo""#])], fixtures ); assert_eq!( @@ -723,8 +780,8 @@ mod test { assert_eq!( vec![ - fixture("fixture_1", vec!["42", r#""foo""#]), - fixture("fixture_2", vec![r#""bar""#]) + fixture("fixture_1", &["42", r#""foo""#]), + fixture("fixture_2", &[r#""bar""#]) ], fixtures ); @@ -787,7 +844,7 @@ mod test { .data; let fixtures = data.fixtures().cloned().collect::>(); - assert_eq!(vec![fixture("the_fixture", vec!["42"])], fixtures); + assert_eq!(vec![fixture("the_fixture", &["42"])], fixtures); assert_eq!( to_strs!(vec!["u", "a", "d"]), diff --git a/src/render/inject.rs b/src/render/inject.rs index acbc582..4c72682 100644 --- a/src/render/inject.rs +++ b/src/render/inject.rs @@ -153,8 +153,7 @@ mod should { test::{assert_eq, *}, utils::fn_args, }; - use mytest::*; - + #[rstest] #[case::as_is("fix: String", "let fix = fix::default();")] #[case::without_underscore("_fix: String", "let _fix = fix::default();")] diff --git a/src/render/test.rs b/src/render/test.rs index 88969cb..1beda72 100644 --- a/src/render/test.rs +++ b/src/render/test.rs @@ -10,7 +10,6 @@ use syn::{ use super::*; use crate::test::{assert_eq, fixture, *}; use crate::utils::*; -use mytest::*; trait SetAsync { fn set_async(&mut self, is_async: bool); @@ -1388,7 +1387,7 @@ mod complete_should { .ast(); let data = RsTestData { items: vec![ - fixture("fix", vec!["2"]).into(), + fixture("fix", &["2"]).into(), ident("a").into(), ident("b").into(), vec!["1f64", "2f32"] @@ -1501,4 +1500,4 @@ mod complete_should { assert_eq!(attrs, &f.attrs[3..]); } } -} \ No newline at end of file +} diff --git a/src/resolver.rs b/src/resolver.rs index 4ffb563..9cdf5cc 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -5,12 +5,14 @@ use std::borrow::Cow; use std::collections::HashMap; -use proc_macro2::{Ident, Span}; +use proc_macro2::Ident; use syn::{parse_quote, Expr}; use crate::parse::Fixture; pub(crate) mod fixtures { + use quote::format_ident; + use super::*; pub(crate) fn get<'a>(fixtures: impl Iterator) -> impl Resolver + 'a { @@ -20,11 +22,13 @@ pub(crate) mod fixtures { } fn extract_resolve_expression(fixture: &Fixture) -> syn::Expr { - let name = &fixture.name; + let resolve = fixture.resolve.as_ref().unwrap_or(&fixture.name); let positional = &fixture.positional.0; - let pname = format!("partial_{}", positional.len()); - let partial = Ident::new(&pname, Span::call_site()); - parse_quote! { #name::#partial(#(#positional), *) } + let f_name = match positional.len() { + 0 => format_ident!("default"), + l => format_ident!("partial_{}", l), + }; + parse_quote! { #resolve::#f_name(#(#positional), *) } } #[cfg(test)] @@ -32,14 +36,30 @@ pub(crate) mod fixtures { use super::*; use crate::test::{assert_eq, *}; - #[test] - fn resolve_by_use_the_given_name() { - let data = vec![fixture("pippo", vec![])]; + #[rstest] + #[case(&[], "default()")] + #[case(&["my_expression"], "partial_1(my_expression)")] + #[case(&["first", "other"], "partial_2(first, other)")] + fn resolve_by_use_the_given_name(#[case] args: &[&str], #[case] expected: &str) { + let data = vec![fixture("pippo", args)]; + let resolver = get(data.iter()); + + let resolved = resolver.resolve(&ident("pippo")).unwrap().into_owned(); + + assert_eq!(resolved, format!("pippo::{}", expected).ast()); + } + + #[rstest] + #[case(&[], "default()")] + #[case(&["my_expression"], "partial_1(my_expression)")] + #[case(&["first", "other"], "partial_2(first, other)")] + fn resolve_by_use_the_resolve_field(#[case] args: &[&str], #[case] expected: &str) { + let data = vec![fixture("pippo", args).with_resolve("pluto")]; let resolver = get(data.iter()); let resolved = resolver.resolve(&ident("pippo")).unwrap().into_owned(); - assert_eq!(resolved, "pippo::partial_0()".ast()); + assert_eq!(resolved, format!("pluto::{}", expected).ast()); } } } diff --git a/src/test.rs b/src/test.rs index abb3940..362f556 100644 --- a/src/test.rs +++ b/src/test.rs @@ -6,6 +6,7 @@ use std::borrow::Cow; use std::iter::FromIterator; +pub(crate) use mytest::{fixture, rstest}; pub(crate) use pretty_assertions::assert_eq; use proc_macro2::TokenTree; use quote::quote; @@ -130,8 +131,8 @@ pub(crate) fn attrs(s: impl AsRef) -> Vec { .attrs } -pub(crate) fn fixture(name: impl AsRef, args: Vec<&str>) -> Fixture { - Fixture::new(ident(name), Positional(to_exprs!(args))) +pub(crate) fn fixture(name: impl AsRef, args: &[&str]) -> Fixture { + Fixture::new(ident(name), None, Positional(to_exprs!(args))) } pub(crate) fn arg_value(name: impl AsRef, value: impl AsRef) -> ArgumentValue { @@ -211,6 +212,13 @@ impl RsTestInfo { } } +impl Fixture { + pub fn with_resolve(mut self, resolve_ident: &str) -> Self { + self.resolve = Some(ident(resolve_ident)); + self + } +} + impl TestCase { pub fn with_description(mut self, description: &str) -> Self { self.description = Some(ident(description)); diff --git a/src/utils.rs b/src/utils.rs index 100821c..d6c9b10 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -36,6 +36,12 @@ pub(crate) fn attr_is(attr: &Attribute, name: &str) -> bool { attr.path.is_ident(&format_ident!("{}", name)) } +pub(crate) fn attr_in(attr: &Attribute, names: &[&str]) -> bool { + names + .into_iter() + .any(|name| attr.path.is_ident(&format_ident!("{}", name))) +} + pub(crate) trait IsLiteralExpression { fn is_literal(&self) -> bool; } diff --git a/tests/fixture/mod.rs b/tests/fixture/mod.rs index 45452eb..4b9dc27 100644 --- a/tests/fixture/mod.rs +++ b/tests/fixture/mod.rs @@ -2,7 +2,7 @@ use std::path::Path; pub use unindent::Unindent; use super::resources; -use rstest::*; +use mytest::*; use rstest_test::{assert_in, assert_not_in, Project, Stringable, TestResults}; fn prj(res: &str) -> Project { @@ -54,6 +54,15 @@ mod should { assert_not_in!(output.stderr.str(), "warning:"); } + #[test] + fn rename() { + let (output, _) = run_test("rename.rs"); + + TestResults::new() + .ok("test") + .assert(output); + } + mod accept_and_return { use super::*; diff --git a/tests/resources/fixture/rename.rs b/tests/resources/fixture/rename.rs new file mode 100644 index 0000000..c399490 --- /dev/null +++ b/tests/resources/fixture/rename.rs @@ -0,0 +1,36 @@ +use rstest::*; + +#[fixture] +fn very_long_and_boring_name(#[default(42)] inject: u32) -> u32 { + inject +} + +#[fixture(very_long_and_boring_name as foo)] +fn compact(foo: u32) -> u32 { + foo +} + +#[fixture(very_long_and_boring_name(21) as foo)] +fn compact_injected(foo: u32) -> u32 { + foo +} + +#[fixture] +fn attribute(#[from(very_long_and_boring_name)] foo: u32) -> u32 { + foo +} + +#[fixture] +fn attribute_injected( + #[from(very_long_and_boring_name)] + #[with(21)] + foo: u32, +) -> u32 { + foo +} + +#[rstest] +fn test(compact: u32, attribute: u32, compact_injected: u32, attribute_injected: u32) { + assert_eq!(compact, attribute); + assert_eq!(compact_injected, attribute_injected); +} diff --git a/tests/resources/rstest/rename.rs b/tests/resources/rstest/rename.rs new file mode 100644 index 0000000..ebd442d --- /dev/null +++ b/tests/resources/rstest/rename.rs @@ -0,0 +1,30 @@ +use rstest::*; + +#[fixture] +fn very_long_and_boring_name(#[default(42)] inject: u32) -> u32 { + inject +} + +#[rstest(very_long_and_boring_name as foo)] +fn compact(foo: u32) { + assert!(42 == foo); +} + +#[rstest(very_long_and_boring_name(21) as foo)] +fn compact_injected(foo: u32) { + assert!(21 == foo); +} + +#[rstest] +fn attribute(#[from(very_long_and_boring_name)] foo: u32) { + assert!(42 == foo); +} + +#[rstest] +fn attribute_injected( + #[from(very_long_and_boring_name)] + #[with(21)] + foo: u32, +) { + assert!(21 == foo); +} diff --git a/tests/rstest/mod.rs b/tests/rstest/mod.rs index d4e4ee0..f189905 100644 --- a/tests/rstest/mod.rs +++ b/tests/rstest/mod.rs @@ -2,7 +2,7 @@ use std::path::Path; use super::resources; -use rstest::*; +use mytest::*; use rstest_test::*; use unindent::Unindent; @@ -871,6 +871,18 @@ fn happy_path() { .assert(output); } +#[test] +fn rename() { + let (output, _) = run_test("rename.rs"); + + TestResults::new() + .ok("compact") + .ok("compact_injected") + .ok("attribute") + .ok("attribute_injected") + .assert(output); +} + mod should_show_correct_errors { use std::process::Output;