diff options
| author | Rémy Rakic <remy.rakic+github@gmail.com> | 2025-01-12 08:05:50 +0000 |
|---|---|---|
| committer | Rémy Rakic <remy.rakic+github@gmail.com> | 2025-01-12 08:05:50 +0000 |
| commit | d19bdf9b48bdf520ad83cfa2af148a6b1ee8ab26 (patch) | |
| tree | 92b4488d40820284fda5aa2a8ca8516ef485f92a | |
| parent | 12445e0b2c532e389b8293924ed7c2b6fad5238f (diff) | |
| download | rust-d19bdf9b48bdf520ad83cfa2af148a6b1ee8ab26.tar.gz rust-d19bdf9b48bdf520ad83cfa2af148a6b1ee8ab26.zip | |
add test for `string_enum`
| -rw-r--r-- | src/tools/compiletest/src/common.rs | 4 | ||||
| -rw-r--r-- | src/tools/compiletest/src/tests.rs | 27 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 36f876bcdc6..8c4a89baa00 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 fec746904de..f8a74528101 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::<Animal>(); + assert!(matches!(animal, Err(()))); +} |
