12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- use lib_ot::{
- core::attributes::Attributes,
- core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
- text_delta::TextDelta,
- };
- pub enum NodeScript {
- InsertNode { path: Path, node: NodeData },
- UpdateAttributes { path: Path, attributes: Attributes },
- UpdateBody { path: Path, changeset: NodeBodyChangeset },
- DeleteNode { path: Path },
- AssertNumberOfNodesAtPath { path: Option<Path>, len: usize },
- AssertNode { path: Path, expected: Option<NodeData> },
- AssertNodeDelta { path: Path, expected: TextDelta },
- }
- pub struct NodeTest {
- node_tree: NodeTree,
- }
- impl NodeTest {
- pub fn new() -> Self {
- Self {
- node_tree: NodeTree::new("root"),
- }
- }
- 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();
- }
- NodeScript::UpdateAttributes { path, attributes } => {
- let transaction = TransactionBuilder::new(&self.node_tree)
- .update_attributes_at_path(&path, attributes)
- .finalize();
- self.node_tree.apply(transaction).unwrap();
- }
- NodeScript::UpdateBody { path, changeset } => {
- //
- let transaction = TransactionBuilder::new(&self.node_tree)
- .update_body_at_path(&path, changeset)
- .finalize();
- self.node_tree.apply(transaction).unwrap();
- }
- 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 } => {
- let node_id = self.node_tree.node_id_at_path(path);
- match node_id {
- None => assert!(node_id.is_none()),
- Some(node_id) => {
- let node_data = self.node_tree.get_node(node_id).cloned();
- assert_eq!(node_data, expected.map(|e| e.into()));
- }
- }
- }
- NodeScript::AssertNumberOfNodesAtPath {
- path,
- len: expected_len,
- } => match path {
- None => {
- let len = self.node_tree.number_of_children(None);
- assert_eq!(len, expected_len)
- }
- Some(path) => {
- let node_id = self.node_tree.node_id_at_path(path).unwrap();
- let len = self.node_tree.number_of_children(Some(node_id));
- assert_eq!(len, expected_len)
- }
- },
- 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");
- }
- }
- }
- }
- }
|