Skip to content

Commit 47d2d8a

Browse files
authored
Merge pull request #79 from str4d/test-cfg-attributes
test: Support `#[cfg(..)]` attributes on test methods
2 parents 52f27c1 + d2656fa commit 47d2d8a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
66

77
## [Unreleased]
8+
### Changed
9+
- `flipperzero_test::tests` now allows `#[cfg(..)]` attributes on test methods.
810

911
## [0.10.0]
1012
### Added

crates/test/macros/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn tests_runner_impl(args: TokenStream) -> parse::Result<TokenStream> {
8787

8888
(
8989
quote!(#attr::__test_list().len()),
90-
quote!(#attr::__test_list().map(|(name, test_fn)| (#module, name, test_fn))),
90+
quote!(#attr::__test_list().iter().copied().map(|(name, test_fn)| (#module, name, test_fn))),
9191
)
9292
})
9393
.collect::<Vec<_>>();
@@ -170,18 +170,23 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
170170
};
171171

172172
let mut tests = vec![];
173+
let mut test_cfgs = vec![];
173174
let mut untouched_tokens = vec![];
174175
for item in items {
175176
match item {
176177
Item::Fn(mut f) => {
177178
let mut is_test = false;
179+
let mut cfg = vec![];
178180

179-
// Find and extract the `#[test]` attribute, if present.
181+
// Find and extract the `#[test]` and `#[cfg(..)] attributes, if present.
180182
f.attrs.retain(|attr| {
181183
if attr.path.is_ident("test") {
182184
is_test = true;
183185
false
184186
} else {
187+
if attr.path.is_ident("cfg") {
188+
cfg.push(attr.clone());
189+
}
185190
true
186191
}
187192
});
@@ -220,6 +225,7 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
220225
)?));
221226

222227
tests.push(f);
228+
test_cfgs.push(cfg);
223229
} else {
224230
untouched_tokens.push(Item::Fn(f));
225231
}
@@ -231,11 +237,13 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
231237
}
232238

233239
let ident = module.ident;
234-
let test_count = tests.len();
235-
let test_names = tests.iter().map(|test| {
240+
let test_names = tests.iter().zip(test_cfgs).map(|(test, cfg)| {
236241
let ident = &test.sig.ident;
237242
let name = ident.to_string();
238-
quote!((#name, #ident))
243+
quote! {
244+
#(#cfg)*
245+
(#name, #ident)
246+
}
239247
});
240248

241249
Ok(quote!(
@@ -245,8 +253,8 @@ fn tests_impl(args: TokenStream, input: TokenStream) -> parse::Result<TokenStrea
245253

246254
#(#tests)*
247255

248-
pub(crate) const fn __test_list() -> [(&'static str, ::flipperzero_test::TestFn); #test_count] {
249-
[#(#test_names), *]
256+
pub(crate) const fn __test_list() -> &'static [(&'static str, ::flipperzero_test::TestFn)] {
257+
&[#(#test_names), *]
250258
}
251259
}
252260
)

0 commit comments

Comments
 (0)