main.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. use lib_ot::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree, Path, TransactionBuilder};
  2. use lib_ot::errors::OTErrorCode;
  3. use std::collections::HashMap;
  4. #[test]
  5. fn main() {
  6. // Create a new arena
  7. let _document = DocumentTree::new();
  8. }
  9. #[test]
  10. fn test_documents() {
  11. let mut document = DocumentTree::new();
  12. let transaction = {
  13. let mut tb = TransactionBuilder::new(&document);
  14. tb.insert_node_at_path(0, NodeSubTree::new("text"));
  15. tb.finalize()
  16. };
  17. document.apply(transaction).unwrap();
  18. assert!(document.node_at_path(0).is_some());
  19. let node = document.node_at_path(0).unwrap();
  20. let node_data = document.arena.get(node).unwrap().get();
  21. assert_eq!(node_data.node_type, "text");
  22. let transaction = {
  23. let mut tb = TransactionBuilder::new(&document);
  24. tb.update_attributes_at_path(
  25. &vec![0].into(),
  26. HashMap::from([("subtype".into(), Some("bullet-list".into()))]),
  27. );
  28. tb.finalize()
  29. };
  30. document.apply(transaction).unwrap();
  31. let transaction = {
  32. let mut tb = TransactionBuilder::new(&document);
  33. tb.delete_node_at_path(&vec![0].into());
  34. tb.finalize()
  35. };
  36. document.apply(transaction).unwrap();
  37. assert!(document.node_at_path(0).is_none());
  38. }
  39. #[test]
  40. fn test_inserts_nodes() {
  41. let mut document = DocumentTree::new();
  42. let transaction = {
  43. let mut tb = TransactionBuilder::new(&document);
  44. tb.insert_node_at_path(0, NodeSubTree::new("text"));
  45. tb.insert_node_at_path(1, NodeSubTree::new("text"));
  46. tb.insert_node_at_path(2, NodeSubTree::new("text"));
  47. tb.finalize()
  48. };
  49. document.apply(transaction).unwrap();
  50. let transaction = {
  51. let mut tb = TransactionBuilder::new(&document);
  52. tb.insert_node_at_path(1, NodeSubTree::new("text"));
  53. tb.finalize()
  54. };
  55. document.apply(transaction).unwrap();
  56. }
  57. #[test]
  58. fn test_inserts_subtrees() {
  59. let mut document = DocumentTree::new();
  60. let transaction = {
  61. let mut tb = TransactionBuilder::new(&document);
  62. tb.insert_node_at_path(
  63. 0,
  64. NodeSubTree {
  65. node_type: "text".into(),
  66. attributes: NodeAttributes::new(),
  67. delta: None,
  68. children: vec![NodeSubTree::new("image")],
  69. },
  70. );
  71. tb.finalize()
  72. };
  73. document.apply(transaction).unwrap();
  74. let node = document.node_at_path(&Path(vec![0, 0])).unwrap();
  75. let data = document.arena.get(node).unwrap().get();
  76. assert_eq!(data.node_type, "image");
  77. }
  78. #[test]
  79. fn test_insert_node() {
  80. let node = NodeSubTree::new("text");
  81. let root_path: Path = vec![0].into();
  82. let op = DocumentOperation::Insert {path: root_path.clone(),nodes: vec![node.clone()] };
  83. let mut document = DocumentTree::new();
  84. document.apply_op(&op).unwrap();
  85. let node_id = document.node_at_path(&root_path).unwrap();
  86. assert!(document.child_at_index_of_path(node_id, 0).is_some());
  87. }
  88. #[test]
  89. fn test_update_nodes() {
  90. let mut document = DocumentTree::new();
  91. let transaction = {
  92. let mut tb = TransactionBuilder::new(&document);
  93. tb.insert_node_at_path(&vec![0], NodeSubTree::new("text"));
  94. tb.insert_node_at_path(&vec![1], NodeSubTree::new("text"));
  95. tb.insert_node_at_path(vec![2], NodeSubTree::new("text"));
  96. tb.finalize()
  97. };
  98. document.apply(transaction).unwrap();
  99. let transaction = {
  100. let mut tb = TransactionBuilder::new(&document);
  101. tb.update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))]));
  102. tb.finalize()
  103. };
  104. document.apply(transaction).unwrap();
  105. let node = document.node_at_path(&Path(vec![1])).unwrap();
  106. let node_data = document.arena.get(node).unwrap().get();
  107. let is_bold = node_data.attributes.0.get("bolded").unwrap().clone();
  108. assert_eq!(is_bold.unwrap(), "true");
  109. }
  110. #[test]
  111. fn test_delete_nodes() {
  112. let mut document = DocumentTree::new();
  113. let transaction = {
  114. let mut tb = TransactionBuilder::new(&document);
  115. tb.insert_node_at_path(0, NodeSubTree::new("text"));
  116. tb.insert_node_at_path(1, NodeSubTree::new("text"));
  117. tb.insert_node_at_path(2, NodeSubTree::new("text"));
  118. tb.finalize()
  119. };
  120. document.apply(transaction).unwrap();
  121. let transaction = {
  122. let mut tb = TransactionBuilder::new(&document);
  123. tb.delete_node_at_path(&Path(vec![1]));
  124. tb.finalize()
  125. };
  126. document.apply(transaction).unwrap();
  127. let len = document.root.children(&document.arena).fold(0, |count, _| count + 1);
  128. assert_eq!(len, 2);
  129. }
  130. #[test]
  131. fn test_errors() {
  132. let mut document = DocumentTree::new();
  133. let transaction = {
  134. let mut tb = TransactionBuilder::new(&document);
  135. tb.insert_node_at_path(0, NodeSubTree::new("text"));
  136. tb.insert_node_at_path(100, NodeSubTree::new("text"));
  137. tb.finalize()
  138. };
  139. let result = document.apply(transaction);
  140. assert_eq!(result.err().unwrap().code, OTErrorCode::PathNotFound);
  141. }