script.rs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use lib_ot::{
  2. core::attributes::Attributes,
  3. core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
  4. text_delta::TextDelta,
  5. };
  6. pub enum NodeScript {
  7. InsertNode { path: Path, node: NodeData },
  8. UpdateAttributes { path: Path, attributes: Attributes },
  9. UpdateBody { path: Path, changeset: NodeBodyChangeset },
  10. DeleteNode { path: Path },
  11. AssertNumberOfNodesAtPath { path: Option<Path>, len: usize },
  12. AssertNode { path: Path, expected: Option<NodeData> },
  13. AssertNodeDelta { path: Path, expected: TextDelta },
  14. }
  15. pub struct NodeTest {
  16. node_tree: NodeTree,
  17. }
  18. impl NodeTest {
  19. pub fn new() -> Self {
  20. Self {
  21. node_tree: NodeTree::new("root"),
  22. }
  23. }
  24. pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
  25. for script in scripts {
  26. self.run_script(script);
  27. }
  28. }
  29. pub fn run_script(&mut self, script: NodeScript) {
  30. match script {
  31. NodeScript::InsertNode { path, node } => {
  32. let transaction = TransactionBuilder::new(&self.node_tree)
  33. .insert_node_at_path(path, node)
  34. .finalize();
  35. self.node_tree.apply(transaction).unwrap();
  36. }
  37. NodeScript::UpdateAttributes { path, attributes } => {
  38. let transaction = TransactionBuilder::new(&self.node_tree)
  39. .update_attributes_at_path(&path, attributes)
  40. .finalize();
  41. self.node_tree.apply(transaction).unwrap();
  42. }
  43. NodeScript::UpdateBody { path, changeset } => {
  44. //
  45. let transaction = TransactionBuilder::new(&self.node_tree)
  46. .update_body_at_path(&path, changeset)
  47. .finalize();
  48. self.node_tree.apply(transaction).unwrap();
  49. }
  50. NodeScript::DeleteNode { path } => {
  51. let transaction = TransactionBuilder::new(&self.node_tree)
  52. .delete_node_at_path(&path)
  53. .finalize();
  54. self.node_tree.apply(transaction).unwrap();
  55. }
  56. NodeScript::AssertNode { path, expected } => {
  57. let node_id = self.node_tree.node_id_at_path(path);
  58. match node_id {
  59. None => assert!(node_id.is_none()),
  60. Some(node_id) => {
  61. let node_data = self.node_tree.get_node(node_id).cloned();
  62. assert_eq!(node_data, expected.map(|e| e.into()));
  63. }
  64. }
  65. }
  66. NodeScript::AssertNumberOfNodesAtPath {
  67. path,
  68. len: expected_len,
  69. } => match path {
  70. None => {
  71. let len = self.node_tree.number_of_children(None);
  72. assert_eq!(len, expected_len)
  73. }
  74. Some(path) => {
  75. let node_id = self.node_tree.node_id_at_path(path).unwrap();
  76. let len = self.node_tree.number_of_children(Some(node_id));
  77. assert_eq!(len, expected_len)
  78. }
  79. },
  80. NodeScript::AssertNodeDelta { path, expected } => {
  81. let node = self.node_tree.get_node_at_path(&path).unwrap();
  82. if let NodeBody::Delta(delta) = node.body.clone() {
  83. debug_assert_eq!(delta, expected);
  84. } else {
  85. panic!("Node body type not match, expect Delta");
  86. }
  87. }
  88. }
  89. }
  90. }