From cb1e092ccd4e81b454313d7bde560361b5b901f1 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 21 Dec 2022 01:44:32 -0500 Subject: [PATCH] tests on derived Display/Debug --- crates/types/enums/src/core/visibility.rs | 10 +++- .../enums/src/files/media_upload_type.rs | 10 +++- crates/types/enums/src/macros.rs | 54 ++++++++++++------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/crates/types/enums/src/core/visibility.rs b/crates/types/enums/src/core/visibility.rs index 2160927f27..7aa2c7e945 100644 --- a/crates/types/enums/src/core/visibility.rs +++ b/crates/types/enums/src/core/visibility.rs @@ -1,3 +1,8 @@ +#[cfg(test)] +use strum::EnumCount; +#[cfg(test)] +use strum::EnumIter; + /// Visibility /// /// Used in various database tables (as enums! careful!) and the HTTP API to convey @@ -11,7 +16,8 @@ /// /// *DO NOT CHANGE VALUES WITHOUT A MIGRATION STRATEGY!* /// -#[derive(Clone, Copy, Eq, PartialEq, Debug, Deserialize, Serialize, sqlx::Type)] +#[cfg_attr(test, derive(EnumIter, EnumCount))] +#[derive(Clone, Copy, Eq, PartialEq, Deserialize, Serialize, sqlx::Type)] #[serde(rename_all = "lowercase")] #[sqlx(rename_all = "lowercase")] pub enum Visibility { @@ -31,7 +37,7 @@ pub enum Visibility { } -impl_string_enum!(Visibility); +impl_enum_display_and_debug_for_to_str!(Visibility); // For reference, here's what the serde implementation might be if manually written. // This may be useful for designing composite types in the future: diff --git a/crates/types/enums/src/files/media_upload_type.rs b/crates/types/enums/src/files/media_upload_type.rs index 59736ee74d..d0b6c66237 100644 --- a/crates/types/enums/src/files/media_upload_type.rs +++ b/crates/types/enums/src/files/media_upload_type.rs @@ -8,10 +8,16 @@ use sqlx_core::encode::IsNull; use sqlx_core::error::BoxDynError; use sqlx_core::mysql::{MySql, MySqlTypeInfo, MySqlValueRef}; +#[cfg(test)] +use strum::EnumCount; +#[cfg(test)] +use strum::EnumIter; + /// Used in the `media_uploads` table in a `VARCHAR` field. /// /// DO NOT CHANGE VALUES WITHOUT A MIGRATION STRATEGY. -#[derive(Clone, Copy, Eq, PartialEq, Debug, Deserialize, Serialize)] +#[cfg_attr(test, derive(EnumIter, EnumCount))] +#[derive(Clone, Copy, Eq, PartialEq, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum MediaUploadType { /// Audio files: wav, mp3, etc. @@ -50,7 +56,7 @@ impl<'r> Decode<'r, MySql> for MediaUploadType { } } -impl_string_enum!(MediaUploadType); +impl_enum_display_and_debug_for_to_str!(MediaUploadType); /// NB: Legacy API for older code. impl MediaUploadType { diff --git a/crates/types/enums/src/macros.rs b/crates/types/enums/src/macros.rs index 089031a94d..79f7694e1d 100644 --- a/crates/types/enums/src/macros.rs +++ b/crates/types/enums/src/macros.rs @@ -1,30 +1,44 @@ -// Default methods for string enum types. -macro_rules! impl_string_enum { +/// Implement `Display` and `Debug` for enums that have a `.to_str()` method. +/// This ensures that the casing follows whatever `.to_str()` specifies. +macro_rules! impl_enum_display_and_debug_for_to_str { ($t:ident) => { -// // Constructors and accessors. -// impl $t { -// #[inline] -// pub fn new(value: String) -> Self { -// $t(value) -// } -// -// #[inline] -// pub fn new_from_str(value: &str) -> Self { -// $t(value.to_string()) -// } -// -// #[inline] -// pub fn as_str(&self) -> &str { -// &self.0 -// } -// } + + // Debug trait. + // Requires that the type has `.to_str()`. + impl std::fmt::Debug for $t { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.to_str()) + } + } // Display trait. + // Requires that the type has `.to_str()`. impl std::fmt::Display for $t { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) + write!(f, "{}", self.to_str()) + } + } + + // NB: This test requires `strum::{EnumIter}` on the enum + #[cfg(test)] + #[test] + fn test_display_trait_matches_to_str() { + use strum::IntoEnumIterator; + for variant in $t::iter() { + assert_eq!(format!("{}", variant), variant.to_str()); + } + } + + // NB: This test requires `strum::{EnumIter}` on the enum + #[cfg(test)] + #[test] + fn test_debug_trait_matches_to_str() { + use strum::IntoEnumIterator; + for variant in $t::iter() { + assert_eq!(format!("{:?}", variant), variant.to_str()); } } } } +