script.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use lib_ot::core::{DocumentTree, NodeAttributes, NodeSubTree, Path, TransactionBuilder};
  2. pub enum NodeScript {
  3. InsertNode { path: Path, node: NodeSubTree },
  4. InsertAttributes { path: Path, attributes: NodeAttributes },
  5. DeleteNode { path: Path },
  6. AssertNumberOfChildrenAtPath { path: Option<Path>, len: usize },
  7. AssertNode { path: Path, expected: Option<NodeSubTree> },
  8. }
  9. pub struct NodeTest {
  10. node_tree: DocumentTree,
  11. }
  12. impl NodeTest {
  13. pub fn new() -> Self {
  14. Self {
  15. node_tree: DocumentTree::new(),
  16. }
  17. }
  18. pub fn run_scripts(&mut self, scripts: Vec<NodeScript>) {
  19. for script in scripts {
  20. self.run_script(script);
  21. }
  22. }
  23. pub fn run_script(&mut self, script: NodeScript) {
  24. match script {
  25. NodeScript::InsertNode { path, node } => {
  26. let transaction = TransactionBuilder::new(&self.node_tree)
  27. .insert_node_at_path(path, node)
  28. .finalize();
  29. self.node_tree.apply(transaction).unwrap();
  30. }
  31. NodeScript::InsertAttributes { path, attributes } => {
  32. let transaction = TransactionBuilder::new(&self.node_tree)
  33. .update_attributes_at_path(&path, attributes.to_inner())
  34. .finalize();
  35. self.node_tree.apply(transaction).unwrap();
  36. }
  37. NodeScript::DeleteNode { path } => {
  38. let transaction = TransactionBuilder::new(&self.node_tree)
  39. .delete_node_at_path(&path)
  40. .finalize();
  41. self.node_tree.apply(transaction).unwrap();
  42. }
  43. NodeScript::AssertNode { path, expected } => {
  44. let node_id = self.node_tree.node_at_path(path);
  45. match node_id {
  46. None => assert!(node_id.is_none()),
  47. Some(node_id) => {
  48. let node_data = self.node_tree.get_node_data(node_id).cloned();
  49. assert_eq!(node_data, expected.and_then(|e| Some(e.to_node_data())));
  50. }
  51. }
  52. }
  53. NodeScript::AssertNumberOfChildrenAtPath {
  54. path,
  55. len: expected_len,
  56. } => match path {
  57. None => {
  58. let len = self.node_tree.number_of_children(None);
  59. assert_eq!(len, expected_len)
  60. }
  61. Some(path) => {
  62. let node_id = self.node_tree.node_at_path(path).unwrap();
  63. let len = self.node_tree.number_of_children(Some(node_id));
  64. assert_eq!(len, expected_len)
  65. }
  66. },
  67. }
  68. }
  69. }