From 096544d6a314e08e10606935d8d260d1e6fc03ba Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Tue, 23 Aug 2022 19:39:00 +0800 Subject: [PATCH] feat: test insert sub trees --- .../src/core/document/document_operation.rs | 30 +++++++++-- shared-lib/lib-ot/src/core/document/node.rs | 1 + .../lib-ot/src/core/document/transaction.rs | 4 +- shared-lib/lib-ot/tests/main.rs | 51 ++++++++++++++----- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/shared-lib/lib-ot/src/core/document/document_operation.rs b/shared-lib/lib-ot/src/core/document/document_operation.rs index 77c74127bd..caca29e110 100644 --- a/shared-lib/lib-ot/src/core/document/document_operation.rs +++ b/shared-lib/lib-ot/src/core/document/document_operation.rs @@ -5,7 +5,10 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta}; #[serde(tag = "type")] pub enum DocumentOperation { #[serde(rename = "insert-operation")] - Insert { path: Position, nodes: Vec> }, + Insert { + path: Position, + nodes: Vec>, + }, #[serde(rename = "update-operation")] Update { path: Position, @@ -14,7 +17,10 @@ pub enum DocumentOperation { old_attributes: NodeAttributes, }, #[serde(rename = "delete-operation")] - Delete { path: Position, nodes: Vec> }, + Delete { + path: Position, + nodes: Vec>, + }, #[serde(rename = "text-edit-operation")] TextEdit { path: Position, @@ -160,7 +166,25 @@ mod tests { let result = serde_json::to_string(&insert).unwrap(); assert_eq!( result, - r#"{"type":"insert-operation","path":[0,1],"nodes":[{"node_type":"text","attributes":{}}]}"# + r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{}}]}"# + ); + } + + #[test] + fn test_serialize_insert_sub_trees() { + let insert = DocumentOperation::Insert { + path: Position(vec![0, 1]), + nodes: vec![Box::new(NodeSubTree { + node_type: "text".into(), + attributes: NodeAttributes::new(), + delta: None, + children: vec![Box::new(NodeSubTree::new("text".into()))], + })], + }; + let result = serde_json::to_string(&insert).unwrap(); + assert_eq!( + result, + r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{},"children":[{"type":"text","attributes":{}}]}]}"# ); } diff --git a/shared-lib/lib-ot/src/core/document/node.rs b/shared-lib/lib-ot/src/core/document/node.rs index 1e298bc72e..e74c7d4918 100644 --- a/shared-lib/lib-ot/src/core/document/node.rs +++ b/shared-lib/lib-ot/src/core/document/node.rs @@ -19,6 +19,7 @@ impl NodeData { #[derive(Clone, serde::Serialize, serde::Deserialize)] pub struct NodeSubTree { + #[serde(rename = "type")] pub node_type: String, pub attributes: NodeAttributes, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/shared-lib/lib-ot/src/core/document/transaction.rs b/shared-lib/lib-ot/src/core/document/transaction.rs index e22ba21c3a..73fce7d8ad 100644 --- a/shared-lib/lib-ot/src/core/document/transaction.rs +++ b/shared-lib/lib-ot/src/core/document/transaction.rs @@ -1,7 +1,7 @@ use crate::core::document::position::Position; use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree}; -use std::collections::HashMap; use indextree::NodeId; +use std::collections::HashMap; pub struct Transaction { pub operations: Vec, @@ -86,7 +86,7 @@ impl<'a> TransactionBuilder<'a> { } else { break; } - }; + } Box::new(NodeSubTree { node_type: node_data.node_type.clone(), diff --git a/shared-lib/lib-ot/tests/main.rs b/shared-lib/lib-ot/tests/main.rs index 7daf74e957..31e7748b3a 100644 --- a/shared-lib/lib-ot/tests/main.rs +++ b/shared-lib/lib-ot/tests/main.rs @@ -1,4 +1,4 @@ -use lib_ot::core::{DocumentTree, NodeData, Position, TransactionBuilder}; +use lib_ot::core::{DocumentTree, NodeAttributes, NodeSubTree, Position, TransactionBuilder}; use lib_ot::errors::OTErrorCode; use std::collections::HashMap; @@ -13,7 +13,7 @@ fn test_documents() { let mut document = DocumentTree::new(); let transaction = { let mut tb = TransactionBuilder::new(&document); - tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]); + tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]); tb.finalize() }; document.apply(transaction).unwrap(); @@ -47,29 +47,52 @@ fn test_inserts_nodes() { let mut document = DocumentTree::new(); let transaction = { let mut tb = TransactionBuilder::new(&document); - tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]); + tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]); tb.finalize() }; document.apply(transaction).unwrap(); let transaction = { let mut tb = TransactionBuilder::new(&document); - tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]); + tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]); tb.finalize() }; document.apply(transaction).unwrap(); } +#[test] +fn test_inserts_subtrees() { + let mut document = DocumentTree::new(); + let transaction = { + let mut tb = TransactionBuilder::new(&document); + tb.insert_nodes_at_path( + &vec![0].into(), + &vec![Box::new(NodeSubTree { + node_type: "text".into(), + attributes: NodeAttributes::new(), + delta: None, + children: vec![Box::new(NodeSubTree::new("image".into()))], + })], + ); + tb.finalize() + }; + document.apply(transaction).unwrap(); + + let node = document.node_at_path(&Position(vec![0, 0])).unwrap(); + let data = document.arena.get(node).unwrap().get(); + assert_eq!(data.node_type, "image"); +} + #[test] fn test_update_nodes() { let mut document = DocumentTree::new(); let transaction = { let mut tb = TransactionBuilder::new(&document); - tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]); + tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]); tb.finalize() }; document.apply(transaction).unwrap(); @@ -92,9 +115,9 @@ fn test_delete_nodes() { let mut document = DocumentTree::new(); let transaction = { let mut tb = TransactionBuilder::new(&document); - tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]); + tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]); tb.finalize() }; document.apply(transaction).unwrap(); @@ -115,8 +138,8 @@ fn test_errors() { let mut document = DocumentTree::new(); let transaction = { let mut tb = TransactionBuilder::new(&document); - tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]); - tb.insert_nodes_at_path(&vec![100].into(), &vec![NodeData::new("text")]); + tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]); + tb.insert_nodes_at_path(&vec![100].into(), &vec![Box::new(NodeSubTree::new("text"))]); tb.finalize() }; let result = document.apply(transaction);