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

refactor: use core instead of std #283

Merged
merged 7 commits into from
Nov 17, 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
22 changes: 11 additions & 11 deletions rstest_macros/src/render/apply_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl ImplFutureArg for FnArg {
Some(ty) => {
let lifetime = update_type_with_lifetime(ty, lifetime_id);
*ty = parse_quote! {
impl std::future::Future<Output = #ty>
impl core::future::Future<Output = #ty>
};
self.remove_mutability();
lifetime
Expand Down Expand Up @@ -166,30 +166,30 @@ mod should {
#[case::simple(
"fn f(a: u32) {}",
&["a"],
"fn f(a: impl std::future::Future<Output = u32>) {}"
"fn f(a: impl core::future::Future<Output = u32>) {}"
)]
#[case::more_than_one(
"fn f(a: u32, b: String, c: std::collection::HashMap<usize, String>) {}",
&["a", "b", "c"],
r#"fn f(a: impl std::future::Future<Output = u32>,
b: impl std::future::Future<Output = String>,
c: impl std::future::Future<Output = std::collection::HashMap<usize, String>>) {}"#,
r#"fn f(a: impl core::future::Future<Output = u32>,
b: impl core::future::Future<Output = String>,
c: impl core::future::Future<Output = std::collection::HashMap<usize, String>>) {}"#,
)]
#[case::just_one(
"fn f(a: u32, b: String) {}",
&["b"],
r#"fn f(a: u32,
b: impl std::future::Future<Output = String>) {}"#
b: impl core::future::Future<Output = String>) {}"#
)]
#[case::generics(
"fn f<S: AsRef<str>>(a: S) {}",
&["a"],
"fn f<S: AsRef<str>>(a: impl std::future::Future<Output = S>) {}"
"fn f<S: AsRef<str>>(a: impl core::future::Future<Output = S>) {}"
)]
#[case::remove_mut(
"fn f(mut a: u32) {}",
&["a"],
r#"fn f(a: impl std::future::Future<Output = u32>) {}"#
r#"fn f(a: impl core::future::Future<Output = u32>) {}"#
)]
fn replace_future_basic_type(
#[case] item_fn: &str,
Expand All @@ -213,17 +213,17 @@ mod should {
#[case::base(
"fn f(ident_name: &u32) {}",
&["ident_name"],
"fn f<'_ident_name>(ident_name: impl std::future::Future<Output = &'_ident_name u32>) {}"
"fn f<'_ident_name>(ident_name: impl core::future::Future<Output = &'_ident_name u32>) {}"
)]
#[case::lifetime_already_exists(
"fn f<'b>(a: &'b u32) {}",
&["a"],
"fn f<'b>(a: impl std::future::Future<Output = &'b u32>) {}"
"fn f<'b>(a: impl core::future::Future<Output = &'b u32>) {}"
)]
#[case::some_other_generics(
"fn f<'b, IT: Iterator<Item=String + 'b>>(a: &u32, it: IT) {}",
&["a"],
"fn f<'_a, 'b, IT: Iterator<Item=String + 'b>>(a: impl std::future::Future<Output = &'_a u32>, it: IT) {}"
"fn f<'_a, 'b, IT: Iterator<Item=String + 'b>>(a: impl core::future::Future<Output = &'_a u32>, it: IT) {}"
)]
fn replace_reference_type(
#[case] item_fn: &str,
Expand Down
4 changes: 2 additions & 2 deletions rstest_macros/src/render/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ mod should {
let expected = parse_str::<syn::ItemFn>(
r#"
async fn get<'_async_ref_u32>(
async_ref_u32: impl std::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl std::future::Future<Output = u32>,
async_ref_u32: impl core::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl core::future::Future<Output = u32>,
simple: u32
)
{ }
Expand Down
87 changes: 86 additions & 1 deletion rstest_macros/src/render/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn handling_magic_conversion_code(fixture: Cow<Expr>, arg_type: &Type) -> Expr {
parse_quote! {
{
use #rstest_path::magic_conversion::*;
(&&&Magic::<#arg_type>(std::marker::PhantomData)).magic_conversion(#fixture)
(&&&Magic::<#arg_type>(core::marker::PhantomData)).magic_conversion(#fixture)
}
}
}
Expand Down Expand Up @@ -208,4 +208,89 @@ mod should {

assert_eq!(injected, expected.ast());
}

#[rstest]
#[case::simple_type(
"fn test(arg: MyType) {}",
0,
"let arg = {
use rstest::magic_conversion::*;
(&&&Magic::<MyType>(core::marker::PhantomData)).magic_conversion(\"value to convert\")
};"
)]
#[case::discard_impl(
"fn test(arg: impl AsRef<str>) {}",
0,
r#"let arg = "value to convert";"#
)]
#[case::discard_generic_type(
"fn test<S: AsRef<str>>(arg: S) {}",
0,
r#"let arg = "value to convert";"#
)]
#[case::reference_type(
"fn test(arg: &MyType) {}",
0,
"let arg = {
use rstest::magic_conversion::*;
(&&&Magic::<&MyType>(core::marker::PhantomData)).magic_conversion(\"value to convert\")
};"
)]
#[case::mutable_reference_type(
"fn test(arg: &mut MyType) {}",
0,
"let arg = {
use rstest::magic_conversion::*;
(&&&Magic::<&mut MyType>(core::marker::PhantomData)).magic_conversion(\"value to convert\")
};"
)]
#[case::generic_type_with_lifetime(
"fn test<'a, T>(arg: &'a T) where T: Default {}",
0,
"let arg = {
use rstest::magic_conversion::*;
(&&&Magic::<&'a T>(core::marker::PhantomData)).magic_conversion(\"value to convert\")
};"
)]
#[case::type_with_generic_parameters(
"fn test(arg: Option<MyType>) {}",
0,
"let arg = {
use rstest::magic_conversion::*;
(&&&Magic::<Option<MyType>>(core::marker::PhantomData)).magic_conversion(\"value to convert\")
};"
)]
#[case::complex_type(
"fn test(arg: Result<Vec<MyType>, MyError>) {}",
0,
"let arg = {
use rstest::magic_conversion::*;
(&&&Magic::<Result<Vec<MyType>, MyError>>(core::marker::PhantomData)).magic_conversion(\"value to convert\")
};"
)]
fn generated_code_uses_phantom_data(
#[case] fn_str: &str,
#[case] n_arg: usize,
#[case] expected: &str,
) {
let function = fn_str.ast();
let arg = fn_args(&function).nth(n_arg).unwrap();
let generics = function
.sig
.generics
.type_params()
.map(|tp| &tp.ident)
.cloned()
.collect::<Vec<_>>();

let mut resolver = std::collections::HashMap::new();
let expr = expr(r#""value to convert""#);
resolver.insert(arg.maybe_pat().unwrap().clone(), &expr);

let ag = ArgumentResolver::new(&resolver, &generics);

let injected = ag.resolve(&arg).unwrap();

assert_eq!(injected, expected.ast());
}
}
2 changes: 1 addition & 1 deletion rstest_macros/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn render_test_call(
let timeout = timeout.map(|x| quote! {#x}).or_else(|| {
std::env::var("RSTEST_TIMEOUT")
.ok()
.map(|to| quote! { std::time::Duration::from_secs( (#to).parse().unwrap()) })
.map(|to| quote! { core::time::Duration::from_secs( (#to).parse().unwrap()) })
});
let rstest_path = crate_name();
match (timeout, is_async) {
Expand Down
12 changes: 6 additions & 6 deletions rstest_macros/src/render/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ mod single_test_should {

let expected = parse_str::<syn::ItemFn>(
r#"async fn test<'_async_ref_u32>(
async_ref_u32: impl std::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl std::future::Future<Output = u32>,
async_ref_u32: impl core::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl core::future::Future<Output = u32>,
simple: u32
)
{ }
Expand Down Expand Up @@ -908,8 +908,8 @@ mod cases_should {

let expected = parse_str::<syn::ItemFn>(
r#"async fn test<'_async_ref_u32>(
async_ref_u32: impl std::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl std::future::Future<Output = u32>,
async_ref_u32: impl core::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl core::future::Future<Output = u32>,
simple: u32
)
{ }
Expand Down Expand Up @@ -1354,8 +1354,8 @@ mod matrix_cases_should {

let expected = parse_str::<syn::ItemFn>(
r#"async fn test<'_async_ref_u32>(
async_ref_u32: impl std::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl std::future::Future<Output = u32>,
async_ref_u32: impl core::future::Future<Output = &'_async_ref_u32 u32>,
async_u32: impl core::future::Future<Output = u32>,
simple: u32
)
{ }
Expand Down
Loading