1- use lib_ot:: core:: { DocumentTree , NodeData , Position , TransactionBuilder } ;
1+ use lib_ot:: core:: { DocumentTree , NodeAttributes , NodeSubTree , Position , TransactionBuilder } ;
22use lib_ot:: errors:: OTErrorCode ;
33use 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]
6689fn 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