test: Added integration tests for the ValidateTheme macro
This commit is contained in:
@@ -5,7 +5,7 @@ use darling::FromVariant;
|
||||
use quote::quote;
|
||||
use syn::{Data, DeriveInput, parse_macro_input};
|
||||
|
||||
/// Derive macro for the EnumDisplayStyle trait.
|
||||
/// Derive macro for generating a `to_display_str` method for an enum.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@@ -16,13 +16,12 @@ use syn::{Data, DeriveInput, parse_macro_input};
|
||||
///
|
||||
/// #[derive(EnumDisplayStyle)]
|
||||
/// enum Weekend {
|
||||
/// Saturday,
|
||||
/// Sunday,
|
||||
/// Saturday,
|
||||
/// Sunday,
|
||||
/// }
|
||||
///
|
||||
/// assert_eq!(Weekend::Saturday.to_display_str(), "Saturday");
|
||||
/// assert_eq!(Weekend::Sunday.to_display_str(), "Sunday");
|
||||
///
|
||||
/// ```
|
||||
///
|
||||
/// Using custom values for the display style:
|
||||
@@ -32,10 +31,10 @@ use syn::{Data, DeriveInput, parse_macro_input};
|
||||
///
|
||||
/// #[derive(EnumDisplayStyle)]
|
||||
/// enum MonitorStatus {
|
||||
/// #[display_style(name = "Monitor Transactions")]
|
||||
/// Active,
|
||||
/// #[display_style(name = "Don't Monitor Transactions")]
|
||||
/// None,
|
||||
/// #[display_style(name = "Monitor Transactions")]
|
||||
/// Active,
|
||||
/// #[display_style(name = "Don't Monitor Transactions")]
|
||||
/// None,
|
||||
/// }
|
||||
///
|
||||
/// assert_eq!(MonitorStatus::Active.to_display_str(), "Monitor Transactions");
|
||||
|
||||
@@ -9,3 +9,6 @@ proc-macro = true
|
||||
[dependencies]
|
||||
quote = "1.0.39"
|
||||
syn = "2.0.99"
|
||||
|
||||
[dev-dependencies]
|
||||
log = "0.4.17"
|
||||
|
||||
@@ -2,6 +2,69 @@ use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, Data, DeriveInput, Fields};
|
||||
|
||||
/// Derive macro for generating a `validate` method for a Theme struct.
|
||||
/// The `validate` method ensures that all values with the `validate` attribute are not `None`.
|
||||
/// Otherwise, an error message it output to both the log file and stdout and the program exits.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// Valid themes pass through the program transitively without any messages being output.
|
||||
///
|
||||
/// ```
|
||||
/// use validate_theme_derive::ValidateTheme;
|
||||
///
|
||||
/// #[derive(ValidateTheme, Default)]
|
||||
/// struct Theme {
|
||||
/// pub name: String,
|
||||
/// #[validate]
|
||||
/// pub good: Option<Style>,
|
||||
/// #[validate]
|
||||
/// pub bad: Option<Style>,
|
||||
/// pub ugly: Option<Style>,
|
||||
/// }
|
||||
///
|
||||
/// struct Style {
|
||||
/// color: String,
|
||||
/// }
|
||||
///
|
||||
/// let theme = Theme {
|
||||
/// good: Some(Style { color: "Green".to_owned() }),
|
||||
/// bad: Some(Style { color: "Red".to_owned() }),
|
||||
/// ..Theme::default()
|
||||
/// };
|
||||
///
|
||||
/// // Since only `good` and `bad` have the `validate` attribute, the `validate` method will only check those fields.
|
||||
/// theme.validate();
|
||||
/// // Since both `good` and `bad` have values, the program will not exit and no message is output.
|
||||
/// ```
|
||||
///
|
||||
/// Invalid themes will output an error message to both the log file and stdout and the program will exit.
|
||||
///
|
||||
/// ```should_panic
|
||||
/// use validate_theme_derive::ValidateTheme;
|
||||
///
|
||||
/// #[derive(ValidateTheme, Default)]
|
||||
/// struct Theme {
|
||||
/// pub name: String,
|
||||
/// #[validate]
|
||||
/// pub good: Option<Style>,
|
||||
/// #[validate]
|
||||
/// pub bad: Option<Style>,
|
||||
/// pub ugly: Option<Style>,
|
||||
/// }
|
||||
///
|
||||
/// struct Style {
|
||||
/// color: String,
|
||||
/// }
|
||||
///
|
||||
/// let theme = Theme {
|
||||
/// bad: Some(Style { color: "Red".to_owned() }),
|
||||
/// ..Theme::default()
|
||||
/// };
|
||||
///
|
||||
/// // Since `good` has the `validate` attribute and since `good` is `None`, the `validate` method will output an error message and exit the program.
|
||||
/// theme.validate();
|
||||
/// ```
|
||||
#[proc_macro_derive(ValidateTheme, attributes(validate))]
|
||||
pub fn derive_validate_theme(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
@@ -22,8 +85,8 @@ pub fn derive_validate_theme(input: TokenStream) -> TokenStream {
|
||||
validation_checks.push(quote! {
|
||||
if self.#field_name.is_none() {
|
||||
log::error!("{} is missing a color value.", stringify!(#field_name));
|
||||
eprintln!("{} is missing a color value.", stringify!(#field_name));
|
||||
process::exit(1);
|
||||
eprintln!("{} is missing a color value.", stringify!(#field_name));
|
||||
std::process::exit(1);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user