-
Notifications
You must be signed in to change notification settings - Fork 37
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
Access test case name from test case body #37
Comments
|
I thought about it. In my opinion yes, there should be a way to retrieve test case name, however right now I'm not sure how to achieve it without creating variable "from thin air". What I mean if you are not aware that |
I thought either to "hide it" inside some macro like Another idea is to have explicit argument in function definition with some extra attribute. |
|
Okay, I feel macro may be quite cumbersome to achieve. However, I was thinking a bit about the issue and what about something like this: #[test_case("".into(), 0)]
#[test_case("bar".into(), 1)]
fn foo(arg_1: String, arg_2: usize) {
dbg!(&arg_1);
dbg!(&arg_2);
dbg!(&self.name); // <- Note self.name where `self` would contain test case data
} Isn't it going to be to "magical"? |
Hmm okay, but I think I can simply modify this idea with #[test_case("".into(), 0)]
#[test_case("bar".into(), 1)]
fn foo(arg_1: String, arg_2: usize) {
dbg!(&arg_1);
dbg!(&arg_2);
dbg!(test_case_context!().name);
} I was thinking about |
I think the last idea is better 🙂 |
Okay, I have some extra notes and edge cases we need to consider but for now, it seems to be a good way forward. It requires major refactoring in macro but technically I believe we can deliver every existing feature with this approach + async await support would be quite easy. The biggest issue I see is to transform @luke-biel - what do you think? |
@frondeus I'd say we introduce the macro as is, with easiest implementation possible. It seems that we agree that |
Hi, big-time One thought experiment: what if The design and implementation is thorny but the idea would be something like this would do the right thing:
I looked into this a bit while trying to get my project target-test-dir to compose with The design issue is that
For the composition of
(I'm not actually certain if the order shown for -next is the bigger problem is that So… thanks for coming along this journey with me, but I don't see a way yet to make these two proc-macros compose cleanly. It's a shame because I personally would prefer the ability to mix and match a variety of proc-macros, rather than having a single proc-macro that does everything. |
BTW, the composition tests test-case-composition.rs in the target-test-dir project work! The problem is that |
I see your issue and I must say I have no idea how to get fn test_fn() {
println!("Call me: {}", function_name!());
}
mod test_fn {
#[test]
#[named]
fn test() {
let res = super::test_fn();
res
} So the Other note is, that just by using a custom test harness, suddenly a lot of |
@luke-biel Yep, I don't see any clean way to make |
Are there current best practices for
#[cfg(test)]
macro_rules! set_snapshot_suffix {
($($expr:expr),*) => {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_suffix(format!($($expr,)*));
let _guard = settings.bind_to_scope();
}
}
#[cfg(test)]
fn suffix(name: &str) {
set_snapshot_suffix!("{}", name);
}
let _result = stringify!(phone);
dbg!(&_result);
// suffix(_result); // can't work
set_snapshot_suffix!("{}", _result); // work
|
https://github.com/huang12zheng/test-case/tree/prehook #[case(("+118150".to_string(), "".to_string());"phone" => ! set_snapshot_suffix)] use crate::expr::TestCaseExpression;
#[derive(Debug)]
pub struct TestCaseComment {
_semicolon: Token![;],
pub comment: LitStr,
pub expression: Option<TestCaseExpression>,
}
impl Parse for TestCaseComment {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self {
_semicolon: input.parse()?,
comment: input.parse()?,
expression: input.parse().ok(),
})
}
}
pub fn test_case_name_prehook(&self) -> TokenStream2 {
if let Some(comment) = self.comment.as_ref() {
if let Some(expr) = comment.expression.as_ref() {
return match expr.result {
TestCaseResult::Panicking(_) => TokenStream2::new(),
TestCaseResult::CommentMacro(_) => {
let assertion = expr.assertion();
let test_case_name: LitStr = comment.comment.clone();
return quote!(
#assertion!(#test_case_name);
);
}
_ => return quote!(expr.assertion()),
};
}
}
return quote! {};
}
quote! {
#(#attrs)*
#signature {
#test_case_name_prehook
#body
#expected
}
}
|
@huang12zheng Afaik best solution in this case would be to use const SUFFIX: &str = "phone";
#[case(1)]
#[case(2)]
fn a_test(i: i32) {
with_settings!({snapshot_suffix = SUFFIX}, { /* do the snapshot comparison */ })
} Or if you need a suffix per test, passing the suffix as an argument: #[case(1, "test_1")]
#[case(2, "test_2")]
fn a_test(i: i32, suffix: &str) {
with_settings!({snapshot_suffix = suffix}, {/* do the snapshot comparison */ })
} I don't see us putting extra macros into As for accessing the test name from the body (which appears to be more and more needed feature), I'm gonna prioritize at least designing it. |
Example code
The text was updated successfully, but these errors were encountered: