Model pages and blocks as document nodes
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
use documents::{
|
||||
Contextualize, Document, DocumentContent, DocumentId, DocumentKey, DocumentTree, TreeError,
|
||||
WorkspaceId,
|
||||
};
|
||||
|
||||
fn node(id: &str, parent: Option<&str>, position: u64, content: DocumentContent) -> Document {
|
||||
Document {
|
||||
key: DocumentKey {
|
||||
workspace: WorkspaceId::new("workspace").unwrap(),
|
||||
document: DocumentId::new(id).unwrap(),
|
||||
},
|
||||
parent_id: parent.map(|id| DocumentId::new(id).unwrap()),
|
||||
position,
|
||||
content,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_pages_and_blocks_share_an_ordered_tree() {
|
||||
let root = node(
|
||||
"root",
|
||||
None,
|
||||
0,
|
||||
DocumentContent::Page {
|
||||
title: "Root".into(),
|
||||
},
|
||||
);
|
||||
let page = node(
|
||||
"page",
|
||||
Some("root"),
|
||||
2,
|
||||
DocumentContent::Page {
|
||||
title: "Nested".into(),
|
||||
},
|
||||
);
|
||||
let paragraph = node(
|
||||
"paragraph",
|
||||
Some("page"),
|
||||
0,
|
||||
DocumentContent::Paragraph("Details".into()),
|
||||
);
|
||||
let heading = node(
|
||||
"heading",
|
||||
Some("root"),
|
||||
1,
|
||||
DocumentContent::Heading("Overview".into()),
|
||||
);
|
||||
let tree = DocumentTree::new(root, [paragraph, page, heading]).unwrap();
|
||||
assert_eq!(
|
||||
tree.nodes()
|
||||
.map(|(depth, node)| (depth, node.key.document.as_str()))
|
||||
.collect::<Vec<_>>(),
|
||||
[(0, "root"), (1, "heading"), (1, "page"), (2, "paragraph")],
|
||||
);
|
||||
assert_eq!(
|
||||
tree.render_context(),
|
||||
"# Root\n## Overview\n# Nested\n Details"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_cycle_is_rejected() {
|
||||
let root = node("root", None, 0, DocumentContent::Paragraph("root".into()));
|
||||
let a = node("a", Some("b"), 0, DocumentContent::Paragraph("a".into()));
|
||||
let b = node("b", Some("a"), 0, DocumentContent::Paragraph("b".into()));
|
||||
assert!(matches!(
|
||||
DocumentTree::new(root, [a, b]),
|
||||
Err(TreeError::InvalidParent)
|
||||
));
|
||||
}
|
||||
Reference in New Issue
Block a user