script.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. use lib_ot::core::{Node, Transaction};
  2. use lib_ot::{
  3. core::attributes::AttributeHashMap,
  4. core::{Body, Changeset, NodeData, NodeTree, Path, TransactionBuilder},
  5. text_delta::TextOperations,
  6. };
  7. use std::collections::HashMap;
  8. pub enum NodeScript {
  9. InsertNode {
  10. path: Path,
  11. node_data: NodeData,
  12. rev_id: usize,
  13. },
  14. UpdateAttributes {
  15. path: Path,
  16. attributes: AttributeHashMap,
  17. },
  18. UpdateBody {
  19. path: Path,
  20. changeset: Changeset,
  21. },
  22. DeleteNode {
  23. path: Path,
  24. rev_id: usize,
  25. },
  26. AssertNumberOfNodesAtPath {
  27. path: Option<Path>,
  28. len: usize,
  29. },
  30. AssertNodeData {
  31. path: Path,
  32. expected: Option<NodeData>,
  33. },
  34. AssertNode {
  35. path: Path,
  36. expected: Option<Node>,
  37. },
  38. AssertNodeDelta {
  39. path: Path,
  40. expected: TextOperations,
  41. },
  42. AssertNodeJSON {
  43. expected: String,
  44. },
  45. }
  46. pub struct NodeTest {
  47. rev_id: usize,
  48. rev_operations: HashMap<usize, Transaction>,
  49. node_tree: NodeTree,
  50. }
  51. impl NodeTest {
  52. pub fn new() -> Self {
  53. Self {
  54. rev_id: 0,
  55. rev_operations: HashMap::new(),
  56. node_tree: NodeTree::new("root"),
  57. }
  58. }
  59. pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
  60. for script in scripts {
  61. self.run_script(script);
  62. }
  63. }
  64. pub fn run_script(&mut self, script: NodeScript) {
  65. match script {
  66. NodeScript::InsertNode {
  67. path,
  68. node_data: node,
  69. rev_id,
  70. } => {
  71. let mut transaction = TransactionBuilder::new(&self.node_tree)
  72. .insert_node_at_path(path, node)
  73. .finalize();
  74. self.transform_transaction_if_need(&mut transaction, rev_id);
  75. self.apply_transaction(transaction);
  76. }
  77. NodeScript::UpdateAttributes { path, attributes } => {
  78. let transaction = TransactionBuilder::new(&self.node_tree)
  79. .update_attributes_at_path(&path, attributes)
  80. .finalize();
  81. self.apply_transaction(transaction);
  82. }
  83. NodeScript::UpdateBody { path, changeset } => {
  84. //
  85. let transaction = TransactionBuilder::new(&self.node_tree)
  86. .update_body_at_path(&path, changeset)
  87. .finalize();
  88. self.apply_transaction(transaction);
  89. }
  90. NodeScript::DeleteNode { path, rev_id } => {
  91. let mut transaction = TransactionBuilder::new(&self.node_tree)
  92. .delete_node_at_path(&path)
  93. .finalize();
  94. self.transform_transaction_if_need(&mut transaction, rev_id);
  95. self.apply_transaction(transaction);
  96. }
  97. NodeScript::AssertNode { path, expected } => {
  98. let node_id = self.node_tree.node_id_at_path(path);
  99. if expected.is_none() && node_id.is_none() {
  100. return;
  101. }
  102. let node = self.node_tree.get_node(node_id.unwrap()).cloned();
  103. assert_eq!(node, expected);
  104. }
  105. NodeScript::AssertNodeData { path, expected } => {
  106. let node_id = self.node_tree.node_id_at_path(path);
  107. match node_id {
  108. None => assert!(node_id.is_none()),
  109. Some(node_id) => {
  110. let node = self.node_tree.get_node(node_id).cloned();
  111. assert_eq!(node, expected.map(|e| e.into()));
  112. }
  113. }
  114. }
  115. NodeScript::AssertNumberOfNodesAtPath {
  116. path,
  117. len: expected_len,
  118. } => match path {
  119. None => {
  120. let len = self.node_tree.number_of_children(None);
  121. assert_eq!(len, expected_len)
  122. }
  123. Some(path) => {
  124. let node_id = self.node_tree.node_id_at_path(path).unwrap();
  125. let len = self.node_tree.number_of_children(Some(node_id));
  126. assert_eq!(len, expected_len)
  127. }
  128. },
  129. NodeScript::AssertNodeDelta { path, expected } => {
  130. let node = self.node_tree.get_node_at_path(&path).unwrap();
  131. if let Body::Delta(delta) = node.body.clone() {
  132. debug_assert_eq!(delta, expected);
  133. } else {
  134. panic!("Node body type not match, expect Delta");
  135. }
  136. }
  137. NodeScript::AssertNodeJSON { expected } => {
  138. let mut children = self.node_tree.children_from_node(self.node_tree.root_node());
  139. let node = children.next().unwrap();
  140. let json = self.node_tree.to_json(node, true).unwrap();
  141. assert_eq!(json, expected)
  142. }
  143. }
  144. }
  145. fn apply_transaction(&mut self, transaction: Transaction) {
  146. self.rev_id += 1;
  147. self.rev_operations.insert(self.rev_id, transaction.clone());
  148. self.node_tree.apply_transaction(transaction).unwrap();
  149. }
  150. fn transform_transaction_if_need(&mut self, transaction: &mut Transaction, rev_id: usize) {
  151. if self.rev_id >= rev_id {
  152. for rev_id in rev_id..=self.rev_id {
  153. let old_transaction = self.rev_operations.get(&rev_id).unwrap();
  154. *transaction = old_transaction.transform(transaction).unwrap();
  155. }
  156. }
  157. }
  158. }