Skip to content

Commit

Permalink
add tests and finish some TODOs
Browse files Browse the repository at this point in the history
In service of #11.
  • Loading branch information
yshavit authored Jun 25, 2024
1 parent 1b12a07 commit d7a8fd6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
27 changes: 26 additions & 1 deletion src/fmt_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ where

// Always write the pending definitions at the end of the doc. If there were no sections, then BottomOfSection
// won't have been triggered, but we still want to write them
// TODO test this specific case
writer_state.write_definitions(out, DefinitionsToWrite::Both, true);
}

Expand Down Expand Up @@ -2031,6 +2030,32 @@ pub mod tests {
);
}

#[test]
fn no_sections_but_writing_to_sections() {
check_render_with(
&MdOptions {
link_reference_placement: ReferencePlacement::Section,
footnote_reference_placement: ReferencePlacement::Section,
},
md_elems![Block::LeafBlock::Paragraph {
body: vec![m_node!(Inline::Link {
text: vec![mdq_inline!("link description")],
link_definition: LinkDefinition {
url: "https://example.com".to_string(),
title: None,
reference: LinkReference::Full("1".to_string()),
},
}),]
}],
indoc! {r#"
[link description][1]
-----
[1]: https://example.com"#},
)
}

#[test]
fn only_footnote_in_section() {
check_render_with(
Expand Down
2 changes: 1 addition & 1 deletion src/fmt_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mod tests {
options.constructs.math_text = true;
let node = markdown::to_mdast(md, &options).unwrap();
let md_elems = MdElem::read(node, &ReadOptions::default()).unwrap();
unwrap!(&md_elems[0], MdElem::Block(Block::LeafBlock(LeafBlock::Paragraph(p)))); // TODO can I use m_node here?
unwrap!(&md_elems[0], MdElem::Block(Block::LeafBlock(LeafBlock::Paragraph(p))));
p.body.iter().for_each(|inline| VARIANTS_CHECKER.see(inline));
let actual = inlines_to_plain_string(&p.body);
assert_eq!(&actual, expect);
Expand Down
54 changes: 49 additions & 5 deletions src/parsing_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ impl Iterator for ParsingIterator<'_> {
#[cfg(test)]
mod tests {
use crate::parsing_iter::{ParsingIterator, Position};

// TODO need more test coverage: run just this file w/ test coverage to find out what's lacking
use crate::select::ParseErrorReason;

#[test]
fn basic() {
Expand Down Expand Up @@ -189,18 +188,63 @@ mod tests {
fn drop_while() {
let mut iter = ParsingIterator::new("A \t B");

assert_eq!("", iter.drop_while(|ch| ch.is_whitespace()));
assert_eq!(iter.drop_while(|ch| ch.is_whitespace()), "",);
next_and_check(&mut iter, Some('A'), Position { line: 0, column: 1 });

assert_eq!(" \t ", iter.drop_while(|ch| ch.is_whitespace()));
assert_eq!(iter.drop_while(|ch| ch.is_whitespace()), " \t ");
peek_and_check(&mut iter, Some('B'), Position { line: 0, column: 4 });
next_and_check(&mut iter, Some('B'), Position { line: 0, column: 5 });

assert_eq!("", iter.drop_while(|ch| ch.is_whitespace()));
assert_eq!(iter.drop_while(|ch| ch.is_whitespace()), "");
peek_and_check(&mut iter, None, Position { line: 0, column: 5 });
next_and_check(&mut iter, None, Position { line: 0, column: 5 });
}

#[test]
fn drop_whitespace() {
let mut iter = ParsingIterator::new(" \t\r\n\t B");
assert_eq!(iter.drop_whitespace(), " \t\r\n\t ");
assert_eq!(iter.next(), Some('B'));
}

#[test]
fn require_whitespace() {
let mut iter = ParsingIterator::new(" BC");
assert_eq!(iter.require_whitespace("foo"), Ok(()));
assert_eq!(iter.next(), Some('B'));
assert_eq!(
iter.require_whitespace("bar"),
Err(ParseErrorReason::InvalidSyntax(
"bar must be followed by whitespace".to_string()
))
);
assert_eq!(iter.next(), Some('C'));
assert_eq!(iter.require_whitespace("foo"), Ok(()),); // EOL counts as whitespace
}

#[test]
fn require_char() {
let mut iter = ParsingIterator::new("AB");
assert_eq!(iter.require_char('A'), Ok(()));
assert_eq!(iter.require_char('F'), Err(ParseErrorReason::Expected('F')));
}

#[test]
fn require_str() {
let mut iter = ParsingIterator::new("ABCD");
assert_eq!(iter.require_str("ABC"), Ok(()));
assert_eq!(iter.require_str("FGH"), Err(ParseErrorReason::Expected('F')));
}

#[test]
fn consume_if() {
let mut iter = ParsingIterator::new("ABC");
assert_eq!(iter.consume_if('A'), true);
assert_eq!(iter.position, Position { line: 0, column: 1 });
assert_eq!(iter.consume_if('F'), false);
assert_eq!(iter.position, Position { line: 0, column: 1 });
}

fn next_and_check(iter: &mut ParsingIterator, expect_ch: Option<char>, expect_pos: Position) {
let next_item = iter.next();
assert_eq!(next_item, expect_ch);
Expand Down

0 comments on commit d7a8fd6

Please sign in to comment.