script.rs 3.5 KB

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