featureflag_test_macros/lib.rs
1//! Test macros for the `featureflag` crate.
2//!
3//! This crate shouldn't be used directly, but should be used
4//! through its reexports in the `featureflag-test` crate.
5#![cfg_attr(docsrs, feature(doc_cfg))]
6
7use quote::ToTokens;
8
9mod utils;
10mod with_features;
11
12/// Enable the specified features for use in tests.
13///
14/// This macro calls `featureflag::evaluator::set_thread_evaluator`, so it
15/// should only be used for single-threaded tests.
16///
17/// Feature values can be any value that implements the `featureflag_test::TestFeature`
18/// trait.
19///
20/// # Examples
21///
22/// ```no_run
23/// #[test]
24/// #[with_features("enabled" = true, "disabled" = false)]
25/// fn my_test() {
26/// assert!(featureflag::is_enabled("enabled"));
27/// assert!(!featureflag::is_enabled("disabled"));
28/// }
29/// ```
30#[proc_macro_attribute]
31pub fn with_features(
32 args: proc_macro::TokenStream,
33 input: proc_macro::TokenStream,
34) -> proc_macro::TokenStream {
35 let args = syn::parse_macro_input!(args);
36 let input = syn::parse_macro_input!(input);
37
38 with_features::with_features(args, input)
39 .map(|output| output.into_token_stream())
40 .unwrap_or_else(|err| err.into_compile_error())
41 .into()
42}