1
0
mirror of https://github.com/danog/strum.git synced 2024-11-26 20:14:40 +01:00

Add test and comment if camel_case is specified (#250)

Co-authored-by: Peter Glotfelty <glotfelty.2@osu.edu>
This commit is contained in:
bouzuya 2023-07-30 07:00:13 +09:00 committed by GitHub
parent fd519ec47f
commit fcb9841a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 9 deletions

View File

@ -59,12 +59,14 @@ impl FromStr for CaseStyle {
fn from_str(text: &str) -> Result<Self, ()> {
Ok(match text {
"camel_case" | "PascalCase" => CaseStyle::PascalCase,
// "camel_case" is a soft-deprecated case-style left for backward compatibility.
// <https://github.com/Peternator7/strum/pull/250#issuecomment-1374682221>
"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() {
#[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());
}
}

View File

@ -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));
}