operation_insert_test.rs 11 KB

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