script.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #![allow(clippy::all)]
  2. use lib_ot::core::{NodeTreeContext, OperationTransform, Transaction};
  3. use lib_ot::text_delta::DeltaTextOperationBuilder;
  4. use lib_ot::{
  5. core::attributes::AttributeHashMap,
  6. core::{Body, Changeset, NodeData, NodeTree, Path, TransactionBuilder},
  7. text_delta::DeltaTextOperations,
  8. };
  9. use std::collections::HashMap;
  10. pub enum NodeScript {
  11. InsertNode {
  12. path: Path,
  13. node_data: NodeData,
  14. rev_id: usize,
  15. },
  16. InsertNodes {
  17. path: Path,
  18. node_data_list: Vec<NodeData>,
  19. rev_id: usize,
  20. },
  21. UpdateAttributes {
  22. path: Path,
  23. attributes: AttributeHashMap,
  24. },
  25. UpdateBody {
  26. path: Path,
  27. changeset: Changeset,
  28. },
  29. DeleteNode {
  30. path: Path,
  31. rev_id: usize,
  32. },
  33. AssertNumberOfChildrenAtPath {
  34. path: Option<Path>,
  35. expected: usize,
  36. },
  37. AssertNodesAtRoot {
  38. expected: Vec<NodeData>,
  39. },
  40. #[allow(dead_code)]
  41. AssertNodesAtPath {
  42. path: Path,
  43. expected: Vec<NodeData>,
  44. },
  45. AssertNode {
  46. path: Path,
  47. expected: Option<NodeData>,
  48. },
  49. AssertNodeDelta {
  50. path: Path,
  51. expected: DeltaTextOperations,
  52. },
  53. #[allow(dead_code)]
  54. AssertTreeJSON {
  55. expected: String,
  56. },
  57. }
  58. pub struct NodeTest {
  59. rev_id: usize,
  60. rev_operations: HashMap<usize, Transaction>,
  61. node_tree: NodeTree,
  62. }
  63. impl NodeTest {
  64. pub fn new() -> Self {
  65. Self {
  66. rev_id: 0,
  67. rev_operations: HashMap::new(),
  68. node_tree: NodeTree::new(NodeTreeContext::default()),
  69. }
  70. }
  71. pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
  72. for script in scripts {
  73. self.run_script(script);
  74. }
  75. }
  76. pub fn run_script(&mut self, script: NodeScript) {
  77. match script {
  78. NodeScript::InsertNode {
  79. path,
  80. node_data: node,
  81. rev_id,
  82. } => {
  83. let mut transaction = TransactionBuilder::new().insert_node_at_path(path, node).build();
  84. self.transform_transaction_if_need(&mut transaction, rev_id);
  85. self.apply_transaction(transaction);
  86. }
  87. NodeScript::InsertNodes {
  88. path,
  89. node_data_list,
  90. rev_id,
  91. } => {
  92. let mut transaction = TransactionBuilder::new()
  93. .insert_nodes_at_path(path, node_data_list)
  94. .build();
  95. self.transform_transaction_if_need(&mut transaction, rev_id);
  96. self.apply_transaction(transaction);
  97. }
  98. NodeScript::UpdateAttributes { path, attributes } => {
  99. let node = self.node_tree.get_node_data_at_path(&path).unwrap();
  100. let transaction = TransactionBuilder::new()
  101. .update_node_at_path(
  102. &path,
  103. Changeset::Attributes {
  104. new: attributes,
  105. old: node.attributes,
  106. },
  107. )
  108. .build();
  109. self.apply_transaction(transaction);
  110. }
  111. NodeScript::UpdateBody { path, changeset } => {
  112. //
  113. let transaction = TransactionBuilder::new().update_node_at_path(&path, changeset).build();
  114. self.apply_transaction(transaction);
  115. }
  116. NodeScript::DeleteNode { path, rev_id } => {
  117. let mut transaction = TransactionBuilder::new()
  118. .delete_node_at_path(&self.node_tree, &path)
  119. .build();
  120. self.transform_transaction_if_need(&mut transaction, rev_id);
  121. self.apply_transaction(transaction);
  122. }
  123. NodeScript::AssertNode { path, expected } => {
  124. let node = self.node_tree.get_node_data_at_path(&path);
  125. assert_eq!(node, expected.map(|e| e.into()));
  126. }
  127. NodeScript::AssertNumberOfChildrenAtPath { path, expected } => match path {
  128. None => {
  129. let len = self.node_tree.number_of_children(None);
  130. assert_eq!(len, expected)
  131. }
  132. Some(path) => {
  133. let node_id = self.node_tree.node_id_at_path(path).unwrap();
  134. let len = self.node_tree.number_of_children(Some(node_id));
  135. assert_eq!(len, expected)
  136. }
  137. },
  138. NodeScript::AssertNodesAtRoot { expected } => {
  139. let nodes = self.node_tree.get_node_data_at_root().unwrap().children;
  140. assert_eq!(nodes, expected)
  141. }
  142. NodeScript::AssertNodesAtPath { path, expected } => {
  143. let nodes = self.node_tree.get_node_data_at_path(&path).unwrap().children;
  144. assert_eq!(nodes, expected)
  145. }
  146. NodeScript::AssertNodeDelta { path, expected } => {
  147. let node = self.node_tree.get_node_at_path(&path).unwrap();
  148. if let Body::Delta(delta) = node.body.clone() {
  149. debug_assert_eq!(delta, expected);
  150. } else {
  151. panic!("Node body type not match, expect Delta");
  152. }
  153. }
  154. NodeScript::AssertTreeJSON { expected } => {
  155. let json = serde_json::to_string(&self.node_tree).unwrap();
  156. assert_eq!(json, expected)
  157. }
  158. }
  159. }
  160. fn apply_transaction(&mut self, transaction: Transaction) {
  161. self.rev_id += 1;
  162. self.rev_operations.insert(self.rev_id, transaction.clone());
  163. self.node_tree.apply_transaction(transaction).unwrap();
  164. }
  165. fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
  166. if self.rev_id >= rev_id {
  167. for rev_id in rev_id..=self.rev_id {
  168. let old_transaction = self.rev_operations.get(&rev_id).unwrap();
  169. *transaction = old_transaction.transform(transaction).unwrap();
  170. }
  171. }
  172. }
  173. }
  174. pub fn edit_node_delta(
  175. delta: &DeltaTextOperations,
  176. new_delta: DeltaTextOperations,
  177. ) -> (Changeset, DeltaTextOperations) {
  178. let inverted = new_delta.invert(&delta);
  179. let expected = delta.compose(&new_delta).unwrap();
  180. let changeset = Changeset::Delta {
  181. delta: new_delta.clone(),
  182. inverted: inverted.clone(),
  183. };
  184. (changeset, expected)
  185. }
  186. pub fn make_node_delta_changeset(
  187. initial_content: &str,
  188. insert_str: &str,
  189. ) -> (DeltaTextOperations, Changeset, DeltaTextOperations) {
  190. let initial_content = initial_content.to_owned();
  191. let initial_delta = DeltaTextOperationBuilder::new().insert(&initial_content).build();
  192. let delta = DeltaTextOperationBuilder::new()
  193. .retain(initial_content.len())
  194. .insert(insert_str)
  195. .build();
  196. let inverted = delta.invert(&initial_delta);
  197. let expected = initial_delta.compose(&delta).unwrap();
  198. let changeset = Changeset::Delta {
  199. delta: delta.clone(),
  200. inverted: inverted.clone(),
  201. };
  202. (initial_delta, changeset, expected)
  203. }