script.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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()
  92. .insert_node_at_path(path, node)
  93. .build();
  94. self.transform_transaction_if_need(&mut transaction, rev_id);
  95. self.apply_transaction(transaction);
  96. },
  97. NodeScript::InsertNodes {
  98. path,
  99. node_data_list,
  100. rev_id,
  101. } => {
  102. let mut transaction = TransactionBuilder::new()
  103. .insert_nodes_at_path(path, node_data_list)
  104. .build();
  105. self.transform_transaction_if_need(&mut transaction, rev_id);
  106. self.apply_transaction(transaction);
  107. },
  108. NodeScript::UpdateAttributes { path, attributes } => {
  109. let node = self.node_tree.get_node_data_at_path(&path).unwrap();
  110. let transaction = TransactionBuilder::new()
  111. .update_node_at_path(
  112. &path,
  113. Changeset::Attributes {
  114. new: attributes,
  115. old: node.attributes,
  116. },
  117. )
  118. .build();
  119. self.apply_transaction(transaction);
  120. },
  121. NodeScript::UpdateBody { path, changeset } => {
  122. //
  123. let transaction = TransactionBuilder::new()
  124. .update_node_at_path(&path, changeset)
  125. .build();
  126. self.apply_transaction(transaction);
  127. },
  128. NodeScript::DeleteNode { path, rev_id } => {
  129. let mut transaction = TransactionBuilder::new()
  130. .delete_node_at_path(&self.node_tree, &path)
  131. .build();
  132. self.transform_transaction_if_need(&mut transaction, rev_id);
  133. self.apply_transaction(transaction);
  134. },
  135. NodeScript::AssertNode { path, expected } => {
  136. let node = self.node_tree.get_node_data_at_path(&path);
  137. assert_eq!(node, expected.map(|e| e.into()));
  138. },
  139. NodeScript::AssertNodeAttributes { path, expected } => {
  140. let node = self.node_tree.get_node_data_at_path(&path).unwrap();
  141. assert_eq!(node.attributes.to_json().unwrap(), expected);
  142. },
  143. NodeScript::AssertNumberOfChildrenAtPath { path, expected } => match path {
  144. None => {
  145. let len = self.node_tree.number_of_children(None);
  146. assert_eq!(len, expected)
  147. },
  148. Some(path) => {
  149. let node_id = self.node_tree.node_id_at_path(path).unwrap();
  150. let len = self.node_tree.number_of_children(Some(node_id));
  151. assert_eq!(len, expected)
  152. },
  153. },
  154. NodeScript::AssertNodesAtRoot { expected } => {
  155. let nodes = self.node_tree.get_node_data_at_root().unwrap().children;
  156. assert_eq!(nodes, expected)
  157. },
  158. NodeScript::AssertNodesAtPath { path, expected } => {
  159. let nodes = self
  160. .node_tree
  161. .get_node_data_at_path(&path)
  162. .unwrap()
  163. .children;
  164. assert_eq!(nodes, expected)
  165. },
  166. NodeScript::AssertNodeDelta { 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, expected);
  170. } else {
  171. panic!("Node body type not match, expect Delta");
  172. }
  173. },
  174. NodeScript::AssertNodeDeltaContent { path, expected } => {
  175. let node = self.node_tree.get_node_at_path(&path).unwrap();
  176. if let Body::Delta(delta) = node.body.clone() {
  177. debug_assert_eq!(delta.content().unwrap(), expected);
  178. } else {
  179. panic!("Node body type not match, expect Delta");
  180. }
  181. },
  182. NodeScript::AssertTreeJSON { expected } => {
  183. let json = serde_json::to_string(&self.node_tree).unwrap();
  184. assert_eq!(json, expected)
  185. },
  186. }
  187. }
  188. fn apply_transaction(&mut self, transaction: Transaction) {
  189. self.rev_id += 1;
  190. self.rev_operations.insert(self.rev_id, transaction.clone());
  191. self.node_tree.apply_transaction(transaction).unwrap();
  192. }
  193. fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
  194. if self.rev_id >= rev_id {
  195. for rev_id in rev_id..=self.rev_id {
  196. let old_transaction = self.rev_operations.get(&rev_id).unwrap();
  197. *transaction = old_transaction.transform(transaction).unwrap();
  198. }
  199. }
  200. }
  201. }
  202. pub fn edit_node_delta(
  203. delta: &DeltaTextOperations,
  204. new_delta: DeltaTextOperations,
  205. ) -> (Changeset, DeltaTextOperations) {
  206. let inverted = new_delta.invert(&delta);
  207. let expected = delta.compose(&new_delta).unwrap();
  208. let changeset = Changeset::Delta {
  209. delta: new_delta.clone(),
  210. inverted: inverted.clone(),
  211. };
  212. (changeset, expected)
  213. }
  214. pub fn make_node_delta_changeset(
  215. initial_content: &str,
  216. insert_str: &str,
  217. ) -> (DeltaTextOperations, Changeset, DeltaTextOperations) {
  218. let initial_content = initial_content.to_owned();
  219. let initial_delta = DeltaTextOperationBuilder::new()
  220. .insert(&initial_content)
  221. .build();
  222. let delta = DeltaTextOperationBuilder::new()
  223. .retain(initial_content.len())
  224. .insert(insert_str)
  225. .build();
  226. let inverted = delta.invert(&initial_delta);
  227. let expected = initial_delta.compose(&delta).unwrap();
  228. let changeset = Changeset::Delta {
  229. delta: delta.clone(),
  230. inverted: inverted.clone(),
  231. };
  232. (initial_delta, changeset, expected)
  233. }