diff --git a/src/parser/comment.rs b/src/parser/comment.rs index c301a5fe..81628774 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -69,22 +69,21 @@ fn comment_line<'r, 's>( #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; #[test] fn require_space_after_hash() { - let input = "# Comment line + let input = OrgSource::new( + "# Comment line #not a comment -# Comment again"; +# Comment again", + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let comment_matcher = parser_with_context!(comment)(&document_context); + let comment_matcher = parser_with_context!(comment)(&initial_context); let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment"); assert_eq!( - remaining, + Into::<&str>::into(remaining), r#"#not a comment # Comment again"# ); diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 738c3240..91eceb23 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -111,27 +111,26 @@ fn footnote_definition_end<'r, 's>( #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::Source; #[test] fn two_paragraphs() { - let input = "[fn:1] A footnote. + let input = OrgSource::new( + "[fn:1] A footnote. [fn:2] A multi- -line footnote."; +line footnote.", + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let footnote_definition_matcher = parser_with_context!(element(true))(&document_context); + let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_footnote_definition) = footnote_definition_matcher(input).expect("Parse first footnote_definition"); let (remaining, second_footnote_definition) = footnote_definition_matcher(remaining).expect("Parse second footnote_definition."); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!( first_footnote_definition.get_source(), "[fn:1] A footnote. @@ -148,19 +147,19 @@ line footnote." #[test] fn multiline_break() { - let input = "[fn:2] A multi- + let input = OrgSource::new( + "[fn:2] A multi- line footnote. -not in the footnote."; +not in the footnote.", + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let footnote_definition_matcher = parser_with_context!(element(true))(&document_context); + let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_footnote_definition) = footnote_definition_matcher(input).expect("Parse first footnote_definition"); - assert_eq!(remaining, "not in the footnote."); + assert_eq!(Into::<&str>::into(remaining), "not in the footnote."); assert_eq!( first_footnote_definition.get_source(), "[fn:2] A multi- diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 5ebc0377..1aecc014 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -45,7 +45,7 @@ impl<'s> OrgSource<'s> { } pub fn get_preceding_character(&self) -> Option { - todo!() + self.preceding_character } pub fn is_at_start_of_line(&self) -> bool { @@ -357,4 +357,12 @@ mod tests { assert_eq!(input.slice(6..).is_at_start_of_line(), true); assert_eq!(input.slice(6..).slice(10..).is_at_start_of_line(), false); } + + #[test] + fn preceding_character_unicode() { + let input = OrgSource::new("๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ"); + assert_eq!(input.get_preceding_character(), None); + assert_eq!(input.slice(8..).get_preceding_character(), Some('๐Ÿ’›')); + } + } diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 65f11b74..8f91e121 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -69,22 +69,20 @@ fn paragraph_end<'r, 's>( #[cfg(test)] mod tests { use crate::parser::element_parser::element; - use crate::parser::parser_context::ContextElement; + use crate::parser::org_source::OrgSource; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::source::Source; #[test] fn two_paragraphs() { - let input = "foo bar baz\n\nlorem ipsum"; + let input = OrgSource::new("foo bar baz\n\nlorem ipsum"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let paragraph_matcher = parser_with_context!(element(true))(&document_context); + let paragraph_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, second_paragraph) = paragraph_matcher(remaining).expect("Parse second paragraph."); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(first_paragraph.get_source(), "foo bar baz\n\n"); assert_eq!(second_paragraph.get_source(), "lorem ipsum"); } diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index b3edfae8..810784e5 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -239,67 +239,56 @@ fn get_context_item_indent<'r, 's>(context: Context<'r, 's>) -> Option<&'r usize #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::Source; #[test] fn plain_list_item_empty() { - let input = "1."; + let input = OrgSource::new("1."); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context); + let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context); let (remaining, result) = plain_list_item_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1."); } #[test] fn plain_list_item_simple() { - let input = "1. foo"; + let input = OrgSource::new("1. foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context); + let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context); let (remaining, result) = plain_list_item_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1. foo"); } #[test] fn plain_list_empty() { - let input = "1."; + let input = OrgSource::new("1."); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let (remaining, result) = plain_list_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1."); } #[test] fn plain_list_simple() { - let input = "1. foo"; + let input = OrgSource::new("1. foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let (remaining, result) = plain_list_matcher(input).unwrap(); - assert_eq!(remaining, ""); + assert_eq!(Into::<&str>::into(remaining), ""); assert_eq!(result.source, "1. foo"); } #[test] fn plain_list_cant_start_line_with_asterisk() { // Plain lists with an asterisk bullet must be indented or else they would be a headline - let input = "* foo"; + let input = OrgSource::new("* foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let result = plain_list_matcher(input); assert!(result.is_err()); } @@ -307,32 +296,30 @@ mod tests { #[test] fn indented_can_start_line_with_asterisk() { // Plain lists with an asterisk bullet must be indented or else they would be a headline - let input = " * foo"; + let input = OrgSource::new(" * foo"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(plain_list)(&document_context); + let plain_list_matcher = parser_with_context!(plain_list)(&initial_context); let result = plain_list_matcher(input); assert!(result.is_ok()); } #[test] fn two_blank_lines_ends_list() { - let input = r#"1. foo + let input = OrgSource::new( + r#"1. foo 2. bar baz 3. lorem ipsum -"#; +"#, + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(element(true))(&document_context); + let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, result) = plain_list_matcher(input).expect("Should parse the plain list successfully."); - assert_eq!(remaining, " ipsum\n"); + assert_eq!(Into::<&str>::into(remaining), " ipsum\n"); assert_eq!( result.get_source(), r#"1. foo @@ -347,18 +334,18 @@ mod tests { #[test] fn two_blank_lines_ends_nested_list() { - let input = r#"1. foo + let input = OrgSource::new( + r#"1. foo 1. bar -baz"#; +baz"#, + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(element(true))(&document_context); + let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, result) = plain_list_matcher(input).expect("Should parse the plain list successfully."); - assert_eq!(remaining, "baz"); + assert_eq!(Into::<&str>::into(remaining), "baz"); assert_eq!( result.get_source(), r#"1. foo @@ -371,7 +358,8 @@ baz"#; #[test] fn interior_trailing_whitespace() { - let input = r#"1. foo + let input = OrgSource::new( + r#"1. foo bar @@ -382,14 +370,13 @@ baz"#; ipsum -dolar"#; +dolar"#, + ); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_list_matcher = parser_with_context!(element(true))(&document_context); + let plain_list_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, result) = plain_list_matcher(input).expect("Should parse the plain list successfully."); - assert_eq!(remaining, "dolar"); + assert_eq!(Into::<&str>::into(remaining), "dolar"); assert_eq!( result.get_source(), r#"1. foo diff --git a/src/parser/plain_text.rs b/src/parser/plain_text.rs index bdc9deb9..7d0423d7 100644 --- a/src/parser/plain_text.rs +++ b/src/parser/plain_text.rs @@ -69,20 +69,17 @@ mod tests { use nom::combinator::map; use super::*; - use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ContextTree; use crate::parser::parser_with_context::parser_with_context; use crate::parser::source::Source; #[test] fn plain_text_simple() { - let input = "foobarbaz"; + let input = OrgSource::new("foobarbaz"); let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = - initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - let plain_text_matcher = parser_with_context!(plain_text)(&document_context); + let plain_text_matcher = parser_with_context!(plain_text)(&initial_context); let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap(); - assert_eq!(remaining, ""); - assert_eq!(result.get_source(), input); + assert_eq!(Into::<&str>::into(remaining), ""); + assert_eq!(result.get_source(), Into::<&str>::into(input)); } } diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index 1bef2340..e52d2708 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -144,11 +144,10 @@ mod tests { #[test] fn plain_text_radio_target() { - let input = "foo bar baz"; + let input = OrgSource::new("foo bar baz"); let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })]; let initial_context: ContextTree<'_, '_> = ContextTree::new(); let document_context = initial_context - .with_additional_node(ContextElement::DocumentRoot(input)) .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); let paragraph_matcher = parser_with_context!(element(true))(&document_context); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); @@ -173,14 +172,13 @@ mod tests { #[test] fn bold_radio_target() { - let input = "foo *bar* baz"; + let input = OrgSource::new("foo *bar* baz"); let radio_target_match = vec![Object::Bold(Bold { source: "*bar*", children: vec![Object::PlainText(PlainText { source: "bar" })], })]; let initial_context: ContextTree<'_, '_> = ContextTree::new(); let document_context = initial_context - .with_additional_node(ContextElement::DocumentRoot(input)) .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); let paragraph_matcher = parser_with_context!(element(true))(&document_context); let (remaining, first_paragraph) = diff --git a/src/parser/util.rs b/src/parser/util.rs index a62197be..2306cd5c 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -206,18 +206,3 @@ pub fn not_yet_implemented() -> Res, ()> { "Not implemented yet.".into(), )))); } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn get_one_before_unicode() { - let input = "๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ"; - let (green_heart_index, _) = input.char_indices().skip(2).next().unwrap(); - let starting_with_green_heart = &input[green_heart_index..]; - let yellow_heart = get_one_before(input, starting_with_green_heart).unwrap(); - assert!(is_slice_of(input, yellow_heart)); - assert_eq!(yellow_heart, "๐Ÿ’›"); - } -}