From fcb9841a744543b993eb1645db35dbda541e9a30 Mon Sep 17 00:00:00 2001 From: bouzuya Date: Sun, 30 Jul 2023 07:00:13 +0900 Subject: [PATCH] Add test and comment if `camel_case` is specified (#250) Co-authored-by: Peter Glotfelty --- strum_macros/src/helpers/case_style.rs | 56 +++++++++++++++++++++----- strum_tests/tests/serialize_all.rs | 27 +++++++++++++ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/strum_macros/src/helpers/case_style.rs b/strum_macros/src/helpers/case_style.rs index d714f86..d6b2270 100644 --- a/strum_macros/src/helpers/case_style.rs +++ b/strum_macros/src/helpers/case_style.rs @@ -59,12 +59,14 @@ impl FromStr for CaseStyle { fn from_str(text: &str) -> Result { Ok(match text { - "camel_case" | "PascalCase" => CaseStyle::PascalCase, + // "camel_case" is a soft-deprecated case-style left for backward compatibility. + // + "PascalCase" | "camel_case" => CaseStyle::PascalCase, "camelCase" => CaseStyle::CamelCase, "snake_case" | "snek_case" => CaseStyle::SnakeCase, - "kebab_case" | "kebab-case" => CaseStyle::KebabCase, + "kebab-case" | "kebab_case" => CaseStyle::KebabCase, "SCREAMING-KEBAB-CASE" => CaseStyle::ScreamingKebabCase, - "shouty_snake_case" | "shouty_snek_case" | "SCREAMING_SNAKE_CASE" => { + "SCREAMING_SNAKE_CASE" | "shouty_snake_case" | "shouty_snek_case" => { CaseStyle::ShoutySnakeCase } "title_case" => CaseStyle::TitleCase, @@ -113,10 +115,46 @@ impl CaseStyleHelpers for Ident { } } -#[test] -fn test_convert_case() { - let id = Ident::new("test_me", proc_macro2::Span::call_site()); - assert_eq!("testMe", id.convert_case(Some(CaseStyle::CamelCase))); - assert_eq!("TestMe", id.convert_case(Some(CaseStyle::PascalCase))); - assert_eq!("Test-Me", id.convert_case(Some(CaseStyle::TrainCase))); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_convert_case() { + let id = Ident::new("test_me", proc_macro2::Span::call_site()); + assert_eq!("testMe", id.convert_case(Some(CaseStyle::CamelCase))); + assert_eq!("TestMe", id.convert_case(Some(CaseStyle::PascalCase))); + assert_eq!("Test-Me", id.convert_case(Some(CaseStyle::TrainCase))); + } + + #[test] + fn test_impl_from_str_for_case_style_pascal_case() { + use CaseStyle::*; + let f = CaseStyle::from_str; + + assert_eq!(PascalCase, f("PascalCase").unwrap()); + assert_eq!(PascalCase, f("camel_case").unwrap()); + + assert_eq!(CamelCase, f("camelCase").unwrap()); + + assert_eq!(SnakeCase, f("snake_case").unwrap()); + assert_eq!(SnakeCase, f("snek_case").unwrap()); + + assert_eq!(KebabCase, f("kebab-case").unwrap()); + assert_eq!(KebabCase, f("kebab_case").unwrap()); + + assert_eq!(ScreamingKebabCase, f("SCREAMING-KEBAB-CASE").unwrap()); + + assert_eq!(ShoutySnakeCase, f("SCREAMING_SNAKE_CASE").unwrap()); + assert_eq!(ShoutySnakeCase, f("shouty_snake_case").unwrap()); + assert_eq!(ShoutySnakeCase, f("shouty_snek_case").unwrap()); + + assert_eq!(LowerCase, f("lowercase").unwrap()); + + assert_eq!(UpperCase, f("UPPERCASE").unwrap()); + + assert_eq!(TitleCase, f("title_case").unwrap()); + + assert_eq!(MixedCase, f("mixed_case").unwrap()); + } } diff --git a/strum_tests/tests/serialize_all.rs b/strum_tests/tests/serialize_all.rs index 5798fa6..84895d1 100644 --- a/strum_tests/tests/serialize_all.rs +++ b/strum_tests/tests/serialize_all.rs @@ -34,3 +34,30 @@ fn test_serialize_all_upper_case() { assert_eq!(Foo2::DarkBlack, Foo2::from_str("DARKBLACK").unwrap()); assert_eq!("DARKBLACK", <&'static str>::from(Foo2::DarkBlack)); } + +// This is a soft-deprecated behavior. Use `camelCase` instead. +#[derive(Debug, Eq, PartialEq, EnumString, Display, IntoStaticStr)] +#[strum(serialize_all = "camel_case")] +enum Foo3 { + CamelCase, +} + +#[test] +fn test_serialize_all_written_in_snake_case_camel_case() { + assert_eq!("CamelCase", Foo3::CamelCase.to_string()); + assert_eq!(Foo3::CamelCase, Foo3::from_str("CamelCase").unwrap()); + assert_eq!("CamelCase", <&'static str>::from(Foo3::CamelCase)); +} + +#[derive(Debug, Eq, PartialEq, EnumString, Display, IntoStaticStr)] +#[strum(serialize_all = "camelCase")] +enum Foo4 { + CamelCase, +} + +#[test] +fn test_serialize_all_camel_case() { + assert_eq!("camelCase", Foo4::CamelCase.to_string()); + assert_eq!(Foo4::CamelCase, Foo4::from_str("camelCase").unwrap()); + assert_eq!("camelCase", <&'static str>::from(Foo4::CamelCase)); +}