script.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. use lib_ot::core::{Node, Transaction};
  2. use lib_ot::{
  3. core::attributes::AttributeHashMap,
  4. core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
  5. text_delta::TextOperations,
  6. };
  7. use std::collections::HashMap;
  8. pub enum NodeScript {
  9. InsertNode {
  10. path: Path,
  11. node_data: NodeData,
  12. rev_id: usize,
  13. },
  14. UpdateAttributes {
  15. path: Path,
  16. attributes: AttributeHashMap,
  17. },
  18. UpdateBody {
  19. path: Path,
  20. changeset: NodeBodyChangeset,
  21. },
  22. DeleteNode {
  23. path: Path,
  24. rev_id: usize,
  25. },
  26. AssertNumberOfNodesAtPath {
  27. path: Option<Path>,
  28. len: usize,
  29. },
  30. AssertNodeData {
  31. path: Path,
  32. expected: Option<NodeData>,
  33. },
  34. AssertNode {
  35. path: Path,
  36. expected: Option<Node>,
  37. },
  38. AssertNodeDelta {
  39. path: Path,
  40. expected: TextOperations,
  41. },
  42. }
  43. pub struct NodeTest {
  44. rev_id: usize,
  45. rev_operations: HashMap<usize, Transaction>,
  46. node_tree: NodeTree,
  47. }
  48. impl NodeTest {
  49. pub fn new() -> Self {
  50. Self {
  51. rev_id: 0,
  52. rev_operations: HashMap::new(),
  53. node_tree: NodeTree::new("root"),
  54. }
  55. }
  56. pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
  57. for script in scripts {
  58. self.run_script(script);
  59. }
  60. }
  61. pub fn run_script(&mut self, script: NodeScript) {
  62. match script {
  63. NodeScript::InsertNode {
  64. path,
  65. node_data: node,
  66. rev_id,
  67. } => {
  68. let mut transaction = TransactionBuilder::new(&self.node_tree)
  69. .insert_node_at_path(path, node)
  70. .finalize();
  71. self.transform_transaction_if_need(&mut transaction, rev_id);
  72. self.apply_transaction(transaction);
  73. }
  74. NodeScript::UpdateAttributes { path, attributes } => {
  75. let transaction = TransactionBuilder::new(&self.node_tree)
  76. .update_attributes_at_path(&path, attributes)
  77. .finalize();
  78. self.apply_transaction(transaction);
  79. }
  80. NodeScript::UpdateBody { path, changeset } => {
  81. //
  82. let transaction = TransactionBuilder::new(&self.node_tree)
  83. .update_body_at_path(&path, changeset)
  84. .finalize();
  85. self.apply_transaction(transaction);
  86. }
  87. NodeScript::DeleteNode { path, rev_id } => {
  88. let mut transaction = TransactionBuilder::new(&self.node_tree)
  89. .delete_node_at_path(&path)
  90. .finalize();
  91. self.transform_transaction_if_need(&mut transaction, rev_id);
  92. self.apply_transaction(transaction);
  93. }
  94. NodeScript::AssertNode { path, expected } => {
  95. let node_id = self.node_tree.node_id_at_path(path);
  96. if expected.is_none() && node_id.is_none() {
  97. return;
  98. }
  99. let node = self.node_tree.get_node(node_id.unwrap()).cloned();
  100. assert_eq!(node, expected);
  101. }
  102. NodeScript::AssertNodeData { path, expected } => {
  103. let node_id = self.node_tree.node_id_at_path(path);
  104. match node_id {
  105. None => assert!(node_id.is_none()),
  106. Some(node_id) => {
  107. let node = self.node_tree.get_node(node_id).cloned();
  108. assert_eq!(node, expected.map(|e| e.into()));
  109. }
  110. }
  111. }
  112. NodeScript::AssertNumberOfNodesAtPath {
  113. path,
  114. len: expected_len,
  115. } => match path {
  116. None => {
  117. let len = self.node_tree.number_of_children(None);
  118. assert_eq!(len, expected_len)
  119. }
  120. Some(path) => {
  121. let node_id = self.node_tree.node_id_at_path(path).unwrap();
  122. let len = self.node_tree.number_of_children(Some(node_id));
  123. assert_eq!(len, expected_len)
  124. }
  125. },
  126. NodeScript::AssertNodeDelta { path, expected } => {
  127. let node = self.node_tree.get_node_at_path(&path).unwrap();
  128. if let NodeBody::Delta(delta) = node.body.clone() {
  129. debug_assert_eq!(delta, expected);
  130. } else {
  131. panic!("Node body type not match, expect Delta");
  132. }
  133. }
  134. }
  135. }
  136. fn apply_transaction(&mut self, transaction: Transaction) {
  137. self.rev_id += 1;
  138. self.rev_operations.insert(self.rev_id, transaction.clone());
  139. self.node_tree.apply_transaction(transaction).unwrap();
  140. }
  141. fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
  142. if self.rev_id >= rev_id {
  143. for rev_id in rev_id..=self.rev_id {
  144. let old_transaction = self.rev_operations.get(&rev_id).unwrap();
  145. *transaction = old_transaction.transform(transaction).unwrap();
  146. }
  147. }
  148. }
  149. }