2022-09-11 04:59:01 +00:00
|
|
|
use lib_ot::core::{
|
|
|
|
NodeAttributes, NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TextDelta, TransactionBuilder,
|
|
|
|
};
|
2022-09-09 07:05:41 +00:00
|
|
|
|
|
|
|
pub enum NodeScript {
|
2022-09-11 00:59:12 +00:00
|
|
|
InsertNode { path: Path, node: NodeData },
|
2022-09-11 04:59:01 +00:00
|
|
|
UpdateAttributes { path: Path, attributes: NodeAttributes },
|
|
|
|
UpdateBody { path: Path, changeset: NodeBodyChangeset },
|
2022-09-09 07:05:41 +00:00
|
|
|
DeleteNode { path: Path },
|
|
|
|
AssertNumberOfChildrenAtPath { path: Option<Path>, len: usize },
|
2022-09-11 00:59:12 +00:00
|
|
|
AssertNode { path: Path, expected: Option<NodeData> },
|
2022-09-11 04:59:01 +00:00
|
|
|
AssertNodeDelta { path: Path, expected: TextDelta },
|
2022-09-09 07:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NodeTest {
|
2022-09-10 00:58:08 +00:00
|
|
|
node_tree: NodeTree,
|
2022-09-09 07:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeTest {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2022-09-10 00:58:08 +00:00
|
|
|
node_tree: NodeTree::new(),
|
2022-09-09 07:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
|
|
|
|
for script in scripts {
|
|
|
|
self.run_script(script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_script(&mut self, script: NodeScript) {
|
|
|
|
match script {
|
|
|
|
NodeScript::InsertNode { path, node } => {
|
|
|
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
|
|
|
.insert_node_at_path(path, node)
|
|
|
|
.finalize();
|
|
|
|
|
|
|
|
self.node_tree.apply(transaction).unwrap();
|
|
|
|
}
|
2022-09-11 04:59:01 +00:00
|
|
|
NodeScript::UpdateAttributes { path, attributes } => {
|
2022-09-09 07:05:41 +00:00
|
|
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
2022-09-10 00:42:53 +00:00
|
|
|
.update_attributes_at_path(&path, attributes)
|
2022-09-09 07:05:41 +00:00
|
|
|
.finalize();
|
|
|
|
self.node_tree.apply(transaction).unwrap();
|
|
|
|
}
|
2022-09-11 04:59:01 +00:00
|
|
|
NodeScript::UpdateBody { path, changeset } => {
|
|
|
|
//
|
|
|
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
|
|
|
.update_body_at_path(&path, changeset)
|
|
|
|
.finalize();
|
|
|
|
self.node_tree.apply(transaction).unwrap();
|
|
|
|
}
|
2022-09-09 07:05:41 +00:00
|
|
|
NodeScript::DeleteNode { path } => {
|
|
|
|
let transaction = TransactionBuilder::new(&self.node_tree)
|
|
|
|
.delete_node_at_path(&path)
|
|
|
|
.finalize();
|
|
|
|
self.node_tree.apply(transaction).unwrap();
|
|
|
|
}
|
|
|
|
NodeScript::AssertNode { path, expected } => {
|
2022-09-11 04:59:01 +00:00
|
|
|
let node_id = self.node_tree.node_id_at_path(path);
|
2022-09-09 07:05:41 +00:00
|
|
|
|
|
|
|
match node_id {
|
|
|
|
None => assert!(node_id.is_none()),
|
|
|
|
Some(node_id) => {
|
2022-09-11 00:59:12 +00:00
|
|
|
let node_data = self.node_tree.get_node(node_id).cloned();
|
2022-09-10 00:58:08 +00:00
|
|
|
assert_eq!(node_data, expected.and_then(|e| Some(e.into())));
|
2022-09-09 07:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NodeScript::AssertNumberOfChildrenAtPath {
|
|
|
|
path,
|
|
|
|
len: expected_len,
|
|
|
|
} => match path {
|
|
|
|
None => {
|
|
|
|
let len = self.node_tree.number_of_children(None);
|
|
|
|
assert_eq!(len, expected_len)
|
|
|
|
}
|
|
|
|
Some(path) => {
|
2022-09-11 04:59:01 +00:00
|
|
|
let node_id = self.node_tree.node_id_at_path(path).unwrap();
|
2022-09-09 07:05:41 +00:00
|
|
|
let len = self.node_tree.number_of_children(Some(node_id));
|
|
|
|
assert_eq!(len, expected_len)
|
|
|
|
}
|
|
|
|
},
|
2022-09-11 04:59:01 +00:00
|
|
|
NodeScript::AssertNodeDelta { path, expected } => {
|
|
|
|
let node = self.node_tree.get_node_at_path(&path).unwrap();
|
|
|
|
if let NodeBody::Delta(delta) = node.body.clone() {
|
|
|
|
debug_assert_eq!(delta, expected);
|
|
|
|
} else {
|
|
|
|
panic!("Node body type not match, expect Delta");
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 07:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|