From d19bdf9b48bdf520ad83cfa2af148a6b1ee8ab26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Sun, 12 Jan 2025 08:05:50 +0000 Subject: [PATCH 1/2] add test for `string_enum` --- src/tools/compiletest/src/common.rs | 4 ++++ src/tools/compiletest/src/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 36f876bcdc604..8c4a89baa0081 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -51,6 +51,10 @@ macro_rules! string_enum { } } +// Make the macro visible outside of this module, for tests. +#[cfg(test)] +pub(crate) use string_enum; + string_enum! { #[derive(Clone, Copy, PartialEq, Debug)] pub enum Mode { diff --git a/src/tools/compiletest/src/tests.rs b/src/tools/compiletest/src/tests.rs index fec746904dedf..f8a74528101b7 100644 --- a/src/tools/compiletest/src/tests.rs +++ b/src/tools/compiletest/src/tests.rs @@ -66,3 +66,30 @@ fn is_test_test() { assert!(!is_test(&OsString::from("#a_dog_gif"))); assert!(!is_test(&OsString::from("~a_temp_file"))); } + +#[test] +fn string_enums() { + // These imports are needed for the macro-generated code + use std::fmt; + use std::str::FromStr; + + crate::common::string_enum! { + #[derive(Clone, Copy, Debug, PartialEq)] + enum Animal { + Cat => "meow", + Dog => "woof", + } + } + + // General assertions, mostly to silence the dead code warnings + assert_eq!(Animal::VARIANTS.len(), 2); + assert_eq!(Animal::STR_VARIANTS.len(), 2); + + // Correct string conversions + assert_eq!(Animal::Cat, "meow".parse().unwrap()); + assert_eq!(Animal::Dog, "woof".parse().unwrap()); + + // Invalid conversions + let animal = "nya".parse::(); + assert!(matches!(animal, Err(()))); +} From 33ce74f308ac0d849b9cd4d88f3ebdb4238be18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Sun, 12 Jan 2025 08:13:45 +0000 Subject: [PATCH 2/2] add error message to `string_enum!`s string conversions --- src/tools/compiletest/src/common.rs | 6 +++--- src/tools/compiletest/src/tests.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 8c4a89baa0081..4e2510ed9aba3 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -39,12 +39,12 @@ macro_rules! string_enum { } impl FromStr for $name { - type Err = (); + type Err = String; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { match s { $($repr => Ok(Self::$variant),)* - _ => Err(()), + _ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)), } } } diff --git a/src/tools/compiletest/src/tests.rs b/src/tools/compiletest/src/tests.rs index f8a74528101b7..43c6dc0a67e89 100644 --- a/src/tools/compiletest/src/tests.rs +++ b/src/tools/compiletest/src/tests.rs @@ -91,5 +91,5 @@ fn string_enums() { // Invalid conversions let animal = "nya".parse::(); - assert!(matches!(animal, Err(()))); + assert_eq!("unknown `Animal` variant: `nya`", animal.unwrap_err()); }