script.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. AssertNodeAttributes {
  50. path: Path,
  51. expected: &'static str,
  52. },
  53. AssertNodeDelta {
  54. path: Path,
  55. expected: DeltaTextOperations,
  56. },
  57. #[allow(dead_code)]
  58. AssertTreeJSON {
  59. expected: String,
  60. },
  61. }
  62. pub struct NodeTest {
  63. rev_id: usize,
  64. rev_operations: HashMap<usize, Transaction>,
  65. node_tree: NodeTree,
  66. }
  67. impl NodeTest {
  68. pub fn new() -> Self {
  69. Self {
  70. rev_id: 0,
  71. rev_operations: HashMap::new(),
  72. node_tree: NodeTree::new(NodeTreeContext::default()),
  73. }
  74. }
  75. pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
  76. for script in scripts {
  77. self.run_script(script);
  78. }
  79. }
  80. pub fn run_script(&mut self, script: NodeScript) {
  81. match script {
  82. NodeScript::InsertNode {
  83. path,
  84. node_data: node,
  85. rev_id,
  86. } => {
  87. let mut transaction = TransactionBuilder::new().insert_node_at_path(path, node).build();
  88. self.transform_transaction_if_need(&mut transaction, rev_id);
  89. self.apply_transaction(transaction);
  90. }
  91. NodeScript::InsertNodes {
  92. path,
  93. node_data_list,
  94. rev_id,
  95. } => {
  96. let mut transaction = TransactionBuilder::new()
  97. .insert_nodes_at_path(path, node_data_list)
  98. .build();
  99. self.transform_transaction_if_need(&mut transaction, rev_id);
  100. self.apply_transaction(transaction);
  101. }
  102. NodeScript::UpdateAttributes { path, attributes } => {
  103. let node = self.node_tree.get_node_data_at_path(&path).unwrap();
  104. let transaction = TransactionBuilder::new()
  105. .update_node_at_path(
  106. &path,
  107. Changeset::Attributes {
  108. new: attributes,
  109. old: node.attributes,
  110. },
  111. )
  112. .build();
  113. self.apply_transaction(transaction);
  114. }
  115. NodeScript::UpdateBody { path, changeset } => {
  116. //
  117. let transaction = TransactionBuilder::new().update_node_at_path(&path, changeset).build();
  118. self.apply_transaction(transaction);
  119. }
  120. NodeScript::DeleteNode { path, rev_id } => {
  121. let mut transaction = TransactionBuilder::new()
  122. .delete_node_at_path(&self.node_tree, &path)
  123. .build();
  124. self.transform_transaction_if_need(&mut transaction, rev_id);
  125. self.apply_transaction(transaction);
  126. }
  127. NodeScript::AssertNode { path, expected } => {
  128. let node = self.node_tree.get_node_data_at_path(&path);
  129. assert_eq!(node, expected.map(|e| e.into()));
  130. }
  131. NodeScript::AssertNodeAttributes { path, expected } => {
  132. let node = self.node_tree.get_node_data_at_path(&path).unwrap();
  133. assert_eq!(node.attributes.to_json().unwrap(), expected);
  134. }
  135. NodeScript::AssertNumberOfChildrenAtPath { path, expected } => match path {
  136. None => {
  137. let len = self.node_tree.number_of_children(None);
  138. assert_eq!(len, expected)
  139. }
  140. Some(path) => {
  141. let node_id = self.node_tree.node_id_at_path(path).unwrap();
  142. let len = self.node_tree.number_of_children(Some(node_id));
  143. assert_eq!(len, expected)
  144. }
  145. },
  146. NodeScript::AssertNodesAtRoot { expected } => {
  147. let nodes = self.node_tree.get_node_data_at_root().unwrap().children;
  148. assert_eq!(nodes, expected)
  149. }
  150. NodeScript::AssertNodesAtPath { path, expected } => {
  151. let nodes = self.node_tree.get_node_data_at_path(&path).unwrap().children;
  152. assert_eq!(nodes, expected)
  153. }
  154. NodeScript::AssertNodeDelta { path, expected } => {
  155. let node = self.node_tree.get_node_at_path(&path).unwrap();
  156. if let Body::Delta(delta) = node.body.clone() {
  157. debug_assert_eq!(delta, expected);
  158. } else {
  159. panic!("Node body type not match, expect Delta");
  160. }
  161. }
  162. NodeScript::AssertTreeJSON { expected } => {
  163. let json = serde_json::to_string(&self.node_tree).unwrap();
  164. assert_eq!(json, expected)
  165. }
  166. }
  167. }
  168. fn apply_transaction(&mut self, transaction: Transaction) {
  169. self.rev_id += 1;
  170. self.rev_operations.insert(self.rev_id, transaction.clone());
  171. self.node_tree.apply_transaction(transaction).unwrap();
  172. }
  173. fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
  174. if self.rev_id >= rev_id {
  175. for rev_id in rev_id..=self.rev_id {
  176. let old_transaction = self.rev_operations.get(&rev_id).unwrap();
  177. *transaction = old_transaction.transform(transaction).unwrap();
  178. }
  179. }
  180. }
  181. }
  182. pub fn edit_node_delta(
  183. delta: &DeltaTextOperations,
  184. new_delta: DeltaTextOperations,
  185. ) -> (Changeset, DeltaTextOperations) {
  186. let inverted = new_delta.invert(&delta);
  187. let expected = delta.compose(&new_delta).unwrap();
  188. let changeset = Changeset::Delta {
  189. delta: new_delta.clone(),
  190. inverted: inverted.clone(),
  191. };
  192. (changeset, expected)
  193. }
  194. pub fn make_node_delta_changeset(
  195. initial_content: &str,
  196. insert_str: &str,
  197. ) -> (DeltaTextOperations, Changeset, DeltaTextOperations) {
  198. let initial_content = initial_content.to_owned();
  199. let initial_delta = DeltaTextOperationBuilder::new().insert(&initial_content).build();
  200. let delta = DeltaTextOperationBuilder::new()
  201. .retain(initial_content.len())
  202. .insert(insert_str)
  203. .build();
  204. let inverted = delta.invert(&initial_delta);
  205. let expected = initial_delta.compose(&delta).unwrap();
  206. let changeset = Changeset::Delta {
  207. delta: delta.clone(),
  208. inverted: inverted.clone(),
  209. };
  210. (initial_delta, changeset, expected)
  211. }