operation_insert_test.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. use crate::node::script::NodeScript::*;
  2. use crate::node::script::NodeTest;
  3. use lib_ot::core::{placeholder_node, NodeData, NodeDataBuilder, NodeOperation, Path};
  4. #[test]
  5. fn operation_insert_op_transform_test() {
  6. let node_1 = NodeDataBuilder::new("text_1").build();
  7. let node_2 = NodeDataBuilder::new("text_2").build();
  8. let op_1 = NodeOperation::Insert {
  9. path: Path(vec![0, 1]),
  10. nodes: vec![node_1],
  11. };
  12. let mut insert_2 = NodeOperation::Insert {
  13. path: Path(vec![0, 1]),
  14. nodes: vec![node_2],
  15. };
  16. // let mut node_tree = NodeTree::new("root");
  17. // node_tree.apply_op(insert_1.clone()).unwrap();
  18. op_1.transform(&mut insert_2);
  19. let json = serde_json::to_string(&insert_2).unwrap();
  20. assert_eq!(json, r#"{"op":"insert","path":[0,2],"nodes":[{"type":"text_2"}]}"#);
  21. }
  22. #[test]
  23. fn operation_insert_one_level_path_test() {
  24. let node_data_1 = NodeDataBuilder::new("text_1").build();
  25. let node_data_2 = NodeDataBuilder::new("text_2").build();
  26. let node_data_3 = NodeDataBuilder::new("text_3").build();
  27. let node_3 = node_data_3.clone();
  28. // 0: text_1
  29. // 1: text_2
  30. //
  31. // Insert a new operation with rev_id 2 to index 1,but the index was already taken, so
  32. // it needs to be transformed.
  33. //
  34. // 0: text_1
  35. // 1: text_2
  36. // 2: text_3
  37. let scripts = vec![
  38. InsertNode {
  39. path: 0.into(),
  40. node_data: node_data_1.clone(),
  41. rev_id: 1,
  42. },
  43. InsertNode {
  44. path: 1.into(),
  45. node_data: node_data_2.clone(),
  46. rev_id: 2,
  47. },
  48. InsertNode {
  49. path: 1.into(),
  50. node_data: node_data_3.clone(),
  51. rev_id: 2,
  52. },
  53. AssertNode {
  54. path: 2.into(),
  55. expected: Some(node_3.clone()),
  56. },
  57. ];
  58. NodeTest::new().run_scripts(scripts);
  59. // If the rev_id of the node_data_3 is 3. then the tree will be:
  60. // 0: text_1
  61. // 1: text_3
  62. // 2: text_2
  63. let scripts = vec![
  64. InsertNode {
  65. path: 0.into(),
  66. node_data: node_data_1,
  67. rev_id: 1,
  68. },
  69. InsertNode {
  70. path: 1.into(),
  71. node_data: node_data_2,
  72. rev_id: 2,
  73. },
  74. InsertNode {
  75. path: 1.into(),
  76. node_data: node_data_3,
  77. rev_id: 3,
  78. },
  79. AssertNode {
  80. path: 1.into(),
  81. expected: Some(node_3),
  82. },
  83. ];
  84. NodeTest::new().run_scripts(scripts);
  85. }
  86. #[test]
  87. fn operation_insert_with_multiple_level_path_test() {
  88. let mut test = NodeTest::new();
  89. let node_data_1 = NodeDataBuilder::new("text_1")
  90. .add_node_data(NodeDataBuilder::new("text_1_1").build())
  91. .add_node_data(NodeDataBuilder::new("text_1_2").build())
  92. .build();
  93. let node_data_2 = NodeDataBuilder::new("text_2")
  94. .add_node_data(NodeDataBuilder::new("text_2_1").build())
  95. .add_node_data(NodeDataBuilder::new("text_2_2").build())
  96. .build();
  97. let node_data_3 = NodeDataBuilder::new("text_3").build();
  98. let scripts = vec![
  99. InsertNode {
  100. path: 0.into(),
  101. node_data: node_data_1,
  102. rev_id: 1,
  103. },
  104. InsertNode {
  105. path: 1.into(),
  106. node_data: node_data_2,
  107. rev_id: 2,
  108. },
  109. InsertNode {
  110. path: 1.into(),
  111. node_data: node_data_3.clone(),
  112. rev_id: 2,
  113. },
  114. AssertNode {
  115. path: 2.into(),
  116. expected: Some(node_data_3),
  117. },
  118. ];
  119. test.run_scripts(scripts);
  120. }
  121. #[test]
  122. fn operation_insert_node_out_of_bound_test() {
  123. let mut test = NodeTest::new();
  124. let image_a = NodeData::new("image_a");
  125. let image_b = NodeData::new("image_b");
  126. let image = NodeDataBuilder::new("image_1")
  127. .add_node_data(image_a)
  128. .add_node_data(image_b)
  129. .build();
  130. let text_node = NodeDataBuilder::new("text_1").add_node_data(image).build();
  131. let image_c = NodeData::new("image_c");
  132. let scripts = vec![
  133. InsertNode {
  134. path: 0.into(),
  135. node_data: text_node,
  136. rev_id: 1,
  137. },
  138. // 0:text_1
  139. // 0:image_1
  140. // 0:image_a
  141. // 1:image_b
  142. InsertNode {
  143. path: vec![0, 0, 3].into(),
  144. node_data: image_c.clone(),
  145. rev_id: 2,
  146. },
  147. // 0:text_1
  148. // 0:image_1
  149. // 0:image_a
  150. // 1:image_b
  151. // 2:placeholder node
  152. // 3:image_c
  153. AssertNode {
  154. path: vec![0, 0, 2].into(),
  155. expected: Some(placeholder_node()),
  156. },
  157. AssertNode {
  158. path: vec![0, 0, 3].into(),
  159. expected: Some(image_c),
  160. },
  161. AssertNode {
  162. path: vec![0, 0, 10].into(),
  163. expected: None,
  164. },
  165. ];
  166. test.run_scripts(scripts);
  167. }
  168. #[test]
  169. fn operation_insert_node_when_parent_is_not_exist_test1() {
  170. let mut test = NodeTest::new();
  171. let text_1 = NodeDataBuilder::new("text_1").build();
  172. let text_2 = NodeDataBuilder::new("text_2").build();
  173. let scripts = vec![
  174. InsertNode {
  175. path: 0.into(),
  176. node_data: text_1,
  177. rev_id: 1,
  178. },
  179. // The node at path 1 is not existing when inserting the text_2 to path 2.
  180. InsertNode {
  181. path: 2.into(),
  182. node_data: text_2.clone(),
  183. rev_id: 2,
  184. },
  185. AssertNode {
  186. path: 1.into(),
  187. expected: Some(placeholder_node()),
  188. },
  189. AssertNode {
  190. path: 2.into(),
  191. expected: Some(text_2),
  192. },
  193. ];
  194. test.run_scripts(scripts);
  195. }
  196. #[test]
  197. fn operation_insert_node_when_parent_is_not_exist_test2() {
  198. let mut test = NodeTest::new();
  199. let text_1 = NodeDataBuilder::new("text_1").build();
  200. let text_2 = NodeDataBuilder::new("text_2").build();
  201. let scripts = vec![
  202. InsertNode {
  203. path: 0.into(),
  204. node_data: text_1,
  205. rev_id: 1,
  206. },
  207. // The node at path 1 is not existing when inserting the text_2 to path 2.
  208. InsertNode {
  209. path: 3.into(),
  210. node_data: text_2.clone(),
  211. rev_id: 2,
  212. },
  213. AssertNode {
  214. path: 1.into(),
  215. expected: Some(placeholder_node()),
  216. },
  217. AssertNode {
  218. path: 2.into(),
  219. expected: Some(placeholder_node()),
  220. },
  221. AssertNode {
  222. path: 3.into(),
  223. expected: Some(text_2),
  224. },
  225. ];
  226. test.run_scripts(scripts);
  227. }
  228. #[test]
  229. fn operation_insert_node_when_its_parent_is_not_exist_test3() {
  230. let mut test = NodeTest::new();
  231. let text_1 = NodeDataBuilder::new("text_1").build();
  232. let text_2 = NodeDataBuilder::new("text_2").build();
  233. let mut placeholder_node = placeholder_node();
  234. placeholder_node.children.push(text_2.clone());
  235. let scripts = vec![
  236. InsertNode {
  237. path: 0.into(),
  238. node_data: text_1,
  239. rev_id: 1,
  240. },
  241. // The node at path 1 is not existing when inserting the text_2 to path 2.
  242. InsertNode {
  243. path: vec![1, 0].into(),
  244. node_data: text_2.clone(),
  245. rev_id: 2,
  246. },
  247. AssertNode {
  248. path: 1.into(),
  249. expected: Some(placeholder_node),
  250. },
  251. AssertNode {
  252. path: vec![1, 0].into(),
  253. expected: Some(text_2),
  254. },
  255. ];
  256. test.run_scripts(scripts);
  257. }
  258. #[test]
  259. fn operation_insert_node_to_the_end_when_parent_is_not_exist_test() {
  260. let mut test = NodeTest::new();
  261. let node_0 = NodeData::new("0");
  262. let node_1 = NodeData::new("1");
  263. let node_1_1 = NodeData::new("1_1");
  264. let text_node = NodeData::new("text");
  265. let mut ghost = placeholder_node();
  266. ghost.children.push(text_node.clone());
  267. // 0:0
  268. // 1:1
  269. // 0:1_1
  270. // 1:ghost
  271. // 0:text
  272. let scripts = vec![
  273. InsertNode {
  274. path: 0.into(),
  275. node_data: node_0,
  276. rev_id: 1,
  277. },
  278. InsertNode {
  279. path: 1.into(),
  280. node_data: node_1,
  281. rev_id: 2,
  282. },
  283. InsertNode {
  284. path: vec![1, 0].into(),
  285. node_data: node_1_1.clone(),
  286. rev_id: 3,
  287. },
  288. InsertNode {
  289. path: vec![1, 1, 0].into(),
  290. node_data: text_node.clone(),
  291. rev_id: 4,
  292. },
  293. AssertNode {
  294. path: vec![1, 0].into(),
  295. expected: Some(node_1_1),
  296. },
  297. AssertNode {
  298. path: vec![1, 1].into(),
  299. expected: Some(ghost),
  300. },
  301. AssertNode {
  302. path: vec![1, 1, 0].into(),
  303. expected: Some(text_node),
  304. },
  305. ];
  306. test.run_scripts(scripts);
  307. }
  308. #[test]
  309. fn operation_insert_node_when_multiple_parent_is_not_exist_test() {
  310. let mut test = NodeTest::new();
  311. let text_1 = NodeDataBuilder::new("text_1").build();
  312. let text_2 = NodeDataBuilder::new("text_2").build();
  313. let path = vec![1, 0, 0, 0, 0, 0];
  314. let mut auto_fill_node = placeholder_node();
  315. let mut iter_node: &mut NodeData = &mut auto_fill_node;
  316. let insert_path = path.split_at(1).1;
  317. for (index, _) in insert_path.iter().enumerate() {
  318. if index == insert_path.len() - 1 {
  319. iter_node.children.push(text_2.clone());
  320. } else {
  321. iter_node.children.push(placeholder_node());
  322. iter_node = iter_node.children.last_mut().unwrap();
  323. }
  324. }
  325. let scripts = vec![
  326. InsertNode {
  327. path: 0.into(),
  328. node_data: text_1,
  329. rev_id: 1,
  330. },
  331. InsertNode {
  332. path: path.clone().into(),
  333. node_data: text_2.clone(),
  334. rev_id: 2,
  335. },
  336. AssertNode {
  337. path: vec![1].into(),
  338. expected: Some(auto_fill_node),
  339. },
  340. AssertNode {
  341. path: path.into(),
  342. expected: Some(text_2),
  343. },
  344. ];
  345. test.run_scripts(scripts);
  346. }
  347. #[test]
  348. fn operation_insert_node_when_multiple_parent_is_not_exist_test2() {
  349. let mut test = NodeTest::new();
  350. // 0:ghost
  351. // 0:ghost
  352. // 1:ghost
  353. // 0:text
  354. let mut text_node_parent = placeholder_node();
  355. let text_node = NodeDataBuilder::new("text").build();
  356. text_node_parent.children.push(text_node.clone());
  357. let mut ghost = placeholder_node();
  358. ghost.children.push(placeholder_node());
  359. ghost.children.push(text_node_parent.clone());
  360. let path = vec![1, 1, 0];
  361. let scripts = vec![
  362. InsertNode {
  363. path: path.into(),
  364. node_data: text_node.clone(),
  365. rev_id: 1,
  366. },
  367. // 0:ghost
  368. // 1:ghost
  369. // 0:ghost
  370. // 1:ghost
  371. // 0:text
  372. AssertNode {
  373. path: 0.into(),
  374. expected: Some(placeholder_node()),
  375. },
  376. AssertNode {
  377. path: 1.into(),
  378. expected: Some(ghost),
  379. },
  380. AssertNumberOfChildrenAtPath {
  381. path: Some(1.into()),
  382. expected: 2,
  383. },
  384. AssertNode {
  385. path: vec![1, 1].into(),
  386. expected: Some(text_node_parent),
  387. },
  388. AssertNode {
  389. path: vec![1, 1, 0].into(),
  390. expected: Some(text_node),
  391. },
  392. ];
  393. test.run_scripts(scripts);
  394. }
  395. #[test]
  396. fn operation_insert_node_when_multiple_parent_is_not_exist_test3() {
  397. let mut test = NodeTest::new();
  398. let text_node = NodeDataBuilder::new("text").build();
  399. let path = vec![3, 3, 0];
  400. let scripts = vec![
  401. InsertNode {
  402. path: path.clone().into(),
  403. node_data: text_node.clone(),
  404. rev_id: 1,
  405. },
  406. // 0:ghost
  407. // 1:ghost
  408. // 2:ghost
  409. // 3:ghost
  410. // 0:ghost
  411. // 1:ghost
  412. // 2:ghost
  413. // 3:ghost
  414. // 0:text
  415. AssertNode {
  416. path: 0.into(),
  417. expected: Some(placeholder_node()),
  418. },
  419. AssertNode {
  420. path: 1.into(),
  421. expected: Some(placeholder_node()),
  422. },
  423. AssertNode {
  424. path: 2.into(),
  425. expected: Some(placeholder_node()),
  426. },
  427. AssertNumberOfChildrenAtPath {
  428. path: Some(3.into()),
  429. expected: 4,
  430. },
  431. AssertNode {
  432. path: path.into(),
  433. expected: Some(text_node),
  434. },
  435. ];
  436. test.run_scripts(scripts);
  437. }