main.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. use lib_ot::core::{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 = TransactionBuilder::new(&document)
  13. .insert_node_at_path(0, NodeSubTree::new("text"))
  14. .finalize();
  15. document.apply(transaction).unwrap();
  16. assert!(document.node_at_path(0).is_some());
  17. let node = document.node_at_path(0).unwrap();
  18. let node_data = document.get_node_data(node).unwrap();
  19. assert_eq!(node_data.node_type, "text");
  20. let transaction = TransactionBuilder::new(&document)
  21. .update_attributes_at_path(
  22. &vec![0].into(),
  23. HashMap::from([("subtype".into(), Some("bullet-list".into()))]),
  24. )
  25. .finalize();
  26. document.apply(transaction).unwrap();
  27. let transaction = TransactionBuilder::new(&document)
  28. .delete_node_at_path(&vec![0].into())
  29. .finalize();
  30. document.apply(transaction).unwrap();
  31. assert!(document.node_at_path(0).is_none());
  32. }
  33. #[test]
  34. fn test_inserts_nodes() {
  35. let mut document = DocumentTree::new();
  36. let transaction = TransactionBuilder::new(&document)
  37. .insert_node_at_path(0, NodeSubTree::new("text"))
  38. .insert_node_at_path(1, NodeSubTree::new("text"))
  39. .insert_node_at_path(2, NodeSubTree::new("text"))
  40. .finalize();
  41. document.apply(transaction).unwrap();
  42. let transaction = TransactionBuilder::new(&document)
  43. .insert_node_at_path(1, NodeSubTree::new("text"))
  44. .finalize();
  45. document.apply(transaction).unwrap();
  46. }
  47. #[test]
  48. fn test_inserts_subtrees() {
  49. let mut document = DocumentTree::new();
  50. let transaction = TransactionBuilder::new(&document)
  51. .insert_node_at_path(
  52. 0,
  53. NodeSubTree {
  54. note_type: "text".into(),
  55. attributes: NodeAttributes::new(),
  56. delta: None,
  57. children: vec![NodeSubTree::new("image")],
  58. },
  59. )
  60. .finalize();
  61. document.apply(transaction).unwrap();
  62. let node = document.node_at_path(&Path(vec![0, 0])).unwrap();
  63. let data = document.get_node_data(node).unwrap();
  64. assert_eq!(data.node_type, "image");
  65. }
  66. #[test]
  67. fn test_update_nodes() {
  68. let mut document = DocumentTree::new();
  69. let transaction = TransactionBuilder::new(&document)
  70. .insert_node_at_path(&vec![0], NodeSubTree::new("text"))
  71. .insert_node_at_path(&vec![1], NodeSubTree::new("text"))
  72. .insert_node_at_path(vec![2], NodeSubTree::new("text"))
  73. .finalize();
  74. document.apply(transaction).unwrap();
  75. let transaction = TransactionBuilder::new(&document)
  76. .update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))]))
  77. .finalize();
  78. document.apply(transaction).unwrap();
  79. let node = document.node_at_path(&Path(vec![1])).unwrap();
  80. let node_data = document.get_node_data(node).unwrap();
  81. let is_bold = node_data.attributes.0.get("bolded").unwrap().clone();
  82. assert_eq!(is_bold.unwrap(), "true");
  83. }
  84. #[test]
  85. fn test_delete_nodes() {
  86. let mut document = DocumentTree::new();
  87. let transaction = TransactionBuilder::new(&document)
  88. .insert_node_at_path(0, NodeSubTree::new("text"))
  89. .insert_node_at_path(1, NodeSubTree::new("text"))
  90. .insert_node_at_path(2, NodeSubTree::new("text"))
  91. .finalize();
  92. document.apply(transaction).unwrap();
  93. let transaction = TransactionBuilder::new(&document)
  94. .delete_node_at_path(&Path(vec![1]))
  95. .finalize();
  96. document.apply(transaction).unwrap();
  97. let len = document.number_of_children();
  98. assert_eq!(len, 2);
  99. }
  100. #[test]
  101. fn test_errors() {
  102. let mut document = DocumentTree::new();
  103. let transaction = TransactionBuilder::new(&document)
  104. .insert_node_at_path(0, NodeSubTree::new("text"))
  105. .insert_node_at_path(100, NodeSubTree::new("text"))
  106. .finalize();
  107. let result = document.apply(transaction);
  108. assert_eq!(result.err().unwrap().code, OTErrorCode::PathNotFound);
  109. }