main.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.get_node_data(node).unwrap();
  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.get_node_data(node).unwrap();
  76. assert_eq!(data.node_type, "image");
  77. }
  78. #[test]
  79. fn test_update_nodes() {
  80. let mut document = DocumentTree::new();
  81. let transaction = {
  82. let mut tb = TransactionBuilder::new(&document);
  83. tb.insert_node_at_path(&vec![0], NodeSubTree::new("text"));
  84. tb.insert_node_at_path(&vec![1], NodeSubTree::new("text"));
  85. tb.insert_node_at_path(vec![2], NodeSubTree::new("text"));
  86. tb.finalize()
  87. };
  88. document.apply(transaction).unwrap();
  89. let transaction = {
  90. let mut tb = TransactionBuilder::new(&document);
  91. tb.update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))]));
  92. tb.finalize()
  93. };
  94. document.apply(transaction).unwrap();
  95. let node = document.node_at_path(&Path(vec![1])).unwrap();
  96. let node_data = document.get_node_data(node).unwrap();
  97. let is_bold = node_data.attributes.0.get("bolded").unwrap().clone();
  98. assert_eq!(is_bold.unwrap(), "true");
  99. }
  100. #[test]
  101. fn test_delete_nodes() {
  102. let mut document = DocumentTree::new();
  103. let transaction = {
  104. let mut tb = TransactionBuilder::new(&document);
  105. tb.insert_node_at_path(0, NodeSubTree::new("text"));
  106. tb.insert_node_at_path(1, NodeSubTree::new("text"));
  107. tb.insert_node_at_path(2, NodeSubTree::new("text"));
  108. tb.finalize()
  109. };
  110. document.apply(transaction).unwrap();
  111. let transaction = {
  112. let mut tb = TransactionBuilder::new(&document);
  113. tb.delete_node_at_path(&Path(vec![1]));
  114. tb.finalize()
  115. };
  116. document.apply(transaction).unwrap();
  117. let len = document.number_of_children();
  118. assert_eq!(len, 2);
  119. }
  120. #[test]
  121. fn test_errors() {
  122. let mut document = DocumentTree::new();
  123. let transaction = {
  124. let mut tb = TransactionBuilder::new(&document);
  125. tb.insert_node_at_path(0, NodeSubTree::new("text"));
  126. tb.insert_node_at_path(100, NodeSubTree::new("text"));
  127. tb.finalize()
  128. };
  129. let result = document.apply(transaction);
  130. assert_eq!(result.err().unwrap().code, OTErrorCode::PathNotFound);
  131. }