tests on derived Display/Debug

This commit is contained in:
Brandon Thomas committed 2023-01-11 00:33:39 -05:00
1 parent 07d13a4ab5
commit cb1e092ccd
3 files changed
+50 -24

No files matched your search

+8 -2
View File
@@ -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:
@@ -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 {
+34 -20
View File
@@ -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());
}
}
}
}