script.rs 3.5 KB

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