Skip to content

Commit d19bdf9

Browse files
committed
add test for string_enum
1 parent 12445e0 commit d19bdf9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Diff for: src/tools/compiletest/src/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ macro_rules! string_enum {
5151
}
5252
}
5353

54+
// Make the macro visible outside of this module, for tests.
55+
#[cfg(test)]
56+
pub(crate) use string_enum;
57+
5458
string_enum! {
5559
#[derive(Clone, Copy, PartialEq, Debug)]
5660
pub enum Mode {

Diff for: src/tools/compiletest/src/tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,30 @@ fn is_test_test() {
6666
assert!(!is_test(&OsString::from("#a_dog_gif")));
6767
assert!(!is_test(&OsString::from("~a_temp_file")));
6868
}
69+
70+
#[test]
71+
fn string_enums() {
72+
// These imports are needed for the macro-generated code
73+
use std::fmt;
74+
use std::str::FromStr;
75+
76+
crate::common::string_enum! {
77+
#[derive(Clone, Copy, Debug, PartialEq)]
78+
enum Animal {
79+
Cat => "meow",
80+
Dog => "woof",
81+
}
82+
}
83+
84+
// General assertions, mostly to silence the dead code warnings
85+
assert_eq!(Animal::VARIANTS.len(), 2);
86+
assert_eq!(Animal::STR_VARIANTS.len(), 2);
87+
88+
// Correct string conversions
89+
assert_eq!(Animal::Cat, "meow".parse().unwrap());
90+
assert_eq!(Animal::Dog, "woof".parse().unwrap());
91+
92+
// Invalid conversions
93+
let animal = "nya".parse::<Animal>();
94+
assert!(matches!(animal, Err(())));
95+
}

0 commit comments

Comments
 (0)