mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Integrate appflowy editor (#1040)
This commit is contained in:
@ -3,7 +3,7 @@ use crate::editor::{TestBuilder, TestOp::*};
|
||||
use flowy_sync::client_document::{NewlineDocument, EmptyDocument};
|
||||
use lib_ot::core::{Interval, OperationTransform, NEW_LINE, WHITESPACE, OTString};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use lib_ot::text_delta::TextOperations;
|
||||
use lib_ot::text_delta::DeltaTextOperations;
|
||||
|
||||
#[test]
|
||||
fn attributes_bold_added() {
|
||||
@ -29,7 +29,7 @@ fn attributes_bold_added_and_invert_all() {
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}}]"#),
|
||||
Bold(0, Interval::new(0, 3), false),
|
||||
AssertDocJson(0, r#"[{"insert":"123"}]"#),
|
||||
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":false}}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<EmptyDocument>(ops);
|
||||
}
|
||||
@ -41,7 +41,7 @@ fn attributes_bold_added_and_invert_partial_suffix() {
|
||||
Bold(0, Interval::new(0, 4), true),
|
||||
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
|
||||
Bold(0, Interval::new(2, 4), false),
|
||||
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34"}]"#),
|
||||
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34","attributes":{"bold":false}}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<EmptyDocument>(ops);
|
||||
}
|
||||
@ -53,7 +53,7 @@ fn attributes_bold_added_and_invert_partial_suffix2() {
|
||||
Bold(0, Interval::new(0, 4), true),
|
||||
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
|
||||
Bold(0, Interval::new(2, 4), false),
|
||||
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34"}]"#),
|
||||
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":true}},{"insert":"34","attributes":{"bold":false}}]"#),
|
||||
Bold(0, Interval::new(2, 4), true),
|
||||
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
|
||||
];
|
||||
@ -95,7 +95,7 @@ fn attributes_bold_added_and_invert_partial_prefix() {
|
||||
Bold(0, Interval::new(0, 4), true),
|
||||
AssertDocJson(0, r#"[{"insert":"1234","attributes":{"bold":true}}]"#),
|
||||
Bold(0, Interval::new(0, 2), false),
|
||||
AssertDocJson(0, r#"[{"insert":"12"},{"insert":"34","attributes":{"bold":true}}]"#),
|
||||
AssertDocJson(0, r#"[{"insert":"12","attributes":{"bold":false}},{"insert":"34","attributes":{"bold":true}}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<EmptyDocument>(ops);
|
||||
}
|
||||
@ -762,12 +762,12 @@ fn attributes_preserve_list_format_on_merge() {
|
||||
|
||||
#[test]
|
||||
fn delta_compose() {
|
||||
let mut delta = TextOperations::from_json(r#"[{"insert":"\n"}]"#).unwrap();
|
||||
let mut delta = DeltaTextOperations::from_json(r#"[{"insert":"\n"}]"#).unwrap();
|
||||
let deltas = vec![
|
||||
TextOperations::from_json(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
|
||||
TextOperations::from_json(r#"[{"insert":"a"}]"#).unwrap(),
|
||||
TextOperations::from_json(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
|
||||
TextOperations::from_json(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
|
||||
DeltaTextOperations::from_json(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
|
||||
DeltaTextOperations::from_json(r#"[{"insert":"a"}]"#).unwrap(),
|
||||
DeltaTextOperations::from_json(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
|
||||
DeltaTextOperations::from_json(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
|
||||
];
|
||||
|
||||
for d in deltas {
|
||||
|
@ -8,7 +8,7 @@ use derive_more::Display;
|
||||
use flowy_sync::client_document::{ClientDocument, InitialDocument};
|
||||
use lib_ot::{
|
||||
core::*,
|
||||
text_delta::{BuildInTextAttribute, TextOperations},
|
||||
text_delta::{BuildInTextAttribute, DeltaTextOperations},
|
||||
};
|
||||
use rand::{prelude::*, Rng as WrappedRng};
|
||||
use std::{sync::Once, time::Duration};
|
||||
@ -81,8 +81,8 @@ pub enum TestOp {
|
||||
|
||||
pub struct TestBuilder {
|
||||
documents: Vec<ClientDocument>,
|
||||
deltas: Vec<Option<TextOperations>>,
|
||||
primes: Vec<Option<TextOperations>>,
|
||||
deltas: Vec<Option<DeltaTextOperations>>,
|
||||
primes: Vec<Option<DeltaTextOperations>>,
|
||||
}
|
||||
|
||||
impl TestBuilder {
|
||||
@ -226,20 +226,20 @@ impl TestBuilder {
|
||||
|
||||
TestOp::AssertDocJson(delta_i, expected) => {
|
||||
let delta_json = self.documents[*delta_i].get_operations_json();
|
||||
let expected_delta: TextOperations = serde_json::from_str(expected).unwrap();
|
||||
let target_delta: TextOperations = serde_json::from_str(&delta_json).unwrap();
|
||||
let expected_delta: DeltaTextOperations = serde_json::from_str(expected).unwrap();
|
||||
let target_delta: DeltaTextOperations = serde_json::from_str(&delta_json).unwrap();
|
||||
|
||||
if expected_delta != target_delta {
|
||||
log::error!("✅ expect: {}", expected,);
|
||||
log::error!("❌ receive: {}", delta_json);
|
||||
println!("✅ expect: {}", expected,);
|
||||
println!("❌ receive: {}", delta_json);
|
||||
}
|
||||
assert_eq!(target_delta, expected_delta);
|
||||
}
|
||||
|
||||
TestOp::AssertPrimeJson(doc_i, expected) => {
|
||||
let prime_json = self.primes[*doc_i].as_ref().unwrap().json_str();
|
||||
let expected_prime: TextOperations = serde_json::from_str(expected).unwrap();
|
||||
let target_prime: TextOperations = serde_json::from_str(&prime_json).unwrap();
|
||||
let expected_prime: DeltaTextOperations = serde_json::from_str(expected).unwrap();
|
||||
let target_prime: DeltaTextOperations = serde_json::from_str(&prime_json).unwrap();
|
||||
|
||||
if expected_prime != target_prime {
|
||||
log::error!("✅ expect prime: {}", expected,);
|
||||
@ -297,8 +297,8 @@ impl Rng {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn gen_delta(&mut self, s: &str) -> TextOperations {
|
||||
let mut delta = TextOperations::default();
|
||||
pub fn gen_delta(&mut self, s: &str) -> DeltaTextOperations {
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let s = OTString::from(s);
|
||||
loop {
|
||||
let left = s.utf16_len() - delta.utf16_base_len;
|
||||
|
@ -1,8 +1,8 @@
|
||||
#![allow(clippy::all)]
|
||||
use crate::editor::{Rng, TestBuilder, TestOp::*};
|
||||
use flowy_sync::client_document::{EmptyDocument, NewlineDocument};
|
||||
use lib_ot::text_delta::TextOperationBuilder;
|
||||
use lib_ot::{core::Interval, core::*, text_delta::TextOperations};
|
||||
use lib_ot::text_delta::DeltaTextOperationBuilder;
|
||||
use lib_ot::{core::Interval, core::*, text_delta::DeltaTextOperations};
|
||||
|
||||
#[test]
|
||||
fn attributes_insert_text() {
|
||||
@ -37,7 +37,7 @@ fn attributes_insert_text_at_middle() {
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_1() {
|
||||
let operations = OperationsBuilder::new().insert("123").insert("4").build();
|
||||
let delta = TextOperationBuilder::from_operations(operations);
|
||||
let delta = DeltaTextOperationBuilder::from_operations(operations);
|
||||
|
||||
let mut iterator = OperationIterator::from_interval(&delta, Interval::new(0, 4));
|
||||
assert_eq!(iterator.ops(), delta.ops);
|
||||
@ -45,7 +45,7 @@ fn delta_get_ops_in_interval_1() {
|
||||
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_2() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("123");
|
||||
let insert_b = DeltaOperation::insert("4");
|
||||
let insert_c = DeltaOperation::insert("5");
|
||||
@ -89,7 +89,7 @@ fn delta_get_ops_in_interval_2() {
|
||||
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_3() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("123456");
|
||||
delta.add(insert_a.clone());
|
||||
assert_eq!(
|
||||
@ -100,7 +100,7 @@ fn delta_get_ops_in_interval_3() {
|
||||
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_4() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("12");
|
||||
let insert_b = DeltaOperation::insert("34");
|
||||
let insert_c = DeltaOperation::insert("56");
|
||||
@ -130,7 +130,7 @@ fn delta_get_ops_in_interval_4() {
|
||||
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_5() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("123456");
|
||||
let insert_b = DeltaOperation::insert("789");
|
||||
delta.ops.push(insert_a.clone());
|
||||
@ -148,7 +148,7 @@ fn delta_get_ops_in_interval_5() {
|
||||
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_6() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("12345678");
|
||||
delta.add(insert_a.clone());
|
||||
assert_eq!(
|
||||
@ -159,7 +159,7 @@ fn delta_get_ops_in_interval_6() {
|
||||
|
||||
#[test]
|
||||
fn delta_get_ops_in_interval_7() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("12345");
|
||||
let retain_a = DeltaOperation::retain(3);
|
||||
|
||||
@ -179,7 +179,7 @@ fn delta_get_ops_in_interval_7() {
|
||||
|
||||
#[test]
|
||||
fn delta_op_seek() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let insert_a = DeltaOperation::insert("12345");
|
||||
let retain_a = DeltaOperation::retain(3);
|
||||
delta.add(insert_a.clone());
|
||||
@ -191,7 +191,7 @@ fn delta_op_seek() {
|
||||
|
||||
#[test]
|
||||
fn delta_utf16_code_unit_seek() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("12345"));
|
||||
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
@ -201,7 +201,7 @@ fn delta_utf16_code_unit_seek() {
|
||||
|
||||
#[test]
|
||||
fn delta_utf16_code_unit_seek_with_attributes() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert("bold", true)
|
||||
.insert("italic", true)
|
||||
@ -221,7 +221,7 @@ fn delta_utf16_code_unit_seek_with_attributes() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_len() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("12345"));
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
assert_eq!(iter.next_op_with_len(2).unwrap(), DeltaOperation::insert("12"));
|
||||
@ -232,7 +232,7 @@ fn delta_next_op_len() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_len_with_chinese() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("你好"));
|
||||
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
@ -242,7 +242,7 @@ fn delta_next_op_len_with_chinese() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_len_with_english() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("ab"));
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
assert_eq!(iter.next_op_len().unwrap(), 2);
|
||||
@ -251,7 +251,7 @@ fn delta_next_op_len_with_english() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_len_after_seek() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("12345"));
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
assert_eq!(iter.next_op_len().unwrap(), 5);
|
||||
@ -264,7 +264,7 @@ fn delta_next_op_len_after_seek() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_len_none() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("12345"));
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
|
||||
@ -275,7 +275,7 @@ fn delta_next_op_len_none() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_with_len_zero() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("12345"));
|
||||
let mut iter = OperationIterator::new(&delta);
|
||||
assert_eq!(iter.next_op_with_len(0), None,);
|
||||
@ -284,7 +284,7 @@ fn delta_next_op_with_len_zero() {
|
||||
|
||||
#[test]
|
||||
fn delta_next_op_with_len_cross_op_return_last() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("12345"));
|
||||
delta.add(DeltaOperation::retain(1));
|
||||
delta.add(DeltaOperation::insert("678"));
|
||||
@ -297,7 +297,7 @@ fn delta_next_op_with_len_cross_op_return_last() {
|
||||
|
||||
#[test]
|
||||
fn lengths() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
assert_eq!(delta.utf16_base_len, 0);
|
||||
assert_eq!(delta.utf16_target_len, 0);
|
||||
delta.retain(5, AttributeHashMap::default());
|
||||
@ -315,7 +315,7 @@ fn lengths() {
|
||||
}
|
||||
#[test]
|
||||
fn sequence() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.retain(5, AttributeHashMap::default());
|
||||
delta.retain(0, AttributeHashMap::default());
|
||||
delta.insert("appflowy", AttributeHashMap::default());
|
||||
@ -348,7 +348,7 @@ fn apply_test() {
|
||||
|
||||
#[test]
|
||||
fn base_len_test() {
|
||||
let mut delta_a = TextOperations::default();
|
||||
let mut delta_a = DeltaTextOperations::default();
|
||||
delta_a.insert("a", AttributeHashMap::default());
|
||||
delta_a.insert("b", AttributeHashMap::default());
|
||||
delta_a.insert("c", AttributeHashMap::default());
|
||||
@ -387,7 +387,7 @@ fn invert_test() {
|
||||
|
||||
#[test]
|
||||
fn empty_ops() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.retain(0, AttributeHashMap::default());
|
||||
delta.insert("", AttributeHashMap::default());
|
||||
delta.delete(0);
|
||||
@ -395,12 +395,12 @@ fn empty_ops() {
|
||||
}
|
||||
#[test]
|
||||
fn eq() {
|
||||
let mut delta_a = TextOperations::default();
|
||||
let mut delta_a = DeltaTextOperations::default();
|
||||
delta_a.delete(1);
|
||||
delta_a.insert("lo", AttributeHashMap::default());
|
||||
delta_a.retain(2, AttributeHashMap::default());
|
||||
delta_a.retain(3, AttributeHashMap::default());
|
||||
let mut delta_b = TextOperations::default();
|
||||
let mut delta_b = DeltaTextOperations::default();
|
||||
delta_b.delete(1);
|
||||
delta_b.insert("l", AttributeHashMap::default());
|
||||
delta_b.insert("o", AttributeHashMap::default());
|
||||
@ -412,7 +412,7 @@ fn eq() {
|
||||
}
|
||||
#[test]
|
||||
fn ops_merging() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
assert_eq!(delta.ops.len(), 0);
|
||||
delta.retain(2, AttributeHashMap::default());
|
||||
assert_eq!(delta.ops.len(), 1);
|
||||
@ -436,7 +436,7 @@ fn ops_merging() {
|
||||
|
||||
#[test]
|
||||
fn is_noop() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
assert!(delta.is_noop());
|
||||
delta.retain(5, AttributeHashMap::default());
|
||||
assert!(delta.is_noop());
|
||||
@ -484,13 +484,13 @@ fn transform_random_delta() {
|
||||
|
||||
#[test]
|
||||
fn transform_with_two_delta() {
|
||||
let mut a = TextOperations::default();
|
||||
let mut a = DeltaTextOperations::default();
|
||||
let mut a_s = String::new();
|
||||
a.insert("123", AttributeBuilder::new().insert("bold", true).build());
|
||||
a_s = a.apply(&a_s).unwrap();
|
||||
assert_eq!(&a_s, "123");
|
||||
|
||||
let mut b = TextOperations::default();
|
||||
let mut b = DeltaTextOperations::default();
|
||||
let mut b_s = String::new();
|
||||
b.insert("456", AttributeHashMap::default());
|
||||
b_s = b.apply(&b_s).unwrap();
|
||||
@ -580,10 +580,10 @@ fn transform_two_conflict_non_seq_delta() {
|
||||
|
||||
#[test]
|
||||
fn delta_invert_no_attribute_delta() {
|
||||
let mut delta = TextOperations::default();
|
||||
let mut delta = DeltaTextOperations::default();
|
||||
delta.add(DeltaOperation::insert("123"));
|
||||
|
||||
let mut change = TextOperations::default();
|
||||
let mut change = DeltaTextOperations::default();
|
||||
change.add(DeltaOperation::retain(3));
|
||||
change.add(DeltaOperation::insert("456"));
|
||||
let undo = change.invert(&delta);
|
||||
|
@ -1,8 +1,8 @@
|
||||
use flowy_sync::client_document::{ClientDocument, EmptyDocument};
|
||||
use lib_ot::text_delta::TextOperation;
|
||||
use lib_ot::text_delta::DeltaTextOperation;
|
||||
use lib_ot::{
|
||||
core::*,
|
||||
text_delta::{BuildInTextAttribute, TextOperations},
|
||||
text_delta::{BuildInTextAttribute, DeltaTextOperations},
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -15,7 +15,7 @@ fn operation_insert_serialize_test() {
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
eprintln!("{}", json);
|
||||
|
||||
let insert_op: TextOperation = serde_json::from_str(&json).unwrap();
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
}
|
||||
|
||||
@ -24,15 +24,15 @@ fn operation_retain_serialize_test() {
|
||||
let operation = DeltaOperation::Retain(12.into());
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
eprintln!("{}", json);
|
||||
let insert_op: TextOperation = serde_json::from_str(&json).unwrap();
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operation_delete_serialize_test() {
|
||||
let operation = TextOperation::Delete(2);
|
||||
let operation = DeltaTextOperation::Delete(2);
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
let insert_op: TextOperation = serde_json::from_str(&json).unwrap();
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ fn delta_deserialize_test() {
|
||||
{"retain":2,"attributes":{"italic":true,"bold":true}},
|
||||
{"retain":2,"attributes":{"italic":true,"bold":true}}
|
||||
]"#;
|
||||
let delta = TextOperations::from_json(json).unwrap();
|
||||
let delta = DeltaTextOperations::from_json(json).unwrap();
|
||||
eprintln!("{}", delta);
|
||||
}
|
||||
|
||||
@ -86,12 +86,12 @@ fn delta_deserialize_null_test() {
|
||||
let json = r#"[
|
||||
{"retain":7,"attributes":{"bold":null}}
|
||||
]"#;
|
||||
let delta1 = TextOperations::from_json(json).unwrap();
|
||||
let delta1 = DeltaTextOperations::from_json(json).unwrap();
|
||||
|
||||
let mut attribute = BuildInTextAttribute::Bold(true);
|
||||
attribute.remove_value();
|
||||
|
||||
let delta2 = OperationBuilder::new()
|
||||
let delta2 = DeltaOperationBuilder::new()
|
||||
.retain_with_attributes(7, attribute.into())
|
||||
.build();
|
||||
|
||||
|
@ -0,0 +1,24 @@
|
||||
use crate::new_document::script::DocumentEditorTest;
|
||||
use crate::new_document::script::EditScript::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_insert_h1_style_test() {
|
||||
let scripts = vec![
|
||||
ComposeTransactionStr {
|
||||
transaction: r#"{"operations":[{"op":"update_text","path":[0,0],"delta":[{"insert":"/"}],"inverted":[{"delete":1}]}],"after_selection":{"start":{"path":[0,0],"offset":1},"end":{"path":[0,0],"offset":1}},"before_selection":{"start":{"path":[0,0],"offset":0},"end":{"path":[0,0],"offset":0}}}"#,
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text","delta":[{"insert":"/"}]}]}}"#,
|
||||
},
|
||||
ComposeTransactionStr {
|
||||
transaction: r#"{"operations":[{"op":"update_text","path":[0,0],"delta":[{"delete":1}],"inverted":[{"insert":"/"}]}],"after_selection":{"start":{"path":[0,0],"offset":0},"end":{"path":[0,0],"offset":0}},"before_selection":{"start":{"path":[0,0],"offset":1},"end":{"path":[0,0],"offset":1}}}"#,
|
||||
},
|
||||
ComposeTransactionStr {
|
||||
transaction: r#"{"operations":[{"op":"update","path":[0,0],"attributes":{"subtype":"heading","heading":"h1"},"oldAttributes":{"subtype":null,"heading":null}}],"after_selection":{"start":{"path":[0,0],"offset":0},"end":{"path":[0,0],"offset":0}},"before_selection":{"start":{"path":[0,0],"offset":0},"end":{"path":[0,0],"offset":0}}}"#,
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text","attributes":{"subtype":"heading","heading":"h1"}}]}}"#,
|
||||
},
|
||||
];
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
mod document_compose_test;
|
||||
mod script;
|
||||
mod test;
|
||||
|
@ -1,17 +1,37 @@
|
||||
use flowy_document::editor::AppFlowyDocumentEditor;
|
||||
use flowy_document::editor::{AppFlowyDocumentEditor, Document, DocumentTransaction};
|
||||
|
||||
use flowy_document::entities::DocumentVersionPB;
|
||||
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 lib_ot::text_delta::DeltaTextOperations;
|
||||
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 },
|
||||
InsertText {
|
||||
path: Path,
|
||||
delta: DeltaTextOperations,
|
||||
},
|
||||
UpdateText {
|
||||
path: Path,
|
||||
delta: DeltaTextOperations,
|
||||
},
|
||||
#[allow(dead_code)]
|
||||
ComposeTransaction {
|
||||
transaction: Transaction,
|
||||
},
|
||||
ComposeTransactionStr {
|
||||
transaction: &'static str,
|
||||
},
|
||||
Delete {
|
||||
path: Path,
|
||||
},
|
||||
AssertContent {
|
||||
expected: &'static str,
|
||||
},
|
||||
AssertPrettyContent {
|
||||
expected: &'static str,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct DocumentEditorTest {
|
||||
@ -21,7 +41,8 @@ pub struct DocumentEditorTest {
|
||||
|
||||
impl DocumentEditorTest {
|
||||
pub async fn new() -> Self {
|
||||
let sdk = FlowySDKTest::new(true);
|
||||
let version = DocumentVersionPB::V1;
|
||||
let sdk = FlowySDKTest::new(version.clone());
|
||||
let _ = sdk.init_user().await;
|
||||
|
||||
let test = ViewTest::new_document_view(&sdk).await;
|
||||
@ -62,6 +83,14 @@ impl DocumentEditorTest {
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
EditScript::ComposeTransaction { transaction } => {
|
||||
self.editor.apply_transaction(transaction).await.unwrap();
|
||||
}
|
||||
EditScript::ComposeTransactionStr { transaction } => {
|
||||
let document_transaction = serde_json::from_str::<DocumentTransaction>(transaction).unwrap();
|
||||
let transaction: Transaction = document_transaction.into();
|
||||
self.editor.apply_transaction(transaction).await.unwrap();
|
||||
}
|
||||
EditScript::Delete { path } => {
|
||||
let operation = NodeOperation::Delete { path, nodes: vec![] };
|
||||
self.editor
|
||||
@ -72,6 +101,9 @@ impl DocumentEditorTest {
|
||||
EditScript::AssertContent { expected } => {
|
||||
//
|
||||
let content = self.editor.get_content(false).await.unwrap();
|
||||
let expected_document: Document = serde_json::from_str(expected).unwrap();
|
||||
let expected = serde_json::to_string(&expected_document).unwrap();
|
||||
|
||||
assert_eq!(content, expected);
|
||||
}
|
||||
EditScript::AssertPrettyContent { expected } => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::new_document::script::DocumentEditorTest;
|
||||
use crate::new_document::script::EditScript::*;
|
||||
|
||||
use lib_ot::text_delta::TextOperationBuilder;
|
||||
use lib_ot::text_delta::DeltaTextOperationBuilder;
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_initialize_test() {
|
||||
@ -13,7 +13,7 @@ async fn document_initialize_test() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_insert_text_test() {
|
||||
let delta = TextOperationBuilder::new().insert("Hello world").build();
|
||||
let delta = DeltaTextOperationBuilder::new().insert("Hello world").build();
|
||||
let expected = r#"{
|
||||
"document": {
|
||||
"type": "editor",
|
||||
@ -49,7 +49,7 @@ async fn document_update_text_test() {
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: TextOperationBuilder::new().insert(&hello_world).build(),
|
||||
delta: DeltaTextOperationBuilder::new().insert(&hello_world).build(),
|
||||
},
|
||||
AssertPrettyContent {
|
||||
expected: r#"{
|
||||
@ -75,7 +75,7 @@ async fn document_update_text_test() {
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: TextOperationBuilder::new()
|
||||
delta: DeltaTextOperationBuilder::new()
|
||||
.retain(hello_world.len())
|
||||
.insert(", AppFlowy")
|
||||
.build(),
|
||||
@ -122,11 +122,11 @@ async fn document_delete_text_test() {
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: TextOperationBuilder::new().insert(&hello_world).build(),
|
||||
delta: DeltaTextOperationBuilder::new().insert(&hello_world).build(),
|
||||
},
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: TextOperationBuilder::new().retain(5).delete(6).build(),
|
||||
delta: DeltaTextOperationBuilder::new().retain(5).delete(6).build(),
|
||||
},
|
||||
AssertPrettyContent { expected },
|
||||
];
|
||||
@ -139,7 +139,7 @@ async fn document_delete_node_test() {
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: TextOperationBuilder::new().insert("Hello world").build(),
|
||||
delta: DeltaTextOperationBuilder::new().insert("Hello world").build(),
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text","delta":[{"insert":"Hello world"}]}]}}"#,
|
||||
|
@ -2,7 +2,7 @@ use flowy_document::old_editor::editor::DeltaDocumentEditor;
|
||||
use flowy_document::TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS;
|
||||
use flowy_revision::disk::RevisionState;
|
||||
use flowy_test::{helper::ViewTest, FlowySDKTest};
|
||||
use lib_ot::{core::Interval, text_delta::TextOperations};
|
||||
use lib_ot::{core::Interval, text_delta::DeltaTextOperations};
|
||||
use std::sync::Arc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
@ -75,7 +75,7 @@ impl DeltaDocumentEditorTest {
|
||||
assert_eq!(next_revision.rev_id, rev_id.unwrap());
|
||||
}
|
||||
EditorScript::AssertJson(expected) => {
|
||||
let expected_delta: TextOperations = serde_json::from_str(expected).unwrap();
|
||||
let expected_delta: DeltaTextOperations = serde_json::from_str(expected).unwrap();
|
||||
let delta = self.editor.document_operations().await.unwrap();
|
||||
if expected_delta != delta {
|
||||
eprintln!("✅ expect: {}", expected,);
|
||||
|
Reference in New Issue
Block a user