script.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use flowy_document::editor::AppFlowyDocumentEditor;
  2. use flowy_test::helper::ViewTest;
  3. use flowy_test::FlowySDKTest;
  4. use lib_ot::core::{Body, Changeset, NodeDataBuilder, NodeOperation, Path, Transaction};
  5. use lib_ot::text_delta::TextOperations;
  6. use std::sync::Arc;
  7. pub enum EditScript {
  8. InsertText { path: Path, delta: TextOperations },
  9. UpdateText { path: Path, delta: TextOperations },
  10. Delete { path: Path },
  11. AssertContent { expected: &'static str },
  12. AssertPrettyContent { expected: &'static str },
  13. }
  14. pub struct DocumentEditorTest {
  15. pub sdk: FlowySDKTest,
  16. pub editor: Arc<AppFlowyDocumentEditor>,
  17. }
  18. impl DocumentEditorTest {
  19. pub async fn new() -> Self {
  20. let sdk = FlowySDKTest::new(true);
  21. let _ = sdk.init_user().await;
  22. let test = ViewTest::new_document_view(&sdk).await;
  23. let document_editor = sdk.document_manager.open_document_editor(&test.view.id).await.unwrap();
  24. let editor = match document_editor.as_any().downcast_ref::<Arc<AppFlowyDocumentEditor>>() {
  25. None => panic!(),
  26. Some(editor) => editor.clone(),
  27. };
  28. Self { sdk, editor }
  29. }
  30. pub async fn run_scripts(&self, scripts: Vec<EditScript>) {
  31. for script in scripts {
  32. self.run_script(script).await;
  33. }
  34. }
  35. async fn run_script(&self, script: EditScript) {
  36. match script {
  37. EditScript::InsertText { path, delta } => {
  38. let node_data = NodeDataBuilder::new("text").insert_body(Body::Delta(delta)).build();
  39. let operation = NodeOperation::Insert {
  40. path,
  41. nodes: vec![node_data],
  42. };
  43. self.editor
  44. .apply_transaction(Transaction::from_operations(vec![operation]))
  45. .await
  46. .unwrap();
  47. }
  48. EditScript::UpdateText { path, delta } => {
  49. let inverted = delta.invert_str("");
  50. let changeset = Changeset::Delta { delta, inverted };
  51. let operation = NodeOperation::Update { path, changeset };
  52. self.editor
  53. .apply_transaction(Transaction::from_operations(vec![operation]))
  54. .await
  55. .unwrap();
  56. }
  57. EditScript::Delete { path } => {
  58. let operation = NodeOperation::Delete { path, nodes: vec![] };
  59. self.editor
  60. .apply_transaction(Transaction::from_operations(vec![operation]))
  61. .await
  62. .unwrap();
  63. }
  64. EditScript::AssertContent { expected } => {
  65. //
  66. let content = self.editor.get_content(false).await.unwrap();
  67. assert_eq!(content, expected);
  68. }
  69. EditScript::AssertPrettyContent { expected } => {
  70. //
  71. let content = self.editor.get_content(true).await.unwrap();
  72. assert_eq!(content, expected);
  73. }
  74. }
  75. }
  76. }