script.rs 5.3 KB

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