summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-11-19 13:40:28 +0100
committerMatthias Beyer <mail@beyermatthias.de>2017-11-19 14:00:27 +0100
commit0d2ed30311b8b11b45161586d4fd49f595696ecb (patch)
treee9669b783b30bb0ae248345b0f7fd6f46a5bd20b
parent68d423f4738202e608909aa18be4f14863466ef8 (diff)
Add tests for iterators
-rw-r--r--src/parser.rs224
1 files changed, 224 insertions, 0 deletions
diff --git a/src/parser.rs b/src/parser.rs
index 80ff151..723f4f3 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -774,5 +774,229 @@ mod tests {
assert_eq!(calc_res.get_years(), 0);
}
+ #[test]
+ fn test_iterator_1() {
+ let res = iterator(&b"2017-01-01 hourly"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for hour in 1..10 { // 10 is randomly chosen (fair dice roll... )
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , 01);
+ assert_eq!(tt.get_moment().unwrap().hour() , hour);
+ assert_eq!(tt.get_moment().unwrap().minute(), 00);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+
+ #[test]
+ fn test_iterator_2() {
+ let res = iterator(&b"2017-01-01 every 2mins"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for min in (1..60).into_iter().filter(|n| n % 2 == 0) {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , 01);
+ assert_eq!(tt.get_moment().unwrap().hour() , 00);
+ assert_eq!(tt.get_moment().unwrap().minute(), min);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+
+ #[test]
+ fn test_iterator_3() {
+ let res = iterator(&b"2017-01-01 daily"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for day in 2..30 {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , day);
+ assert_eq!(tt.get_moment().unwrap().hour() , 00);
+ assert_eq!(tt.get_moment().unwrap().minute(), 00);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+
+ #[test]
+ fn test_iterator_4() {
+ let res = iterator(&b"2017-01-01 weekly"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for week in 1..3 {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , 01 + (week * 7));
+ assert_eq!(tt.get_moment().unwrap().hour() , 00);
+ assert_eq!(tt.get_moment().unwrap().minute(), 00);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+
+ #[test]
+ fn test_until_spec_1() {
+ let res = until_spec(&b"until 2017-01-01T05:00:00"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+ }
+
+ #[test]
+ fn test_until_iterator_1() {
+ let res = iterator(&b"2017-01-01 hourly until 2017-01-01T05:00:00"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+ println!("Okay: {:#?}", ui);
+
+ for hour in 1..10 { // 10 is randomly chosen (fair dice roll... )
+ if hour > 5 - 1 {
+ let n = ui.next();
+ assert!(n.is_none(), "Is Some, should be None: {:?}", n);
+ return;
+ } else {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , 01);
+ assert_eq!(tt.get_moment().unwrap().hour() , hour);
+ assert_eq!(tt.get_moment().unwrap().minute(), 00);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+ }
+
+ #[test]
+ fn test_until_iterator_2() {
+ let res = iterator(&b"2017-01-01 every 2mins until 2017-01-01T00:10:00"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for min in (1..60).into_iter().filter(|n| n % 2 == 0) {
+ if min > 10 - 1 {
+ let n = ui.next();
+ assert!(n.is_none(), "Is Some, should be None: {:?}", n);
+ return;
+ } else {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , 01);
+ assert_eq!(tt.get_moment().unwrap().hour() , 00);
+ assert_eq!(tt.get_moment().unwrap().minute(), min);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+ }
+
+ #[test]
+ fn test_until_iterator_3() {
+ let res = iterator(&b"2017-01-01 daily until 2017-01-05"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for day in 2..30 {
+ if day > 5 - 1 {
+ let n = ui.next();
+ assert!(n.is_none(), "Is Some, should be None: {:?}", n);
+ return;
+ } else {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , day);
+ assert_eq!(tt.get_moment().unwrap().hour() , 00);
+ assert_eq!(tt.get_moment().unwrap().minute(), 00);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+ }
+
+ #[test]
+ fn test_until_iterator_4() {
+ let res = iterator(&b"2017-01-01 weekly until 2017-01-14"[..]);
+ assert!(res.is_done(), format!("Not done: {:?}", res));
+ let (_, i) = res.unwrap();
+ println!("{:#?}", i);
+
+ let ui : Result<UserIterator<iter::Iter>, _> = i.into_user_iterator();
+ assert!(ui.is_ok(), "Not okay: {:#?}", ui);
+ let mut ui = ui.unwrap();
+
+ for week in 1..3 {
+ if (week * 7 + 1) > 14 {
+ let n = ui.next();
+ assert!(n.is_none(), "Is Some, should be None: {:?}", n);
+ return;
+ } else {
+ let n = ui.next().unwrap();
+ assert!(n.is_ok(), "Not ok: {:#?}", n);
+ let tt = n.unwrap();
+ assert_eq!(tt.get_moment().unwrap().year() , 2017);
+ assert_eq!(tt.get_moment().unwrap().month() , 01);
+ assert_eq!(tt.get_moment().unwrap().day() , 01 + (week * 7));
+ assert_eq!(tt.get_moment().unwrap().hour() , 00);
+ assert_eq!(tt.get_moment().unwrap().minute(), 00);
+ assert_eq!(tt.get_moment().unwrap().second(), 00);
+ }
+ }
+ }
}