AppFlowy/frontend/rust-lib/flowy-document/tests/new_document/script.rs
Nathan.fooo f1a5726fcb
Feat: add appflowy editor in backend (#1320)
* chore: remove update attributes

* chore: format code

* chore: extension for transaction

* refactor: add document editor trait

* chore: add appflowy_document editor

* chore: add document serde

* chore: add new document editor

* chore: add tests

* chore: add more test

* chore: add test

Co-authored-by: nathan <nathan@appflowy.io>
2022-10-20 11:35:11 +08:00

85 lines
3.0 KiB
Rust

use flowy_document::editor::AppFlowyDocumentEditor;
use flowy_test::helper::ViewTest;
use flowy_test::FlowySDKTest;
use lib_ot::core::{Body, Changeset, NodeDataBuilder, NodeOperation, Path, Transaction};
use lib_ot::text_delta::TextOperations;
use std::sync::Arc;
pub enum EditScript {
InsertText { path: Path, delta: TextOperations },
UpdateText { path: Path, delta: TextOperations },
Delete { path: Path },
AssertContent { expected: &'static str },
AssertPrettyContent { expected: &'static str },
}
pub struct DocumentEditorTest {
pub sdk: FlowySDKTest,
pub editor: Arc<AppFlowyDocumentEditor>,
}
impl DocumentEditorTest {
pub async fn new() -> Self {
let sdk = FlowySDKTest::new(true);
let _ = sdk.init_user().await;
let test = ViewTest::new_document_view(&sdk).await;
let document_editor = sdk.document_manager.open_document_editor(&test.view.id).await.unwrap();
let editor = match document_editor.as_any().downcast_ref::<Arc<AppFlowyDocumentEditor>>() {
None => panic!(),
Some(editor) => editor.clone(),
};
Self { sdk, editor }
}
pub async fn run_scripts(&self, scripts: Vec<EditScript>) {
for script in scripts {
self.run_script(script).await;
}
}
async fn run_script(&self, script: EditScript) {
match script {
EditScript::InsertText { path, delta } => {
let node_data = NodeDataBuilder::new("text").insert_body(Body::Delta(delta)).build();
let operation = NodeOperation::Insert {
path,
nodes: vec![node_data],
};
self.editor
.apply_transaction(Transaction::from_operations(vec![operation]))
.await
.unwrap();
}
EditScript::UpdateText { path, delta } => {
let inverted = delta.invert_str("");
let changeset = Changeset::Delta { delta, inverted };
let operation = NodeOperation::Update { path, changeset };
self.editor
.apply_transaction(Transaction::from_operations(vec![operation]))
.await
.unwrap();
}
EditScript::Delete { path } => {
let operation = NodeOperation::Delete { path, nodes: vec![] };
self.editor
.apply_transaction(Transaction::from_operations(vec![operation]))
.await
.unwrap();
}
EditScript::AssertContent { expected } => {
//
let content = self.editor.get_content(false).await.unwrap();
assert_eq!(content, expected);
}
EditScript::AssertPrettyContent { expected } => {
//
let content = self.editor.get_content(true).await.unwrap();
assert_eq!(content, expected);
}
}
}
}