From 25233cec4a949d8f80a60f6b21786600d85db50c Mon Sep 17 00:00:00 2001 From: Nick Groenen Date: Mon, 15 Feb 2021 12:09:08 +0100 Subject: [PATCH] Add some unit tests for ObsidianNoteReference::from_str --- src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index da15012..aa53848 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,7 +117,7 @@ struct Context { frontmatter_strategy: FrontmatterStrategy, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] /// ObsidianNoteReference represents the structure of a `[[note]]` or `![[embed]]` reference. struct ObsidianNoteReference<'a> { /// The file (note name or partial path) being referenced. @@ -761,3 +761,52 @@ fn codeblock_kind_to_owned<'a>(codeblock_kind: CodeBlockKind) -> CodeBlockKind<' CodeBlockKind::Fenced(cowstr) => CodeBlockKind::Fenced(CowStr::from(cowstr.into_string())), } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_note_refs_from_strings() { + assert_eq!( + ObsidianNoteReference::from_str("Just a note"), + ObsidianNoteReference { + file: Some("Just a note"), + label: None, + section: None, + } + ); + assert_eq!( + ObsidianNoteReference::from_str("A note?"), + ObsidianNoteReference { + file: Some("A note?"), + label: None, + section: None, + } + ); + assert_eq!( + ObsidianNoteReference::from_str("Note#with heading"), + ObsidianNoteReference { + file: Some("Note"), + label: None, + section: Some("with heading"), + } + ); + assert_eq!( + ObsidianNoteReference::from_str("Note#Heading|Label"), + ObsidianNoteReference { + file: Some("Note"), + label: Some("Label"), + section: Some("Heading"), + } + ); + assert_eq!( + ObsidianNoteReference::from_str("#Heading|Label"), + ObsidianNoteReference { + file: None, + label: Some("Label"), + section: Some("Heading"), + } + ); + } +}