Skip to content

Commit

Permalink
Close #121
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed May 24, 2021
1 parent 66b3fed commit 88e32a5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -8,14 +8,14 @@

### Fixed

- use mutable fixture in in cases and value list (See #121)

## [0.10.0] 2021/05/16

### Add

- Rename fixture (See #107 and #108)

### Changed

### Fixed

- Wired behaviour in `#[fixture]` with generics types that have transitive
Expand Down
19 changes: 14 additions & 5 deletions src/render/inject.rs
Expand Up @@ -7,7 +7,7 @@ use syn::{parse_quote, Expr, FnArg, Ident, Stmt, Type};
use crate::{
refident::{MaybeIdent, MaybeType},
resolver::Resolver,
utils::IsLiteralExpression,
utils::{fn_arg_mutability, IsLiteralExpression},
};

pub(crate) fn resolve_aruments<'a>(
Expand Down Expand Up @@ -44,6 +44,10 @@ where

fn resolve(&self, arg: &FnArg) -> Option<Stmt> {
let ident = arg.maybe_ident()?;
let mutability = fn_arg_mutability(arg);
let unused_mut: Option<syn::Attribute> = mutability
.as_ref()
.map(|_| parse_quote! {#[allow(unused_mut)]});
let arg_type = arg.maybe_type()?;
let fixture_name = self.fixture_name(ident);

Expand All @@ -57,7 +61,8 @@ where
fixture = Cow::Owned((self.magic_conversion)(fixture, arg_type));
}
Some(parse_quote! {
let #ident = #fixture;
#unused_mut
let #mutability #ident = #fixture;
})
}

Expand Down Expand Up @@ -153,13 +158,16 @@ mod should {
test::{assert_eq, *},
utils::fn_args,
};

#[rstest]
#[case::as_is("fix: String", "let fix = fix::default();")]
#[case::without_underscore("_fix: String", "let _fix = fix::default();")]
#[case::do_not_remove_inner_underscores("f_i_x: String", "let f_i_x = f_i_x::default();")]
#[case::do_not_remove_double_underscore("__fix: String", "let __fix = __fix::default();")]
#[case::without_mut("mut fix: String", "let fix = fix::default();")]
#[case::preserve_mut_but_annotate_as_allow_unused_mut(
"mut fix: String",
"#[allow(unused_mut)] let mut fix = fix::default();"
)]
fn call_fixture(#[case] arg_str: &str, #[case] expected: &str) {
let arg = arg_str.ast();

Expand All @@ -171,7 +179,8 @@ mod should {
}

#[rstest]
#[case::as_is("mut fix: String", ("fix", expr("bar()")), "let fix = bar();")]
#[case::as_is("fix: String", ("fix", expr("bar()")), "let fix = bar();")]
#[case::with_allow_unused_mut("mut fix: String", ("fix", expr("bar()")), "#[allow(unused_mut)] let mut fix = bar();")]
#[case::without_undescore("_fix: String", ("fix", expr("bar()")), "let _fix = bar();")]
fn call_given_fixture(
#[case] arg_str: &str,
Expand Down
10 changes: 10 additions & 0 deletions src/utils.rs
Expand Up @@ -260,6 +260,16 @@ fn first_type_path_segment_ident(t: &Type) -> Option<&Ident> {
}
}

pub(crate) fn fn_arg_mutability(arg: &FnArg) -> Option<syn::token::Mut> {
match arg {
FnArg::Typed(syn::PatType { pat, .. }) => match pat.as_ref() {
syn::Pat::Ident(syn::PatIdent { mutability, .. }) => mutability.clone(),
_ => None,
},
_ => None,
}
}

#[cfg(test)]
mod test {
use syn::parse_quote;
Expand Down
@@ -0,0 +1,25 @@
use rstest::*;

#[fixture]
fn f() -> String {
"f".to_owned()
}

fn append(s: &mut String, a: &str) -> String {
s.push_str("-");
s.push_str(a);
s.clone()
}

#[rstest]
#[case(append(&mut f, "a"), "f-a", "f-a-b")]
fn use_mutate_fixture(
mut f: String,
#[case] a: String,
#[values(append(&mut f, "b"))] b: String,
#[case] expected_a: &str,
#[case] expected_b: &str,
) {
assert_eq!(expected_a, a);
assert_eq!(expected_b, b);
}
9 changes: 9 additions & 0 deletions tests/rstest/mod.rs
Expand Up @@ -93,6 +93,15 @@ fn impl_input() {
.assert(output);
}

#[test]
fn use_mutable_fixture_in_parametric_argumnts() {
let (output, _) = run_test("use_mutable_fixture_in_parametric_argumnts.rs");

TestResults::new()
.ok("use_mutate_fixture::case_1::b_1")
.assert(output);
}

#[test]
fn should_reject_no_item_function() {
let (output, name) = run_test("reject_no_item_function.rs");
Expand Down

0 comments on commit 88e32a5

Please sign in to comment.