1
0
mirror of https://github.com/danog/strum.git synced 2024-12-11 17:09:36 +01:00
strum/strum_tests/tests/from_repr.rs
kraktus fd519ec47f
Fix EnumIter macro code generation (#287)
* Add test to ensure macro call `::core`-related functions

Avoiding local core modules to break the macro-generated code.

Currently failing due to issue with `EnumIter` macros.

* Fix macro of `EnumIter`

close https://github.com/Peternator7/strum/issues/284

---------

Co-authored-by: kraktus <kraktus@users.noreply.github.com>
2023-07-29 14:58:18 -07:00

67 lines
1.6 KiB
Rust

use strum::FromRepr;
mod core {} // ensure macros call `::core`
#[derive(Debug, FromRepr, PartialEq)]
/// Day of the week
#[repr(u8)]
enum Week {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday = 4 + 3,
Saturday = 8,
}
#[test]
fn simple_test() {
assert_eq!(Week::from_repr(0), Some(Week::Sunday));
assert_eq!(Week::from_repr(1), Some(Week::Monday));
assert_eq!(Week::from_repr(6), None);
assert_eq!(Week::from_repr(7), Some(Week::Friday));
assert_eq!(Week::from_repr(8), Some(Week::Saturday));
assert_eq!(Week::from_repr(9), None);
}
#[rustversion::since(1.46)]
#[test]
fn const_test() {
// This is to test that it works in a const fn
const fn from_repr(discriminant: u8) -> Option<Week> {
Week::from_repr(discriminant)
}
assert_eq!(from_repr(0), Some(Week::Sunday));
assert_eq!(from_repr(1), Some(Week::Monday));
assert_eq!(from_repr(6), None);
assert_eq!(from_repr(7), Some(Week::Friday));
assert_eq!(from_repr(8), Some(Week::Saturday));
assert_eq!(from_repr(9), None);
}
#[test]
fn crate_module_path_test() {
pub mod nested {
pub mod module {
pub use strum;
}
}
#[derive(Debug, FromRepr, PartialEq)]
#[strum(crate = "nested::module::strum")]
enum Week {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}
assert_eq!(Week::from_repr(0), Some(Week::Sunday));
assert_eq!(Week::from_repr(6), Some(Week::Saturday));
assert_eq!(Week::from_repr(7), None);
}