forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

testutil.rs113 lines, 3.2 KiBHistoryRawDownload
1//! Compact builders and a dump format for core tests.
2//! Dump: `p(ab) h2(x) ul(li(p(a)))`, `|` for a hard break, `{s:x}` strong, `{e:x}` em,
3//! `{c:x}` code, `{l=href:x}` link, `hr`.
4
5use loro::LoroDoc;
6use note_model::flat;
7use note_model::marks::Marks;
8use note_model::write::{Block, Inline};
9
10use super::model::{Blk, Model};
11use super::pos::{Pos, Selection};
12
13pub fn t(s: &str) -> Inline {
14 Inline::Text(s.into(), Marks::default())
15}
16pub fn strong(s: &str) -> Inline {
17 Inline::Text(s.into(), Marks { strong: true, ..Marks::default() })
18}
19pub fn br() -> Inline {
20 Inline::HardBreak
21}
22pub fn p(s: &str) -> Block {
23 Block::Paragraph(if s.is_empty() { Vec::new() } else { vec![t(s)] })
24}
25pub fn pi(v: Vec<Inline>) -> Block {
26 Block::Paragraph(v)
27}
28pub fn h(n: i64, s: &str) -> Block {
29 Block::Heading(n, vec![t(s)])
30}
31pub fn hr() -> Block {
32 Block::Rule
33}
34pub fn quote(v: Vec<Block>) -> Block {
35 Block::Quote(v)
36}
37pub fn ul(items: Vec<Vec<Block>>) -> Block {
38 Block::List { ordered: false, start: 1, items }
39}
40
41pub fn build(blocks: Vec<Block>) -> Model {
42 let doc = LoroDoc::new();
43 super::marks::configure(&doc);
44 let text = flat::body(&doc);
45 if blocks.is_empty() {
46 flat::init(&doc).unwrap();
47 } else {
48 flat::write(&text, 0, &flat::flatten(&blocks), false).unwrap();
49 }
50 doc.commit();
51 Model::new(doc)
52}
53
54pub fn dump(m: &Model) -> String {
55 m.check();
56 flat::doc_blocks(&m.doc).iter().map(block).collect::<Vec<_>>().join(" ")
57}
58
59fn block(b: &Block) -> String {
60 match b {
61 Block::Paragraph(v) => format!("p({})", inline(v)),
62 Block::Heading(n, v) => format!("h{n}({})", inline(v)),
63 Block::Code { text, .. } => format!("code({text})"),
64 Block::Rule => "hr".into(),
65 Block::Quote(v) => format!("quote({})", v.iter().map(block).collect::<Vec<_>>().join(" ")),
66 Block::List { ordered, items, .. } => format!(
67 "{}({})",
68 if *ordered { "ol" } else { "ul" },
69 items.iter().map(|i| format!("li({})", i.iter().map(block).collect::<Vec<_>>().join(" "))).collect::<Vec<_>>().join(" ")
70 ),
71 }
72}
73
74fn inline(v: &[Inline]) -> String {
75 let mut s = String::new();
76 for i in v {
77 match i {
78 Inline::Text(t, m) => {
79 let mut x = t.clone();
80 if m.code {
81 x = format!("{{c:{x}}}");
82 }
83 if m.strong {
84 x = format!("{{s:{x}}}");
85 }
86 if m.em {
87 x = format!("{{e:{x}}}");
88 }
89 if let Some((href, _)) = &m.link {
90 x = format!("{{l={href}:{x}}}");
91 }
92 s.push_str(&x);
93 }
94 Inline::HardBreak => s.push('|'),
95 Inline::Image { .. } => s.push_str("[img]"),
96 }
97 }
98 s
99}
100
101/// The n-th text block.
102pub fn tb(m: &Model, n: usize) -> Blk {
103 let all = m.blocks(0, m.len());
104 all.into_iter().filter(|b| b.kind().is_text()).nth(n).expect("text block")
105}
106
107pub fn at(m: &Model, n: usize, off: usize) -> Selection {
108 Selection::caret(tb(m, n).start + off)
109}
110
111pub fn pos(m: &Model, n: usize, off: usize) -> Pos {
112 tb(m, n).start + off
113}