2020-09-17 20:21:21 +02:00
|
|
|
use strum::{EnumIter, IntoEnumIterator};
|
2017-02-16 04:16:40 +01:00
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumIter)]
|
2017-02-16 04:16:40 +01:00
|
|
|
enum Week {
|
|
|
|
Sunday,
|
|
|
|
Monday,
|
|
|
|
Tuesday,
|
|
|
|
Wednesday,
|
|
|
|
Thursday,
|
|
|
|
Friday,
|
|
|
|
Saturday,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_test() {
|
|
|
|
let results = Week::iter().collect::<Vec<_>>();
|
2018-09-26 21:13:45 +02:00
|
|
|
let expected = vec![
|
|
|
|
Week::Sunday,
|
|
|
|
Week::Monday,
|
|
|
|
Week::Tuesday,
|
|
|
|
Week::Wednesday,
|
|
|
|
Week::Thursday,
|
|
|
|
Week::Friday,
|
|
|
|
Week::Saturday,
|
|
|
|
];
|
2017-02-16 04:16:40 +01:00
|
|
|
|
|
|
|
assert_eq!(expected, results);
|
|
|
|
}
|
|
|
|
|
2018-09-26 21:13:45 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, EnumIter)]
|
2017-02-16 04:16:40 +01:00
|
|
|
enum Complicated<U: Default, V: Default> {
|
|
|
|
A(U),
|
|
|
|
B { v: V },
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complicated_test() {
|
|
|
|
let results = Complicated::iter().collect::<Vec<_>>();
|
2018-09-26 21:13:45 +02:00
|
|
|
let expected = vec![
|
|
|
|
Complicated::A(0),
|
|
|
|
Complicated::B { v: String::new() },
|
|
|
|
Complicated::C,
|
|
|
|
];
|
2017-02-16 04:16:40 +01:00
|
|
|
|
|
|
|
assert_eq!(expected, results);
|
|
|
|
}
|
2018-03-10 22:06:17 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn len_test() {
|
2018-09-26 21:13:45 +02:00
|
|
|
let mut i = Complicated::<(), ()>::iter();
|
2018-03-10 22:06:17 +01:00
|
|
|
assert_eq!(3, i.len());
|
|
|
|
i.next();
|
2018-09-26 21:13:45 +02:00
|
|
|
|
2018-03-10 22:06:17 +01:00
|
|
|
assert_eq!(2, i.len());
|
|
|
|
i.next();
|
|
|
|
|
|
|
|
assert_eq!(1, i.len());
|
|
|
|
i.next();
|
|
|
|
|
|
|
|
assert_eq!(0, i.len());
|
2019-11-17 02:33:14 +01:00
|
|
|
i.next();
|
|
|
|
|
|
|
|
assert_eq!(0, i.size_hint().1.unwrap());
|
2018-03-10 22:06:17 +01:00
|
|
|
}
|
2018-04-16 18:43:32 +02:00
|
|
|
|
2019-12-19 23:44:30 +01:00
|
|
|
#[test]
|
|
|
|
fn double_ended_len_test() {
|
|
|
|
let mut i = Complicated::<(), ()>::iter();
|
|
|
|
assert_eq!(3, i.len());
|
|
|
|
i.next_back();
|
|
|
|
|
|
|
|
assert_eq!(2, i.len());
|
|
|
|
i.next();
|
|
|
|
|
|
|
|
assert_eq!(1, i.len());
|
|
|
|
i.next_back();
|
|
|
|
|
|
|
|
assert_eq!(0, i.len());
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:43:32 +02:00
|
|
|
#[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<_>>();
|
2018-09-26 21:13:45 +02:00
|
|
|
let expected = vec![
|
|
|
|
Week::Sunday,
|
|
|
|
Week::Monday,
|
|
|
|
Week::Tuesday,
|
|
|
|
Week::Wednesday,
|
|
|
|
Week::Thursday,
|
|
|
|
Week::Friday,
|
|
|
|
Week::Saturday,
|
|
|
|
Week::Sunday,
|
|
|
|
Week::Monday,
|
|
|
|
Week::Tuesday,
|
|
|
|
];
|
2018-04-16 18:43:32 +02:00
|
|
|
assert_eq!(expected, results);
|
|
|
|
}
|
2019-12-19 23:44:30 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reverse_test() {
|
|
|
|
let results = Week::iter().rev().collect::<Vec<_>>();
|
|
|
|
let expected = vec![
|
|
|
|
Week::Saturday,
|
|
|
|
Week::Friday,
|
|
|
|
Week::Thursday,
|
|
|
|
Week::Wednesday,
|
|
|
|
Week::Tuesday,
|
|
|
|
Week::Monday,
|
|
|
|
Week::Sunday,
|
|
|
|
];
|
|
|
|
assert_eq!(expected, results);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_from_both_sides_test() {
|
|
|
|
let mut iter = Week::iter();
|
|
|
|
|
|
|
|
assert_eq!(Some(Week::Sunday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Saturday), iter.next_back());
|
|
|
|
assert_eq!(Some(Week::Friday), iter.next_back());
|
|
|
|
assert_eq!(Some(Week::Monday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Tuesday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Wednesday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Thursday), iter.next_back());
|
|
|
|
assert_eq!(None, iter.next());
|
|
|
|
assert_eq!(None, iter.next_back());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_from_both_sides_test2() {
|
|
|
|
let mut iter = Week::iter();
|
|
|
|
|
|
|
|
assert_eq!(Some(Week::Sunday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Saturday), iter.next_back());
|
|
|
|
assert_eq!(Some(Week::Friday), iter.next_back());
|
|
|
|
assert_eq!(Some(Week::Monday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Tuesday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Wednesday), iter.next());
|
|
|
|
assert_eq!(Some(Week::Thursday), iter.next());
|
|
|
|
assert_eq!(None, iter.next_back());
|
|
|
|
assert_eq!(None, iter.next());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_nth_test() {
|
|
|
|
let mut iter = Week::iter();
|
2020-09-17 07:01:05 +02:00
|
|
|
let saturday = iter.next_back();
|
|
|
|
let friday = iter.next_back();
|
|
|
|
let thursday = iter.next_back();
|
2019-12-19 23:44:30 +01:00
|
|
|
|
|
|
|
assert_eq!(Some(Week::Tuesday), iter.nth(2));
|
2020-09-17 07:01:05 +02:00
|
|
|
assert_eq!(Some(Week::Saturday), saturday);
|
|
|
|
assert_eq!(Some(Week::Friday), friday);
|
|
|
|
assert_eq!(Some(Week::Thursday), thursday);
|
2019-12-19 23:44:30 +01:00
|
|
|
assert_eq!(None, iter.nth(1));
|
|
|
|
assert_eq!(None, iter.next());
|
|
|
|
assert_eq!(None, iter.next_back());
|
2020-03-02 00:55:53 +01:00
|
|
|
}
|
2021-07-17 05:40:19 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn crate_module_path_test() {
|
|
|
|
pub mod nested {
|
|
|
|
pub mod module {
|
|
|
|
pub use strum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, EnumIter)]
|
|
|
|
#[strum(crate = "nested::module::strum")]
|
|
|
|
enum Week {
|
|
|
|
Sunday,
|
|
|
|
Monday,
|
|
|
|
Tuesday,
|
|
|
|
Wednesday,
|
|
|
|
Thursday,
|
|
|
|
Friday,
|
|
|
|
Saturday,
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|