script.rs 7.9 KB

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