use mercurius::*;
use std::cell::RefCell;
use std::iter::once;
use std::rc::Rc;

macro_rules! assert_events {
    ($iter:expr, $( $event:expr), *) => {
        $(
            assert_eq!($iter.next(), Some($event));
        )*
        assert_eq!($iter.next(), None);
    };
}

#[test]
fn link() {
    let mut iter = Raw::new(Rc::new(RefCell::new(None)).into(), once(()))
        .after(|&e| e == (), |_| once(()))
        .iter(once(()))
        .iter(once(()))
        .link(Raw::new(Rc::new(RefCell::new(None)).into(), once(())).iter(once(())));
    assert_events!(iter, (), (), (), (), (), ());
}

#[test]
fn inspect() -> Result<()> {
    let mut fm = Rc::new(RefCell::new(None));
    let mut iter = Raw::new(Rc::new(RefCell::new(Some(FrontMatter {
        title: "Foo".into(),
        create_date: ReadableDate::new(2000, 1, 1),
        ..Default::default()
    }))).into(), once(()))
        .after(|&e| e == (), |_| once(()))
        .iter(once(()))
        .inspect_frontmatter(&mut fm);
    assert_events!(iter, (), (), ());
    assert_eq!(fm.borrow().as_ref().unwrap().title, "Foo");
    Ok(())
}
