Skip to content

Commit 096544d

Browse files
committed
feat: test insert sub trees
1 parent d6ef13a commit 096544d

File tree

4 files changed

+67
-19
lines changed

4 files changed

+67
-19
lines changed

shared-lib/lib-ot/src/core/document/document_operation.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta};
55
#[serde(tag = "type")]
66
pub enum DocumentOperation {
77
#[serde(rename = "insert-operation")]
8-
Insert { path: Position, nodes: Vec<Box<NodeSubTree>> },
8+
Insert {
9+
path: Position,
10+
nodes: Vec<Box<NodeSubTree>>,
11+
},
912
#[serde(rename = "update-operation")]
1013
Update {
1114
path: Position,
@@ -14,7 +17,10 @@ pub enum DocumentOperation {
1417
old_attributes: NodeAttributes,
1518
},
1619
#[serde(rename = "delete-operation")]
17-
Delete { path: Position, nodes: Vec<Box<NodeSubTree>> },
20+
Delete {
21+
path: Position,
22+
nodes: Vec<Box<NodeSubTree>>,
23+
},
1824
#[serde(rename = "text-edit-operation")]
1925
TextEdit {
2026
path: Position,
@@ -160,7 +166,25 @@ mod tests {
160166
let result = serde_json::to_string(&insert).unwrap();
161167
assert_eq!(
162168
result,
163-
r#"{"type":"insert-operation","path":[0,1],"nodes":[{"node_type":"text","attributes":{}}]}"#
169+
r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{}}]}"#
170+
);
171+
}
172+
173+
#[test]
174+
fn test_serialize_insert_sub_trees() {
175+
let insert = DocumentOperation::Insert {
176+
path: Position(vec![0, 1]),
177+
nodes: vec![Box::new(NodeSubTree {
178+
node_type: "text".into(),
179+
attributes: NodeAttributes::new(),
180+
delta: None,
181+
children: vec![Box::new(NodeSubTree::new("text".into()))],
182+
})],
183+
};
184+
let result = serde_json::to_string(&insert).unwrap();
185+
assert_eq!(
186+
result,
187+
r#"{"type":"insert-operation","path":[0,1],"nodes":[{"type":"text","attributes":{},"children":[{"type":"text","attributes":{}}]}]}"#
164188
);
165189
}
166190

shared-lib/lib-ot/src/core/document/node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ impl NodeData {
1919

2020
#[derive(Clone, serde::Serialize, serde::Deserialize)]
2121
pub struct NodeSubTree {
22+
#[serde(rename = "type")]
2223
pub node_type: String,
2324
pub attributes: NodeAttributes,
2425
#[serde(skip_serializing_if = "Option::is_none")]

shared-lib/lib-ot/src/core/document/transaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::core::document::position::Position;
22
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
3-
use std::collections::HashMap;
43
use indextree::NodeId;
4+
use std::collections::HashMap;
55

66
pub struct Transaction {
77
pub operations: Vec<DocumentOperation>,
@@ -86,7 +86,7 @@ impl<'a> TransactionBuilder<'a> {
8686
} else {
8787
break;
8888
}
89-
};
89+
}
9090

9191
Box::new(NodeSubTree {
9292
node_type: node_data.node_type.clone(),

shared-lib/lib-ot/tests/main.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lib_ot::core::{DocumentTree, NodeData, Position, TransactionBuilder};
1+
use lib_ot::core::{DocumentTree, NodeAttributes, NodeSubTree, Position, TransactionBuilder};
22
use lib_ot::errors::OTErrorCode;
33
use std::collections::HashMap;
44

@@ -13,7 +13,7 @@ fn test_documents() {
1313
let mut document = DocumentTree::new();
1414
let transaction = {
1515
let mut tb = TransactionBuilder::new(&document);
16-
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
16+
tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
1717
tb.finalize()
1818
};
1919
document.apply(transaction).unwrap();
@@ -47,29 +47,52 @@ fn test_inserts_nodes() {
4747
let mut document = DocumentTree::new();
4848
let transaction = {
4949
let mut tb = TransactionBuilder::new(&document);
50-
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
51-
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
52-
tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
50+
tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
51+
tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
52+
tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
5353
tb.finalize()
5454
};
5555
document.apply(transaction).unwrap();
5656

5757
let transaction = {
5858
let mut tb = TransactionBuilder::new(&document);
59-
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
59+
tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
6060
tb.finalize()
6161
};
6262
document.apply(transaction).unwrap();
6363
}
6464

65+
#[test]
66+
fn test_inserts_subtrees() {
67+
let mut document = DocumentTree::new();
68+
let transaction = {
69+
let mut tb = TransactionBuilder::new(&document);
70+
tb.insert_nodes_at_path(
71+
&vec![0].into(),
72+
&vec![Box::new(NodeSubTree {
73+
node_type: "text".into(),
74+
attributes: NodeAttributes::new(),
75+
delta: None,
76+
children: vec![Box::new(NodeSubTree::new("image".into()))],
77+
})],
78+
);
79+
tb.finalize()
80+
};
81+
document.apply(transaction).unwrap();
82+
83+
let node = document.node_at_path(&Position(vec![0, 0])).unwrap();
84+
let data = document.arena.get(node).unwrap().get();
85+
assert_eq!(data.node_type, "image");
86+
}
87+
6588
#[test]
6689
fn test_update_nodes() {
6790
let mut document = DocumentTree::new();
6891
let transaction = {
6992
let mut tb = TransactionBuilder::new(&document);
70-
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
71-
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
72-
tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
93+
tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
94+
tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
95+
tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
7396
tb.finalize()
7497
};
7598
document.apply(transaction).unwrap();
@@ -92,9 +115,9 @@ fn test_delete_nodes() {
92115
let mut document = DocumentTree::new();
93116
let transaction = {
94117
let mut tb = TransactionBuilder::new(&document);
95-
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
96-
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
97-
tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
118+
tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
119+
tb.insert_nodes_at_path(&vec![1].into(), &vec![Box::new(NodeSubTree::new("text"))]);
120+
tb.insert_nodes_at_path(&vec![2].into(), &vec![Box::new(NodeSubTree::new("text"))]);
98121
tb.finalize()
99122
};
100123
document.apply(transaction).unwrap();
@@ -115,8 +138,8 @@ fn test_errors() {
115138
let mut document = DocumentTree::new();
116139
let transaction = {
117140
let mut tb = TransactionBuilder::new(&document);
118-
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
119-
tb.insert_nodes_at_path(&vec![100].into(), &vec![NodeData::new("text")]);
141+
tb.insert_nodes_at_path(&vec![0].into(), &vec![Box::new(NodeSubTree::new("text"))]);
142+
tb.insert_nodes_at_path(&vec![100].into(), &vec![Box::new(NodeSubTree::new("text"))]);
120143
tb.finalize()
121144
};
122145
let result = document.apply(transaction);

0 commit comments

Comments
 (0)