//! The pieces the Bear-like Notes adds to the writing rule: note links,//! folds, contents, picture widths, find, search words and tag icons. Every//! note is invented.usesuper::format::{apply, Action};usesuper::style::{style, Kind, Style};usesuper::{derive, find, folds, icons, links, search, Selection};#[test]fn a_note_link_is_drawn_as_a_link_with_its_brackets_marks() {let text = "See [[Garden plans]] and [[Seeds/Spring]] but not [[]] or `[[code]]`.";let styled = style(text, None);let linked: Vec<&str> = styled.spans.iter().filter(|s| s.style == Style::NoteLink).map(|s| s.url.as_deref().unwrap()).collect(); assert_eq!(linked, ["Garden plans", "Seeds/Spring"]);let found = links::links(text); assert_eq!(found.len(), 2); assert_eq!(&text[found[0].start as usize..found[0].end as usize], "[[Garden plans]]");// The brackets are marks, hidden away from the cursor; the words are plain. assert!(styled.spans.iter().any(|s| s.style == Style::Mark && s.start == 4 && s.end == 6)); assert_eq!(derive::derive(text).preview, "See Garden plans and Seeds/Spring but not [[]] or [[code]].");}#[test]fn a_link_reads_as_a_title_or_a_title_and_its_heading() {let titles = ["garden plans", "2024/25 budget"];let is = |t: &str| titles.contains(&links::title_key(t).as_str()); assert_eq!(links::split("Garden Plans", is), ("Garden Plans", None)); assert_eq!(links::split("Garden plans/Spring", is), ("Garden plans", Some("Spring"))); assert_eq!(links::split("2024/25 budget", is), ("2024/25 budget", None)); assert_eq!(links::split("Nowhere/at all", is), ("Nowhere/at all", None));}#[test]fn a_rename_rewrites_the_links_that_named_the_old_title() {let text = "One [[Garden]], two [[garden/Spring]], and [[Gardens]] stays.";let none = |_: &str| false; assert_eq!(links::renamed(text, "Garden", "Allotment", none).unwrap(), "One [[Allotment]], two [[Allotment/Spring]], and [[Gardens]] stays."); assert_eq!(links::renamed("No links here", "Garden", "Allotment", none), None);}#[test]fn the_link_being_typed_is_found_and_ends_at_a_closing_pair() {let text = "Go to [[gar"; assert_eq!(links::typing(text, 11), Some((6, 11, "gar".to_string())));let closed = "Go to [[gar]] now"; assert_eq!(links::typing(closed, 11), Some((6, 13, "gar".to_string()))); assert_eq!(links::typing("[[done]] after", 14), None); assert_eq!(links::typing("no link", 3), None);// The bar's key makes the pair with the cursor between.let made = apply("Go to ", Selection::cursor(6), &Action::NoteLink { title: None }); assert_eq!((made.text.as_str(), made.selection.start), ("Go to [[]]", 8));let named = apply("Go to ", Selection::cursor(6), &Action::NoteLink { title: Some("Garden".into()) }); assert_eq!(named.text, "Go to [[Garden]]");}#[test]fn headings_fold_their_section_and_items_their_deeper_items() {let text = "# Title\nintro\n## A\none\n\n### A1\ndeep\n## B\n- top\n - under\n - under too\n- next\n";let all = folds::folds(text);let at = |line: &str| text.find(line).unwrap() as u32;let title = all.iter().find(|f| f.line == 0).unwrap(); assert_eq!(&text[title.start as usize..title.end as usize], "\nintro\n## A\none\n\n### A1\ndeep\n## B\n- top\n - under\n - under too\n- next");let a = all.iter().find(|f| f.line == at("## A")).unwrap(); assert_eq!(&text[a.start as usize..a.end as usize], "\none\n\n### A1\ndeep");let top = all.iter().find(|f| f.line == at("- top")).unwrap(); assert_eq!(top.kind, "list"); assert_eq!(&text[top.start as usize..top.end as usize], "\n - under\n - under too"); assert!(all.iter().all(|f| f.line != at("- next")));let contents: Vec<(u8, String)> = folds::contents(text).into_iter().map(|h| (h.level, h.words)).collect(); assert_eq!(contents, [(1, "Title".into()), (2, "A".into()), (3, "A1".into()), (2, "B".into())]); assert_eq!(folds::heading_at(text, "a1"), Some(at("### A1")));}#[test]fn a_picture_wears_a_fixed_width_written_as_the_reference_app_writes_it() {let text = "# Pics\n\n<!-- {\"width\":245} -->\n<!-- {\"width\":100} -->\n";let styled = style(text, None);let widths: Vec<&str> = styled.blocks.iter().filter(|b| b.kind == Kind::Picture).map(|b| b.width.as_deref().unwrap()).collect(); assert_eq!(widths, ["full", "medium", "small"]); assert_eq!(derive::pictures(text), ["photo:abc", "photo:def", "photo:ghi"]);// The comment is a mark: the preview and the words never show it. assert_eq!(derive::derive("# T\nsee <!-- {\"width\":160} --> here").preview, "see a cat here");let small = apply(text, Selection::cursor(10), &Action::PictureWidth { width: "small".into() }); assert!(small.text.starts_with("# Pics\n<!-- {\"width\":160} -->\n"), "{}", small.text);let full = apply(text, Selection::cursor(30), &Action::PictureWidth { width: "full".into() }); assert!(full.text.contains("\n"), "{}", full.text);}#[test]fn find_matches_any_case_and_accents_with_utf16_positions() {let text = "Café, cafe and CAFÉ 😀 café";let found = find::find(text, "cafe"); assert_eq!(found.len(), 4);let last = found[3];let units: Vec<u16> = text.encode_utf16().collect(); assert_eq!(String::from_utf16(&units[last.start as usize..last.end as usize]).unwrap(), "café"); assert!(find::holds("Crème brûlée", "creme brulee"));}#[test]fn search_words_read_and_combine() {let terms = search::parse("garden -weeds \"next week\" #home/garden -#old @todo @nothing");let kinds: Vec<(search::TermKind, bool, &str)> = terms.iter().map(|t| (t.kind.clone(), t.not, t.value.as_str())).collect();use search::TermKind::*; assert_eq!(kinds, [(Word, false, "garden"), (Word, true, "weeds"), (Phrase, false, "next week"), (Tag, false, "home/garden"), (Tag, true, "old"), (Special, false, "todo"), (Word, false, "@nothing")]);let two = search::parse("#two words# milk"); assert_eq!((two[0].kind.clone(), two[0].value.as_str(), two[1].value.as_str()), (Tag, "two words", "milk"));let tags = vec!["home/garden/beds".to_string()];let facts = search::Facts { text: "The garden next week, no w-e-e-d-s, @nothing", tags: &tags, open_checks: 1, checks: 2, ..Default::default() }; assert!(search::matches(&terms, &facts));let done = search::Facts { open_checks: 0, ..facts.clone() }; assert!(!search::matches(&search::parse("@todo"), &done)); assert!(search::matches(&search::parse("@done -@untagged"), &done));}#[test]fn a_tag_wears_the_catalogues_icon_for_its_last_word_or_the_fallback() { assert_eq!(icons::for_tag("Work"), "tag_work"); assert_eq!(icons::for_tag("home/Garden"), "tag_garden"); assert_eq!(icons::for_tag("Rezepte"), "tag_food"); assert_eq!(icons::for_tag("voyages"), "tag_travel"); assert_eq!(icons::for_tag("यात्रा"), "tag_travel"); assert_eq!(icons::for_tag("zettelkasten"), "hash"); assert!(icons::known("tag_book") && icons::known("hash") && !icons::known("tag_nothing"));}#[test]fn a_notes_checkboxes_and_paragraphs_are_counted() {let d = derive::derive("# List\n- [ ] milk\n- [x] bread\n- [x] eggs\n\nSome words\n```\ncode\nmore\n```\n"); assert_eq!((d.checks, d.checks_done, d.paragraphs), (3, 2, 6));}#[test]fn a_tag_is_taken_off_wherever_it_is_written() {// Its line goes when it held only the tag; words around it keep their place; any case is the tag. assert_eq!(derive::untag("# Soup\nLeeks. #Recipes\n\n#recipes\n", "recipes"), "# Soup\nLeeks.\n"); assert_eq!(derive::untag("#work Plans for #work and #work/next", "#work"), "Plans for and #work/next");// In code it is no tag, and another tag stays. assert_eq!(derive::untag("`#home` and #garden", "home"), "`#home` and #garden"); assert_eq!(derive::untag("Words #two words# here", "two words"), "Words here"); assert_eq!(derive::untag("No tag here", "home"), "No tag here");}/// Notes brought in from HTML carry anchors (`href="#fn-6f1a2b3c"`): an attribute's `#` is no tag, so no tag named like/// an id is filed from them. A quoted or bracketed tag a person wrote still is one.#[test]fn an_html_anchor_is_no_tag() {let text = "Footnote<sup><a href=\"#fn-6f1a2b3c\" id='#fnref-6f1a2b3c'>1</a></sup> and <a href = \"#top\">top</a>\n\"#quoted\" (#bracketed) [#square] #plain"; assert_eq!(derive::tags(text), vec!["quoted", "bracketed", "square", "plain"]);}/// A colour code is no tag: 3, 4, 6 or 8 hex characters ending there, in either case; a word of hex letters alone is/// a colour without a lower-case letter and a tag with one. A tag begins with a letter: an issue number, a reference/// or a year is none (the owner's Words, 22 Nov 2026: every example from his drawer).#[test]fn a_colour_code_is_no_tag() {let text = "Palette #1B2638, #3A171C and #3a5f0bff; also #3a5f0b and C# code.\n#decade #abc #a1b #1B2638x #1B26381 #1b2638/dark #beefed #real"; assert_eq!(derive::tags(text), vec!["decade", "abc", "beefed", "real"]);// His drawer: none of these is a tag.let his = "Ground #1B1A18, paper #E9D9B7 and #F3EADA, ink #FFFFFF on #211B12.\nFixes #509, see #1132-era and #1310-polish.\n#FFF #fff #BEEF #CAFE #000 #12ab"; assert_eq!(derive::tags(his), Vec::<String>::new());// A word of hex letters with a lower-case letter is a word; a tag may begin upper case. assert_eq!(derive::tags("#facade #cafe #Bad #Facade #test #work/2024 #Q3-plans"), vec!["facade", "cafe", "Bad", "test", "work/2024", "Q3-plans"], "#Facade is #facade again");// A tag begins with a letter, never a digit, an underscore, a dash or a slash. assert_eq!(derive::tags("#2024 #1st #_x #-y #/z #x1"), vec!["x1"]);// Never in code, a code block or a link's address. assert_eq!(derive::tags("`#incode` and\n```\n#inblock\n```\n<https://example.com/#anchor> https://example.com/page#frag #out"), vec!["out"]);}