use equt_md_ext::{error::Result, Event, MarkdownExt, Parser, Tag};

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

#[test]
fn before() -> Result<()> {
    let mut parser = Parser::new(include_str!("data/before.md"))?.before(
        |e| match e {
            Event::Start(Tag::Heading(1)) => true,
            _ => false,
        },
        |fm| {
            vec![Event::Text(
                fm.as_ref()
                    .and_then(|fm| Some(fm.title.clone()))
                    .unwrap_or("Title".into())
                    .into(),
            )]
        },
    );
    assert_events!(
        parser,
        Event::Text("Before".into()),
        Event::Start(Tag::Heading(1)),
        Event::Text("First".into()),
        Event::End(Tag::Heading(1)),
        Event::Start(Tag::Heading(1)),
        Event::Text("Second".into()),
        Event::End(Tag::Heading(1))
    );
    Ok(())
}

#[test]
fn after() -> Result<()> {
    let mut parser = Parser::new(include_str!("data/after.md"))?.after(
        |e| match e {
            Event::End(Tag::Heading(1)) => true,
            _ => false,
        },
        |fm| {
            vec![Event::Text(
                fm.as_ref()
                    .and_then(|fm| Some(fm.title.clone()))
                    .unwrap_or("Title".into())
                    .into(),
            )]
        },
    );
    assert_events!(
        parser,
        Event::Start(Tag::Heading(1)),
        Event::Text("First".into()),
        Event::End(Tag::Heading(1)),
        Event::Text("After".into()),
        Event::Start(Tag::Heading(1)),
        Event::Text("Second".into()),
        Event::End(Tag::Heading(1))
    );
    Ok(())
}
