mirror of
https://github.com/danog/strum.git
synced 2024-12-04 02:17:56 +01:00
1c00f6cfa5
* Run rustfmt over repository. * Bump `syn` to 0.15 * Implemented ability to `serialize_all` using cases from `heck`. Issue #21 * Use `path` and `version` in dependency specifications. Issue #21 * Updated documentation. Issue #21 * Added `CHANGELOG.md`. * Also convert case when deriving `Display`. Issue #21 * Added `EnumDiscriminants` derive. Issue #33 * Added the ability to rename derived `EnumDiscriminants`. Issue #33 * Updated `README.md` and lib.rs docs. Issue #33 * Updated `CHANGELOG.md`. Issue #33 * WIP: refactoring to allow attributes on discriminants enum. * Use single `strum_discriminants` top level attribute. Issue #33 * Allow multiple declarations of `strum_discriminants` attribute. Issue #33 * Pass through all other attributes to discriminant enum. Issue #33 * Add `impl From<MyEnum> for MyEnumDiscriminants`. Issue #33 * Add `impl<'_enum> From<&'_enum MyEnum> for MyEnumDiscriminants`. Issue #33 * Added complex case test for `From` derivation. Issue #33 * Added docs to some helper functions. * Added docs about `From` impls. Issue #33
103 lines
1.9 KiB
Rust
103 lines
1.9 KiB
Rust
extern crate strum;
|
|
#[macro_use]
|
|
extern crate strum_macros;
|
|
|
|
use strum::IntoEnumIterator;
|
|
|
|
#[derive(Debug, Eq, PartialEq, EnumIter)]
|
|
enum Week {
|
|
Sunday,
|
|
Monday,
|
|
Tuesday,
|
|
Wednesday,
|
|
Thursday,
|
|
Friday,
|
|
Saturday,
|
|
}
|
|
|
|
#[test]
|
|
fn simple_test() {
|
|
let results = Week::iter().collect::<Vec<_>>();
|
|
let expected = vec![
|
|
Week::Sunday,
|
|
Week::Monday,
|
|
Week::Tuesday,
|
|
Week::Wednesday,
|
|
Week::Thursday,
|
|
Week::Friday,
|
|
Week::Saturday,
|
|
];
|
|
|
|
assert_eq!(expected, results);
|
|
}
|
|
|
|
#[derive(Debug, Eq, PartialEq, EnumIter)]
|
|
enum Complicated<U: Default, V: Default> {
|
|
A(U),
|
|
B { v: V },
|
|
C,
|
|
}
|
|
|
|
#[test]
|
|
fn complicated_test() {
|
|
let results = Complicated::iter().collect::<Vec<_>>();
|
|
let expected = vec![
|
|
Complicated::A(0),
|
|
Complicated::B { v: String::new() },
|
|
Complicated::C,
|
|
];
|
|
|
|
assert_eq!(expected, results);
|
|
}
|
|
|
|
#[test]
|
|
fn len_test() {
|
|
let mut i = Complicated::<(), ()>::iter();
|
|
assert_eq!(3, i.len());
|
|
i.next();
|
|
|
|
assert_eq!(2, i.len());
|
|
i.next();
|
|
|
|
assert_eq!(1, i.len());
|
|
i.next();
|
|
|
|
assert_eq!(0, i.len());
|
|
}
|
|
|
|
#[test]
|
|
fn clone_test() {
|
|
let mut i = Week::iter();
|
|
i.next();
|
|
i.next();
|
|
|
|
let mut i_cloned = i.clone();
|
|
|
|
assert_eq!(Some(Week::Tuesday), i.next());
|
|
assert_eq!(Some(Week::Tuesday), i_cloned.next());
|
|
|
|
i.next();
|
|
i.next();
|
|
|
|
assert_eq!(Some(Week::Friday), i.next());
|
|
assert_eq!(Some(Week::Wednesday), i_cloned.next());
|
|
}
|
|
|
|
#[test]
|
|
fn cycle_test() {
|
|
let results = Week::iter().cycle().take(10).collect::<Vec<_>>();
|
|
let expected = vec![
|
|
Week::Sunday,
|
|
Week::Monday,
|
|
Week::Tuesday,
|
|
Week::Wednesday,
|
|
Week::Thursday,
|
|
Week::Friday,
|
|
Week::Saturday,
|
|
Week::Sunday,
|
|
Week::Monday,
|
|
Week::Tuesday,
|
|
];
|
|
assert_eq!(expected, results);
|
|
}
|