mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: test insert sub trees
This commit is contained in:
parent
d6ef13adae
commit
096544d6a3
@ -5,7 +5,10 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta};
|
|||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum DocumentOperation {
|
pub enum DocumentOperation {
|
||||||
#[serde(rename = "insert-operation")]
|
#[serde(rename = "insert-operation")]
|
||||||
Insert { path: Position, nodes: Vec<Box<NodeSubTree>> },
|
Insert {
|
||||||
|
path: Position,
|
||||||
|
nodes: Vec<Box<NodeSubTree>>,
|
||||||
|
},
|
||||||
#[serde(rename = "update-operation")]
|
#[serde(rename = "update-operation")]
|
||||||
Update {
|
Update {
|
||||||
path: Position,
|
path: Position,
|
||||||
@ -14,7 +17,10 @@ pub enum DocumentOperation {
|
|||||||
old_attributes: NodeAttributes,
|
old_attributes: NodeAttributes,
|
||||||
},
|
},
|
||||||
#[serde(rename = "delete-operation")]
|
#[serde(rename = "delete-operation")]
|
||||||
Delete { path: Position, nodes: Vec<Box<NodeSubTree>> },
|
Delete {
|
||||||
|
path: Position,
|
||||||
|
nodes: Vec<Box<NodeSubTree>>,
|
||||||
|
},
|
||||||
#[serde(rename = "text-edit-operation")]
|
#[serde(rename = "text-edit-operation")]
|
||||||
TextEdit {
|
TextEdit {
|
||||||
path: Position,
|
path: Position,
|
||||||
@ -160,7 +166,25 @@ mod tests {
|
|||||||
let result = serde_json::to_string(&insert).unwrap();
|
let result = serde_json::to_string(&insert).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
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":{}}]}]}"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ impl NodeData {
|
|||||||
|
|
||||||
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct NodeSubTree {
|
pub struct NodeSubTree {
|
||||||
|
#[serde(rename = "type")]
|
||||||
pub node_type: String,
|
pub node_type: String,
|
||||||
pub attributes: NodeAttributes,
|
pub attributes: NodeAttributes,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::core::document::position::Position;
|
use crate::core::document::position::Position;
|
||||||
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
|
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
|
||||||
use std::collections::HashMap;
|
|
||||||
use indextree::NodeId;
|
use indextree::NodeId;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub struct Transaction {
|
pub struct Transaction {
|
||||||
pub operations: Vec<DocumentOperation>,
|
pub operations: Vec<DocumentOperation>,
|
||||||
@ -86,7 +86,7 @@ impl<'a> TransactionBuilder<'a> {
|
|||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
Box::new(NodeSubTree {
|
Box::new(NodeSubTree {
|
||||||
node_type: node_data.node_type.clone(),
|
node_type: node_data.node_type.clone(),
|
||||||
|
@ -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 lib_ot::errors::OTErrorCode;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ fn test_documents() {
|
|||||||
let mut document = DocumentTree::new();
|
let mut document = DocumentTree::new();
|
||||||
let transaction = {
|
let transaction = {
|
||||||
let mut tb = TransactionBuilder::new(&document);
|
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()
|
tb.finalize()
|
||||||
};
|
};
|
||||||
document.apply(transaction).unwrap();
|
document.apply(transaction).unwrap();
|
||||||
@ -47,29 +47,52 @@ fn test_inserts_nodes() {
|
|||||||
let mut document = DocumentTree::new();
|
let mut document = DocumentTree::new();
|
||||||
let transaction = {
|
let transaction = {
|
||||||
let mut tb = TransactionBuilder::new(&document);
|
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.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.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
|
tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
|
||||||
tb.finalize()
|
tb.finalize()
|
||||||
};
|
};
|
||||||
document.apply(transaction).unwrap();
|
document.apply(transaction).unwrap();
|
||||||
|
|
||||||
let transaction = {
|
let transaction = {
|
||||||
let mut tb = TransactionBuilder::new(&document);
|
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()
|
tb.finalize()
|
||||||
};
|
};
|
||||||
document.apply(transaction).unwrap();
|
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]
|
#[test]
|
||||||
fn test_update_nodes() {
|
fn test_update_nodes() {
|
||||||
let mut document = DocumentTree::new();
|
let mut document = DocumentTree::new();
|
||||||
let transaction = {
|
let transaction = {
|
||||||
let mut tb = TransactionBuilder::new(&document);
|
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.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.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
|
tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
|
||||||
tb.finalize()
|
tb.finalize()
|
||||||
};
|
};
|
||||||
document.apply(transaction).unwrap();
|
document.apply(transaction).unwrap();
|
||||||
@ -92,9 +115,9 @@ fn test_delete_nodes() {
|
|||||||
let mut document = DocumentTree::new();
|
let mut document = DocumentTree::new();
|
||||||
let transaction = {
|
let transaction = {
|
||||||
let mut tb = TransactionBuilder::new(&document);
|
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.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.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
|
tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
|
||||||
tb.finalize()
|
tb.finalize()
|
||||||
};
|
};
|
||||||
document.apply(transaction).unwrap();
|
document.apply(transaction).unwrap();
|
||||||
@ -115,8 +138,8 @@ fn test_errors() {
|
|||||||
let mut document = DocumentTree::new();
|
let mut document = DocumentTree::new();
|
||||||
let transaction = {
|
let transaction = {
|
||||||
let mut tb = TransactionBuilder::new(&document);
|
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.insert_nodes_at_path(&vec![100].into(), &vec![NodeData::new("text")]);
|
tb.insert_nodes_at_path(&vec![100].into(), &vec![Box::new(NodeSubTree::new("text"))]);
|
||||||
tb.finalize()
|
tb.finalize()
|
||||||
};
|
};
|
||||||
let result = document.apply(transaction);
|
let result = document.apply(transaction);
|
||||||
|
Loading…
Reference in New Issue
Block a user