Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#[from(other)path)] should accept also path #255

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
### Add

- Implemented `#[by_ref]` attribute to take get a local lifetime for test arguments.
See [#241](https://github.com/la10736/rstest/issues/241) for more details. Thanks to
See [#241](https://github.com/la10736/rstest/issues/241) for more details. Thanks to
@narpfel for suggesting it and useful discussions.

### Fixed
Expand All @@ -16,6 +16,8 @@ See [#241](https://github.com/la10736/rstest/issues/241) for more details. Thank
[#241](https://github.com/la10736/rstest/issues/241) for more details.
- [`PathBuf`](https://doc.rust-lang.org/std/path/struct.PathBuf.html) does no longer need to be
in scope when using `#[files]` (see [#242](https://github.com/la10736/rstest/pull/242))
- `#[from(now::accept::also::path::for::fixture)]` See [#246](https://github.com/la10736/rstest/issues/246)
for more details

## [0.19.0] 2024/4/9

Expand Down
30 changes: 29 additions & 1 deletion rstest/tests/resources/fixture/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ fn very_long_and_boring_name(#[default(42)] inject: u32) -> u32 {
inject
}

mod sub_module {
use super::*;

#[fixture]
pub fn mod_fixture() -> u32 {
42
}
}

#[fixture(very_long_and_boring_name as foo)]
fn compact(foo: u32) -> u32 {
foo
Expand All @@ -15,11 +24,21 @@ fn compact_injected(foo: u32) -> u32 {
foo
}

#[fixture(sub_module::mod_fixture as foo)]
fn compact_from_mod(foo: u32) -> u32 {
foo
}

#[fixture]
fn attribute(#[from(very_long_and_boring_name)] foo: u32) -> u32 {
foo
}

#[fixture]
fn attribute_mod(#[from(sub_module::mod_fixture)] foo: u32) -> u32 {
foo
}

#[fixture]
fn attribute_injected(
#[from(very_long_and_boring_name)]
Expand All @@ -30,7 +49,16 @@ fn attribute_injected(
}

#[rstest]
fn test(compact: u32, attribute: u32, compact_injected: u32, attribute_injected: u32) {
fn test(
compact: u32,
attribute: u32,
attribute_mod: u32,
compact_from_mod: u32,
compact_injected: u32,
attribute_injected: u32,
) {
assert_eq!(compact, attribute);
assert_eq!(attribute, attribute_mod);
assert_eq!(attribute_mod, compact_from_mod);
assert_eq!(compact_injected, attribute_injected);
}
19 changes: 19 additions & 0 deletions rstest/tests/resources/rstest/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ fn very_long_and_boring_name(#[default(42)] inject: u32) -> u32 {
inject
}

mod sub_module {
use super::*;

#[fixture]
pub fn mod_fixture() -> u32 {
42
}
}

#[rstest(very_long_and_boring_name as foo)]
fn compact(foo: u32) {
assert!(42 == foo);
}

#[rstest(sub_module::mod_fixture as foo)]
fn compact_mod(foo: u32) {
assert!(42 == foo);
}

#[rstest(very_long_and_boring_name(21) as foo)]
fn compact_injected(foo: u32) {
assert!(21 == foo);
Expand All @@ -20,6 +34,11 @@ fn attribute(#[from(very_long_and_boring_name)] foo: u32) {
assert!(42 == foo);
}

#[rstest]
fn attribute_mod(#[from(sub_module::mod_fixture)] foo: u32) {
assert!(42 == foo);
}

#[rstest]
fn attribute_injected(
#[from(very_long_and_boring_name)]
Expand Down
2 changes: 2 additions & 0 deletions rstest/tests/rstest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,10 @@ fn rename() {

TestResults::new()
.ok("compact")
.ok("compact_mod")
.ok("compact_injected")
.ok("attribute")
.ok("attribute_mod")
.ok("attribute_injected")
.assert(output);
}
Expand Down
2 changes: 1 addition & 1 deletion rstest_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ actix-rt = "2.7.0"
async-std = { version = "1.12.0", features = ["attributes"] }
maplit = "1.0.2"
pretty_assertions = "1.2.1"
rstest = { version = "0.19.0", default-features = false }
rstest = { path = "../rstest", default-features = false }
rstest_reuse = { path = "../rstest_reuse" }
rstest_test = { path = "../rstest_test" }

Expand Down
17 changes: 17 additions & 0 deletions rstest_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ use quote::ToTokens;
/// assert_eq!(42, short)
/// }
/// ```
///
/// This feature can also be useful when you don't want to declare the `use` of a fixture or simple
/// use the fixture's path:
///
/// ```
/// use rstest::*;
///
/// # mod magic_numbers {
/// # use rstest::*;
/// # #[fixture]
/// # pub fn fortytwo() -> i32 { 42 }
/// # }
/// #[rstest]
/// fn the_test(#[from(magic_numbers::fortytwo)] x: i32) {
/// assert_eq!(42, x)
/// }
/// ```
///
/// # `#[once]` Fixture
///
Expand Down
3 changes: 3 additions & 0 deletions rstest_macros/src/parse/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ mod extend {
fn test_fn(
#[from(long_fixture_name)]
#[with(42, "other")] short: u32,
#[from(sub_module::fix)]
f: u32,
#[from(simple)]
s: &str,
no_change: i32) {
Expand All @@ -460,6 +462,7 @@ mod extend {
fixture("short", &["42", r#""other""#])
.with_resolve("long_fixture_name")
.into(),
fixture("f", &[]).with_resolve("sub_module::fix").into(),
fixture("s", &[]).with_resolve("simple").into(),
]
.into(),
Expand Down
14 changes: 10 additions & 4 deletions rstest_macros/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ impl Parse for Positional {
#[derive(PartialEq, Debug, Clone)]
pub(crate) struct Fixture {
pub(crate) name: Ident,
pub(crate) resolve: Option<Ident>,
pub(crate) resolve: Option<syn::Path>,
pub(crate) positional: Positional,
}

impl Fixture {
pub(crate) fn new(name: Ident, resolve: Option<Ident>, positional: Positional) -> Self {
pub(crate) fn new(name: Ident, resolve: Option<syn::Path>, positional: Positional) -> Self {
Self {
name,
resolve,
Expand All @@ -153,7 +153,7 @@ impl Fixture {

impl Parse for Fixture {
fn parse(input: ParseStream) -> syn::Result<Self> {
let resolve = input.parse()?;
let resolve: syn::Path = input.parse()?;
if input.peek(Paren) || input.peek(Token![as]) {
let positional = if input.peek(Paren) {
let content;
Expand All @@ -167,7 +167,13 @@ impl Parse for Fixture {
let _: Token![as] = input.parse()?;
Ok(Self::new(input.parse()?, Some(resolve), positional))
} else {
Ok(Self::new(resolve, None, positional))
let name = resolve.get_ident().ok_or_else(|| {
syn::Error::new_spanned(
resolve.to_token_stream(),
format!("Should be an ident"),
)
})?;
Ok(Self::new(name.clone(), None, positional))
}
} else {
Err(syn::Error::new(
Expand Down
6 changes: 5 additions & 1 deletion rstest_macros/src/parse/rstest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,15 @@ mod test {
#[test]
fn rename() {
let data = parse_rstest(
r#"long_fixture_name(42, "other") as short, simple as s, no_change()"#,
r#"long_fixture_name(42, "other") as short, sub_module::fix as f, simple as s, no_change()"#,
);

let expected = RsTestInfo {
data: vec![
fixture("short", &["42", r#""other""#])
.with_resolve("long_fixture_name")
.into(),
fixture("f", &[]).with_resolve("sub_module::fix").into(),
fixture("s", &[]).with_resolve("simple").into(),
fixture("no_change", &[]).into(),
]
Expand All @@ -423,6 +424,8 @@ mod test {
#[with(42, "other")] short: u32,
#[from(simple)]
s: &str,
#[from(sub_module::fix)]
f: u32,
no_change: i32) {
}
"#
Expand All @@ -434,6 +437,7 @@ mod test {
.with_resolve("long_fixture_name")
.into(),
fixture("s", &[]).with_resolve("simple").into(),
fixture("f", &[]).with_resolve("sub_module::fix").into(),
]
.into(),
..Default::default()
Expand Down
15 changes: 11 additions & 4 deletions rstest_macros/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub(crate) mod fixtures {
}

fn extract_resolve_expression(fixture: &Fixture) -> syn::Expr {
let resolve = fixture.resolve.as_ref().unwrap_or(&fixture.name);
let resolve = fixture
.resolve
.clone()
.unwrap_or_else(|| fixture.name.clone().into());
let positional = &fixture.positional.0;
let f_name = match positional.len() {
0 => format_ident!("default"),
Expand Down Expand Up @@ -53,13 +56,17 @@ pub(crate) mod fixtures {
#[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")];
fn resolve_by_use_the_resolve_field(
#[case] args: &[&str],
#[case] expected: &str,
#[values("pluto", "minnie::pluto")] resolver_path: &str,
) {
let data = vec![fixture("pippo", args).with_resolve(resolver_path)];
let resolver = get(data.iter());

let resolved = resolver.resolve(&ident("pippo")).unwrap().into_owned();

assert_eq!(resolved, format!("pluto::{}", expected).ast());
assert_eq!(resolved, format!("{}::{}", resolver_path, expected).ast());
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions rstest_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ pub(crate) fn ident(s: impl AsRef<str>) -> Ident {
s.as_ref().ast()
}

pub(crate) fn path(s: impl AsRef<str>) -> syn::Path {
s.as_ref().ast()
}

pub(crate) fn expr(s: impl AsRef<str>) -> syn::Expr {
s.as_ref().ast()
}
Expand Down Expand Up @@ -218,8 +222,8 @@ impl RsTestInfo {
}

impl Fixture {
pub fn with_resolve(mut self, resolve_ident: &str) -> Self {
self.resolve = Some(ident(resolve_ident));
pub fn with_resolve(mut self, resolve_path: &str) -> Self {
self.resolve = Some(path(resolve_path));
self
}
}
Expand Down
Loading