script.rs 7.4 KB

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