#[cfg(test)] mod walker_tests { use commons::walker::*; #[test] fn next() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); assert_eq!(walker.current(), &1); assert_eq!(walker.forward(), Some(&2)); assert_eq!(walker.forward(), Some(&3)); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); } #[test] fn prev() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.goto(EGoto::End); assert_eq!(walker.current(), &5); assert_eq!(walker.prev(), Some(&4)); assert_eq!(walker.prev(), Some(&3)); assert_eq!(walker.prev(), Some(&2)); assert_eq!(walker.prev(), Some(&1)); assert_eq!(walker.prev(), None); } #[test] fn goto() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.goto(EGoto::Target(3)); assert_eq!(walker.current(), &4); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::Begin); assert_eq!(walker.current(), &1); assert_eq!(walker.forward(), Some(&2)); assert_eq!(walker.forward(), Some(&3)); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::End); assert_eq!(walker.current(), &5); assert_eq!(walker.forward(), None); } #[test] fn segment() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); let mut segment = walker.segment(2, 4); assert_eq!(segment.current(), &3); assert_eq!(segment.forward(), Some(&4)); assert_eq!(segment.forward(), None); let mut segment = walker.segment(2, 4); assert_eq!(segment.current(), &3); assert_eq!(segment.forward(), Some(&4)); assert_eq!(segment.forward(), None); let mut segment = walker.segment(2, 4); assert_eq!(segment.current(), &3); assert_eq!(segment.forward(), Some(&4)); assert_eq!(segment.forward(), None); } #[test] fn take() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.goto(EGoto::Begin); let mut segment = walker.take(3); assert_eq!(segment.current(), &1); assert_eq!(segment.forward(), Some(&2)); assert_eq!(segment.forward(), Some(&3)); assert_eq!(segment.forward(), None); walker.goto(EGoto::Target(2)); let mut segment = walker.take(3); assert_eq!(segment.current(), &3); assert_eq!(segment.forward(), Some(&4)); assert_eq!(segment.forward(), Some(&5)); assert_eq!(segment.forward(), None); walker.goto(EGoto::End); let mut segment = walker.take(3); assert_eq!(segment.current(), &5); assert_eq!(segment.forward(), None); } #[test] fn at() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); assert_eq!(walker.at(0), Some(&1)); assert_eq!(walker.at(1), Some(&2)); assert_eq!(walker.at(2), Some(&3)); assert_eq!(walker.at(3), Some(&4)); assert_eq!(walker.at(4), Some(&5)); assert_eq!(walker.at(5), None); } #[test] fn foward() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.forward_custom(EMover::Delta(2)); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::Begin); walker.forward_custom(EMover::Until(|x| *x == 3)); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::Begin); walker.forward_custom(EMover::UntilSequence(vec![3, 4])); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); } #[test] fn backward() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.goto(EGoto::End); walker.backward(EMover::Delta(2)); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::End); walker.backward(EMover::Until(|x| *x == 3)); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::End); walker.backward(EMover::UntilSequence(vec![2, 3])); assert_eq!(walker.current(), &2); assert_eq!(walker.forward(), Some(&3)); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); } #[test] fn skip() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.skip(2); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); walker.goto(EGoto::Begin); walker.skip(2); assert_eq!(walker.current(), &3); assert_eq!(walker.forward(), Some(&4)); assert_eq!(walker.forward(), Some(&5)); assert_eq!(walker.forward(), None); assert_eq!(walker.forward(), None); } #[test] fn is_end() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.goto(EGoto::Begin); assert_eq!(walker.is_end(), false); walker.goto(EGoto::End); assert_eq!(walker.is_end(), true); walker.goto(EGoto::Target(2)); assert_eq!(walker.is_end(), false); } #[test] fn is_begin() { let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); walker.goto(EGoto::Begin); assert_eq!(walker.is_begin(), true); walker.goto(EGoto::End); assert_eq!(walker.is_begin(), false); walker.goto(EGoto::Target(2)); assert_eq!(walker.is_begin(), false); } // #[test] // fn match_sequence() { // let mut walker = Walker::new(vec![1, 2, 3, 4, 5]); // walker.goto(EGoto::Begin); // assert_eq!(walker.match_sequence(&[1, 2, 3]), true); // assert_eq!(walker.match_sequence(&[1, 2, 3, 4]), true); // assert_eq!(walker.match_sequence(&[1, 2, 3, 4, 5]), true); // assert_eq!(walker.match_sequence(&[1, 2, 3, 4, 5, 6]), false); // assert_eq!(walker.match_sequence(&[1, 2, 3, 4, 6]), false); // assert_eq!(walker.match_sequence(&[1, 2, 3, 6]), false); // assert_eq!(walker.match_sequence(&[1, 2, 6]), false); // assert_eq!(walker.match_sequence(&[1, 6]), false); // assert_eq!(walker.match_sequence(&[6]), false); // walker.goto(EGoto::Target(2)); // assert_eq!(walker.match_sequence(&[3, 4, 5]), true); // assert_eq!(walker.match_sequence(&[3, 4, 5, 6]), false); // assert_eq!(walker.match_sequence(&[3, 4, 6]), false); // assert_eq!(walker.match_sequence(&[3, 6]), false); // assert_eq!(walker.match_sequence(&[6]), false); // walker.goto(EGoto::End); // assert_eq!(walker.match_sequence(&[5]), true); // assert_eq!(walker.match_sequence(&[5, 6]), false); // assert_eq!(walker.match_sequence(&[6]), false); // } }