Skip to content

Commit

Permalink
#282 : add e2e test, cover also the once fixture case and add cheange…
Browse files Browse the repository at this point in the history
… log lines
  • Loading branch information
la10736 committed Nov 17, 2024
1 parent cb5db35 commit 88f9c2e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

### Changed

- MSRV to 1.70.0 (see [#284](https://github.com/la10736/rstest/issues/284) thanks to @rnbguy)

### Add

- `#![no_std]` support: now you can use `rstest` also in `no_std` lib
(see [#282](https://github.com/la10736/rstest/issues/282) thanks to @rnbguy)

### Fixed


## [0.23.0] 2024/9/29

### Add
Expand Down
7 changes: 7 additions & 0 deletions rstest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ pub mod magic_conversion;
#[doc(hidden)]
pub mod timeout;

#[doc(hidden)]
pub mod __std {
pub mod sync {
pub use std::sync::{Once, OnceLock};
}
}

/// Define a fixture that you can use in all `rstest`'s test arguments. You should just mark your
/// function as `#[fixture]` and then use it as a test's argument. Fixture functions can also
/// use other fixtures.
Expand Down
33 changes: 33 additions & 0 deletions rstest/tests/resources/rstest/no_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![no_std]

use rstest::{fixture, rstest};

#[fixture]
#[once]
fn once_fixture() -> u64 {
42
}

#[fixture]
#[once]
fn empty_once_fixture() {}

#[fixture]
async fn async_fixture() -> u64 {
42
}

pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[rstest]
#[case("2", "2", "4")]
fn it_works(#[case] left: u64, #[case] right: u64, #[case] expected: u64) {
assert_eq!(add(left, right), expected);
}

#[rstest]
async fn async_works(#[future] async_fixture: u64) {
assert_eq!(42, async_fixture.await);
}
14 changes: 13 additions & 1 deletion rstest/tests/rstest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ fn local_lifetime() {
#[test]
fn by_ref() {
let prj = prj("by_ref.rs");
let files_path = prj.path().join("files");
let files_path: std::path::PathBuf = prj.path().join("files");
std::fs::create_dir(&files_path).unwrap();
let name = "my_name.txt";
let mut out = File::create(files_path.join(name)).unwrap();
Expand All @@ -1204,6 +1204,18 @@ fn by_ref() {
.assert(output);
}

#[test]
fn no_std() {
let prj = prj("no_std.rs");
prj.add_dependency("async-std", r#"{version="*", features=["attributes"]}"#);
let output = prj.run_tests().unwrap();

TestResults::new()
.ok("it_works::case_1")
.ok("async_works")
.assert(output);
}

mod async_timeout_feature {
use super::*;

Expand Down
5 changes: 5 additions & 0 deletions rstest_macros/src/render/crate_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ pub fn crate_name() -> syn::Path {
}
}
}

pub fn std_path() -> syn::Path {
let rstest = crate_name();
parse_quote! { #rstest::__std }
}
9 changes: 5 additions & 4 deletions rstest_macros/src/render/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use syn::{parse_quote, FnArg, Generics, Ident, ItemFn, ReturnType};

use quote::quote;

use super::apply_arguments::ApplyArguments;
use super::{inject, render_exec_call};
use crate::refident::MaybeIdent;
use crate::render::{apply_arguments::ApplyArguments, crate_resolver::std_path};
use crate::resolver::{self, Resolver};
use crate::{parse::fixture::FixtureInfo, utils::generics_clean_up};

Expand All @@ -20,14 +20,15 @@ fn wrap_return_type_as_static_ref(rt: ReturnType) -> ReturnType {
}

fn wrap_call_impl_with_call_once_impl(call_impl: TokenStream, rt: &ReturnType) -> TokenStream {
let std = std_path();
match rt {
syn::ReturnType::Type(_, t) => parse_quote! {
static CELL: std::sync::OnceLock<#t> =
std::sync::OnceLock::new();
static CELL: #std::sync::OnceLock<#t> =
#std::sync::OnceLock::new();
CELL.get_or_init(|| #call_impl )
},
_ => parse_quote! {
static CELL: std::sync::Once = std::sync::Once::new();
static CELL: #std::sync::Once = #std::sync::Once::new();
CELL.call_once(|| #call_impl );
},
}
Expand Down

0 comments on commit 88f9c2e

Please sign in to comment.