mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: run rustfmt with custom defined fmt configuration (#1848)
* chore: update rustfmt * chore: apply rustfmt format
This commit is contained in:
@ -7,324 +7,340 @@ mod undo_redo_test;
|
||||
use derive_more::Display;
|
||||
use flowy_client_sync::client_document::{ClientDocument, InitialDocument};
|
||||
use lib_ot::{
|
||||
core::*,
|
||||
text_delta::{BuildInTextAttribute, DeltaTextOperations},
|
||||
core::*,
|
||||
text_delta::{BuildInTextAttribute, DeltaTextOperations},
|
||||
};
|
||||
use rand::{prelude::*, Rng as WrappedRng};
|
||||
use std::{sync::Once, time::Duration};
|
||||
|
||||
#[derive(Clone, Debug, Display)]
|
||||
pub enum TestOp {
|
||||
#[display(fmt = "Insert")]
|
||||
Insert(usize, &'static str, usize),
|
||||
#[display(fmt = "Insert")]
|
||||
Insert(usize, &'static str, usize),
|
||||
|
||||
// delta_i, s, start, length,
|
||||
#[display(fmt = "InsertBold")]
|
||||
InsertBold(usize, &'static str, Interval),
|
||||
// delta_i, s, start, length,
|
||||
#[display(fmt = "InsertBold")]
|
||||
InsertBold(usize, &'static str, Interval),
|
||||
|
||||
// delta_i, start, length, enable
|
||||
#[display(fmt = "Bold")]
|
||||
Bold(usize, Interval, bool),
|
||||
// delta_i, start, length, enable
|
||||
#[display(fmt = "Bold")]
|
||||
Bold(usize, Interval, bool),
|
||||
|
||||
#[display(fmt = "Delete")]
|
||||
Delete(usize, Interval),
|
||||
#[display(fmt = "Delete")]
|
||||
Delete(usize, Interval),
|
||||
|
||||
#[display(fmt = "Replace")]
|
||||
Replace(usize, Interval, &'static str),
|
||||
#[display(fmt = "Replace")]
|
||||
Replace(usize, Interval, &'static str),
|
||||
|
||||
#[display(fmt = "Italic")]
|
||||
Italic(usize, Interval, bool),
|
||||
#[display(fmt = "Italic")]
|
||||
Italic(usize, Interval, bool),
|
||||
|
||||
#[display(fmt = "Header")]
|
||||
Header(usize, Interval, usize),
|
||||
#[display(fmt = "Header")]
|
||||
Header(usize, Interval, usize),
|
||||
|
||||
#[display(fmt = "Link")]
|
||||
Link(usize, Interval, &'static str),
|
||||
#[display(fmt = "Link")]
|
||||
Link(usize, Interval, &'static str),
|
||||
|
||||
#[display(fmt = "Bullet")]
|
||||
Bullet(usize, Interval, bool),
|
||||
#[display(fmt = "Bullet")]
|
||||
Bullet(usize, Interval, bool),
|
||||
|
||||
#[display(fmt = "Transform")]
|
||||
Transform(usize, usize),
|
||||
#[display(fmt = "Transform")]
|
||||
Transform(usize, usize),
|
||||
|
||||
#[display(fmt = "TransformPrime")]
|
||||
TransformPrime(usize, usize),
|
||||
#[display(fmt = "TransformPrime")]
|
||||
TransformPrime(usize, usize),
|
||||
|
||||
// invert the delta_a base on the delta_b
|
||||
#[display(fmt = "Invert")]
|
||||
Invert(usize, usize),
|
||||
// invert the delta_a base on the delta_b
|
||||
#[display(fmt = "Invert")]
|
||||
Invert(usize, usize),
|
||||
|
||||
#[display(fmt = "Undo")]
|
||||
Undo(usize),
|
||||
#[display(fmt = "Undo")]
|
||||
Undo(usize),
|
||||
|
||||
#[display(fmt = "Redo")]
|
||||
Redo(usize),
|
||||
#[display(fmt = "Redo")]
|
||||
Redo(usize),
|
||||
|
||||
#[display(fmt = "Wait")]
|
||||
Wait(usize),
|
||||
#[display(fmt = "Wait")]
|
||||
Wait(usize),
|
||||
|
||||
#[display(fmt = "AssertStr")]
|
||||
AssertStr(usize, &'static str),
|
||||
#[display(fmt = "AssertStr")]
|
||||
AssertStr(usize, &'static str),
|
||||
|
||||
#[display(fmt = "AssertDocJson")]
|
||||
AssertDocJson(usize, &'static str),
|
||||
#[display(fmt = "AssertDocJson")]
|
||||
AssertDocJson(usize, &'static str),
|
||||
|
||||
#[display(fmt = "AssertPrimeJson")]
|
||||
AssertPrimeJson(usize, &'static str),
|
||||
#[display(fmt = "AssertPrimeJson")]
|
||||
AssertPrimeJson(usize, &'static str),
|
||||
|
||||
#[display(fmt = "DocComposeDelta")]
|
||||
DocComposeDelta(usize, usize),
|
||||
#[display(fmt = "DocComposeDelta")]
|
||||
DocComposeDelta(usize, usize),
|
||||
|
||||
#[display(fmt = "ApplyPrimeDelta")]
|
||||
DocComposePrime(usize, usize),
|
||||
#[display(fmt = "ApplyPrimeDelta")]
|
||||
DocComposePrime(usize, usize),
|
||||
}
|
||||
|
||||
pub struct TestBuilder {
|
||||
documents: Vec<ClientDocument>,
|
||||
deltas: Vec<Option<DeltaTextOperations>>,
|
||||
primes: Vec<Option<DeltaTextOperations>>,
|
||||
documents: Vec<ClientDocument>,
|
||||
deltas: Vec<Option<DeltaTextOperations>>,
|
||||
primes: Vec<Option<DeltaTextOperations>>,
|
||||
}
|
||||
|
||||
impl TestBuilder {
|
||||
pub fn new() -> Self {
|
||||
static INIT: Once = Once::new();
|
||||
INIT.call_once(|| {
|
||||
let _ = color_eyre::install();
|
||||
// let subscriber = FmtSubscriber::builder().with_max_level(Level::INFO).finish();
|
||||
// tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||
});
|
||||
pub fn new() -> Self {
|
||||
static INIT: Once = Once::new();
|
||||
INIT.call_once(|| {
|
||||
let _ = color_eyre::install();
|
||||
// let subscriber = FmtSubscriber::builder().with_max_level(Level::INFO).finish();
|
||||
// tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
|
||||
});
|
||||
|
||||
Self {
|
||||
documents: vec![],
|
||||
deltas: vec![],
|
||||
primes: vec![],
|
||||
}
|
||||
Self {
|
||||
documents: vec![],
|
||||
deltas: vec![],
|
||||
primes: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn run_op(&mut self, op: &TestOp) {
|
||||
tracing::trace!("***************** 😈{} *******************", &op);
|
||||
match op {
|
||||
TestOp::Insert(delta_i, s, index) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let delta = document.insert(*index, s).unwrap();
|
||||
tracing::debug!("Insert delta: {}", delta.json_str());
|
||||
fn run_op(&mut self, op: &TestOp) {
|
||||
tracing::trace!("***************** 😈{} *******************", &op);
|
||||
match op {
|
||||
TestOp::Insert(delta_i, s, index) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let delta = document.insert(*index, s).unwrap();
|
||||
tracing::debug!("Insert delta: {}", delta.json_str());
|
||||
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Delete(delta_i, iv) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let delta = document.replace(*iv, "").unwrap();
|
||||
tracing::trace!("Delete delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Replace(delta_i, iv, s) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let delta = document.replace(*iv, s).unwrap();
|
||||
tracing::trace!("Replace delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::InsertBold(delta_i, s, iv) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
document.insert(iv.start, s).unwrap();
|
||||
document.format(*iv, BuildInTextAttribute::Bold(true)).unwrap();
|
||||
}
|
||||
TestOp::Bold(delta_i, iv, enable) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Bold(*enable);
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Bold delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Italic(delta_i, iv, enable) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = match *enable {
|
||||
true => BuildInTextAttribute::Italic(true),
|
||||
false => BuildInTextAttribute::Italic(false),
|
||||
};
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Italic delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Header(delta_i, iv, level) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Header(*level);
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Header delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Link(delta_i, iv, link) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Link(link.to_owned());
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Link delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Bullet(delta_i, iv, enable) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Bullet(*enable);
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::debug!("Bullet delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Delete(delta_i, iv) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let delta = document.replace(*iv, "").unwrap();
|
||||
tracing::trace!("Delete delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Replace(delta_i, iv, s) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let delta = document.replace(*iv, s).unwrap();
|
||||
tracing::trace!("Replace delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::InsertBold(delta_i, s, iv) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
document.insert(iv.start, s).unwrap();
|
||||
document
|
||||
.format(*iv, BuildInTextAttribute::Bold(true))
|
||||
.unwrap();
|
||||
},
|
||||
TestOp::Bold(delta_i, iv, enable) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Bold(*enable);
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Bold delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Italic(delta_i, iv, enable) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = match *enable {
|
||||
true => BuildInTextAttribute::Italic(true),
|
||||
false => BuildInTextAttribute::Italic(false),
|
||||
};
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Italic delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Header(delta_i, iv, level) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Header(*level);
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Header delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Link(delta_i, iv, link) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Link(link.to_owned());
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::trace!("Link delta: {}", delta.json_str());
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Bullet(delta_i, iv, enable) => {
|
||||
let document = &mut self.documents[*delta_i];
|
||||
let attribute = BuildInTextAttribute::Bullet(*enable);
|
||||
let delta = document.format(*iv, attribute).unwrap();
|
||||
tracing::debug!("Bullet delta: {}", delta.json_str());
|
||||
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
}
|
||||
TestOp::Transform(delta_a_i, delta_b_i) => {
|
||||
let (a_prime, b_prime) = self.documents[*delta_a_i]
|
||||
.get_operations()
|
||||
.transform(self.documents[*delta_b_i].get_operations())
|
||||
.unwrap();
|
||||
tracing::trace!("a:{:?},b:{:?}", a_prime, b_prime);
|
||||
self.deltas.insert(*delta_i, Some(delta));
|
||||
},
|
||||
TestOp::Transform(delta_a_i, delta_b_i) => {
|
||||
let (a_prime, b_prime) = self.documents[*delta_a_i]
|
||||
.get_operations()
|
||||
.transform(self.documents[*delta_b_i].get_operations())
|
||||
.unwrap();
|
||||
tracing::trace!("a:{:?},b:{:?}", a_prime, b_prime);
|
||||
|
||||
let data_left = self.documents[*delta_a_i].get_operations().compose(&b_prime).unwrap();
|
||||
let data_right = self.documents[*delta_b_i].get_operations().compose(&a_prime).unwrap();
|
||||
let data_left = self.documents[*delta_a_i]
|
||||
.get_operations()
|
||||
.compose(&b_prime)
|
||||
.unwrap();
|
||||
let data_right = self.documents[*delta_b_i]
|
||||
.get_operations()
|
||||
.compose(&a_prime)
|
||||
.unwrap();
|
||||
|
||||
self.documents[*delta_a_i].set_operations(data_left);
|
||||
self.documents[*delta_b_i].set_operations(data_right);
|
||||
}
|
||||
TestOp::TransformPrime(a_doc_index, b_doc_index) => {
|
||||
let (prime_left, prime_right) = self.documents[*a_doc_index]
|
||||
.get_operations()
|
||||
.transform(self.documents[*b_doc_index].get_operations())
|
||||
.unwrap();
|
||||
self.documents[*delta_a_i].set_operations(data_left);
|
||||
self.documents[*delta_b_i].set_operations(data_right);
|
||||
},
|
||||
TestOp::TransformPrime(a_doc_index, b_doc_index) => {
|
||||
let (prime_left, prime_right) = self.documents[*a_doc_index]
|
||||
.get_operations()
|
||||
.transform(self.documents[*b_doc_index].get_operations())
|
||||
.unwrap();
|
||||
|
||||
self.primes.insert(*a_doc_index, Some(prime_left));
|
||||
self.primes.insert(*b_doc_index, Some(prime_right));
|
||||
}
|
||||
TestOp::Invert(delta_a_i, delta_b_i) => {
|
||||
let delta_a = &self.documents[*delta_a_i].get_operations();
|
||||
let delta_b = &self.documents[*delta_b_i].get_operations();
|
||||
tracing::debug!("Invert: ");
|
||||
tracing::debug!("a: {}", delta_a.json_str());
|
||||
tracing::debug!("b: {}", delta_b.json_str());
|
||||
self.primes.insert(*a_doc_index, Some(prime_left));
|
||||
self.primes.insert(*b_doc_index, Some(prime_right));
|
||||
},
|
||||
TestOp::Invert(delta_a_i, delta_b_i) => {
|
||||
let delta_a = &self.documents[*delta_a_i].get_operations();
|
||||
let delta_b = &self.documents[*delta_b_i].get_operations();
|
||||
tracing::debug!("Invert: ");
|
||||
tracing::debug!("a: {}", delta_a.json_str());
|
||||
tracing::debug!("b: {}", delta_b.json_str());
|
||||
|
||||
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
||||
let undo = b_prime.invert(delta_a);
|
||||
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
||||
let undo = b_prime.invert(delta_a);
|
||||
|
||||
let new_delta = delta_a.compose(&b_prime).unwrap();
|
||||
tracing::debug!("new delta: {}", new_delta.json_str());
|
||||
tracing::debug!("undo delta: {}", undo.json_str());
|
||||
let new_delta = delta_a.compose(&b_prime).unwrap();
|
||||
tracing::debug!("new delta: {}", new_delta.json_str());
|
||||
tracing::debug!("undo delta: {}", undo.json_str());
|
||||
|
||||
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
||||
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
||||
|
||||
tracing::debug!("inverted delta a: {}", new_delta_after_undo.to_string());
|
||||
tracing::debug!("inverted delta a: {}", new_delta_after_undo.to_string());
|
||||
|
||||
assert_eq!(delta_a, &&new_delta_after_undo);
|
||||
assert_eq!(delta_a, &&new_delta_after_undo);
|
||||
|
||||
self.documents[*delta_a_i].set_operations(new_delta_after_undo);
|
||||
}
|
||||
TestOp::Undo(delta_i) => {
|
||||
self.documents[*delta_i].undo().unwrap();
|
||||
}
|
||||
TestOp::Redo(delta_i) => {
|
||||
self.documents[*delta_i].redo().unwrap();
|
||||
}
|
||||
TestOp::Wait(mills_sec) => {
|
||||
std::thread::sleep(Duration::from_millis(*mills_sec as u64));
|
||||
}
|
||||
TestOp::AssertStr(delta_i, expected) => {
|
||||
assert_eq!(&self.documents[*delta_i].to_content(), expected);
|
||||
}
|
||||
self.documents[*delta_a_i].set_operations(new_delta_after_undo);
|
||||
},
|
||||
TestOp::Undo(delta_i) => {
|
||||
self.documents[*delta_i].undo().unwrap();
|
||||
},
|
||||
TestOp::Redo(delta_i) => {
|
||||
self.documents[*delta_i].redo().unwrap();
|
||||
},
|
||||
TestOp::Wait(mills_sec) => {
|
||||
std::thread::sleep(Duration::from_millis(*mills_sec as u64));
|
||||
},
|
||||
TestOp::AssertStr(delta_i, expected) => {
|
||||
assert_eq!(&self.documents[*delta_i].to_content(), expected);
|
||||
},
|
||||
|
||||
TestOp::AssertDocJson(delta_i, expected) => {
|
||||
let delta_json = self.documents[*delta_i].get_operations_json();
|
||||
let expected_delta: DeltaTextOperations = serde_json::from_str(expected).unwrap();
|
||||
let target_delta: DeltaTextOperations = serde_json::from_str(&delta_json).unwrap();
|
||||
TestOp::AssertDocJson(delta_i, expected) => {
|
||||
let delta_json = self.documents[*delta_i].get_operations_json();
|
||||
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 {
|
||||
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: DeltaTextOperations = serde_json::from_str(expected).unwrap();
|
||||
let target_prime: DeltaTextOperations = serde_json::from_str(&prime_json).unwrap();
|
||||
|
||||
if expected_prime != target_prime {
|
||||
tracing::error!("✅ expect prime: {}", expected,);
|
||||
tracing::error!("❌ receive prime: {}", prime_json);
|
||||
}
|
||||
assert_eq!(target_prime, expected_prime);
|
||||
}
|
||||
TestOp::DocComposeDelta(doc_index, delta_i) => {
|
||||
let delta = self.deltas.get(*delta_i).unwrap().as_ref().unwrap();
|
||||
self.documents[*doc_index].compose_operations(delta.clone()).unwrap();
|
||||
}
|
||||
TestOp::DocComposePrime(doc_index, prime_i) => {
|
||||
let delta = self
|
||||
.primes
|
||||
.get(*prime_i)
|
||||
.expect("Must call TransformPrime first")
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
let new_delta = self.documents[*doc_index].get_operations().compose(delta).unwrap();
|
||||
self.documents[*doc_index].set_operations(new_delta);
|
||||
}
|
||||
if expected_delta != target_delta {
|
||||
println!("✅ expect: {}", expected,);
|
||||
println!("❌ receive: {}", delta_json);
|
||||
}
|
||||
}
|
||||
assert_eq!(target_delta, expected_delta);
|
||||
},
|
||||
|
||||
pub fn run_scripts<C: InitialDocument>(mut self, scripts: Vec<TestOp>) {
|
||||
self.documents = vec![ClientDocument::new::<C>(), ClientDocument::new::<C>()];
|
||||
self.primes = vec![None, None];
|
||||
self.deltas = vec![None, None];
|
||||
for (_i, op) in scripts.iter().enumerate() {
|
||||
self.run_op(op);
|
||||
TestOp::AssertPrimeJson(doc_i, expected) => {
|
||||
let prime_json = self.primes[*doc_i].as_ref().unwrap().json_str();
|
||||
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 {
|
||||
tracing::error!("✅ expect prime: {}", expected,);
|
||||
tracing::error!("❌ receive prime: {}", prime_json);
|
||||
}
|
||||
assert_eq!(target_prime, expected_prime);
|
||||
},
|
||||
TestOp::DocComposeDelta(doc_index, delta_i) => {
|
||||
let delta = self.deltas.get(*delta_i).unwrap().as_ref().unwrap();
|
||||
self.documents[*doc_index]
|
||||
.compose_operations(delta.clone())
|
||||
.unwrap();
|
||||
},
|
||||
TestOp::DocComposePrime(doc_index, prime_i) => {
|
||||
let delta = self
|
||||
.primes
|
||||
.get(*prime_i)
|
||||
.expect("Must call TransformPrime first")
|
||||
.as_ref()
|
||||
.unwrap();
|
||||
let new_delta = self.documents[*doc_index]
|
||||
.get_operations()
|
||||
.compose(delta)
|
||||
.unwrap();
|
||||
self.documents[*doc_index].set_operations(new_delta);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_scripts<C: InitialDocument>(mut self, scripts: Vec<TestOp>) {
|
||||
self.documents = vec![ClientDocument::new::<C>(), ClientDocument::new::<C>()];
|
||||
self.primes = vec![None, None];
|
||||
self.deltas = vec![None, None];
|
||||
for (_i, op) in scripts.iter().enumerate() {
|
||||
self.run_op(op);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Rng(StdRng);
|
||||
|
||||
impl Default for Rng {
|
||||
fn default() -> Self {
|
||||
Rng(StdRng::from_rng(thread_rng()).unwrap())
|
||||
}
|
||||
fn default() -> Self {
|
||||
Rng(StdRng::from_rng(thread_rng()).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl Rng {
|
||||
#[allow(dead_code)]
|
||||
pub fn from_seed(seed: [u8; 32]) -> Self {
|
||||
Rng(StdRng::from_seed(seed))
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn from_seed(seed: [u8; 32]) -> Self {
|
||||
Rng(StdRng::from_seed(seed))
|
||||
}
|
||||
|
||||
pub fn gen_string(&mut self, len: usize) -> String {
|
||||
(0..len)
|
||||
.map(|_| {
|
||||
let c = self.0.gen::<char>();
|
||||
format!("{:x}", c as u32)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
pub fn gen_string(&mut self, len: usize) -> String {
|
||||
(0..len)
|
||||
.map(|_| {
|
||||
let c = self.0.gen::<char>();
|
||||
format!("{:x}", c as u32)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
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;
|
||||
if left == 0 {
|
||||
break;
|
||||
}
|
||||
let i = if left == 1 {
|
||||
1
|
||||
} else {
|
||||
1 + self.0.gen_range(0..std::cmp::min(left - 1, 20))
|
||||
};
|
||||
match self.0.gen_range(0.0..1.0) {
|
||||
f if f < 0.2 => {
|
||||
delta.insert(&self.gen_string(i), AttributeHashMap::default());
|
||||
}
|
||||
f if f < 0.4 => {
|
||||
delta.delete(i);
|
||||
}
|
||||
_ => {
|
||||
delta.retain(i, AttributeHashMap::default());
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.0.gen_range(0.0..1.0) < 0.3 {
|
||||
delta.insert(&("1".to_owned() + &self.gen_string(10)), AttributeHashMap::default());
|
||||
}
|
||||
delta
|
||||
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;
|
||||
if left == 0 {
|
||||
break;
|
||||
}
|
||||
let i = if left == 1 {
|
||||
1
|
||||
} else {
|
||||
1 + self.0.gen_range(0..std::cmp::min(left - 1, 20))
|
||||
};
|
||||
match self.0.gen_range(0.0..1.0) {
|
||||
f if f < 0.2 => {
|
||||
delta.insert(&self.gen_string(i), AttributeHashMap::default());
|
||||
},
|
||||
f if f < 0.4 => {
|
||||
delta.delete(i);
|
||||
},
|
||||
_ => {
|
||||
delta.retain(i, AttributeHashMap::default());
|
||||
},
|
||||
}
|
||||
}
|
||||
if self.0.gen_range(0.0..1.0) < 0.3 {
|
||||
delta.insert(
|
||||
&("1".to_owned() + &self.gen_string(10)),
|
||||
AttributeHashMap::default(),
|
||||
);
|
||||
}
|
||||
delta
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,113 +1,118 @@
|
||||
use flowy_client_sync::client_document::{ClientDocument, EmptyDocument};
|
||||
use lib_ot::text_delta::DeltaTextOperation;
|
||||
use lib_ot::{
|
||||
core::*,
|
||||
text_delta::{BuildInTextAttribute, DeltaTextOperations},
|
||||
core::*,
|
||||
text_delta::{BuildInTextAttribute, DeltaTextOperations},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn operation_insert_serialize_test() {
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert("bold", true)
|
||||
.insert("italic", true)
|
||||
.build();
|
||||
let operation = DeltaOperation::insert_with_attributes("123", attributes);
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
eprintln!("{}", json);
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert("bold", true)
|
||||
.insert("italic", true)
|
||||
.build();
|
||||
let operation = DeltaOperation::insert_with_attributes("123", attributes);
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
eprintln!("{}", json);
|
||||
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operation_retain_serialize_test() {
|
||||
let operation = DeltaOperation::Retain(12.into());
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
eprintln!("{}", json);
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
let operation = DeltaOperation::Retain(12.into());
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
eprintln!("{}", json);
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operation_delete_serialize_test() {
|
||||
let operation = DeltaTextOperation::Delete(2);
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
let operation = DeltaTextOperation::Delete(2);
|
||||
let json = serde_json::to_string(&operation).unwrap();
|
||||
let insert_op: DeltaTextOperation = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(insert_op, operation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attributes_serialize_test() {
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert_entry(BuildInTextAttribute::Bold(true))
|
||||
.insert_entry(BuildInTextAttribute::Italic(true))
|
||||
.build();
|
||||
let retain = DeltaOperation::insert_with_attributes("123", attributes);
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert_entry(BuildInTextAttribute::Bold(true))
|
||||
.insert_entry(BuildInTextAttribute::Italic(true))
|
||||
.build();
|
||||
let retain = DeltaOperation::insert_with_attributes("123", attributes);
|
||||
|
||||
let json = serde_json::to_string(&retain).unwrap();
|
||||
eprintln!("{}", json);
|
||||
let json = serde_json::to_string(&retain).unwrap();
|
||||
eprintln!("{}", json);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_serialize_multi_attribute_test() {
|
||||
let mut delta = DeltaOperations::default();
|
||||
let mut delta = DeltaOperations::default();
|
||||
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert_entry(BuildInTextAttribute::Bold(true))
|
||||
.insert_entry(BuildInTextAttribute::Italic(true))
|
||||
.build();
|
||||
let retain = DeltaOperation::insert_with_attributes("123", attributes);
|
||||
let attributes = AttributeBuilder::new()
|
||||
.insert_entry(BuildInTextAttribute::Bold(true))
|
||||
.insert_entry(BuildInTextAttribute::Italic(true))
|
||||
.build();
|
||||
let retain = DeltaOperation::insert_with_attributes("123", attributes);
|
||||
|
||||
delta.add(retain);
|
||||
delta.add(DeltaOperation::Retain(5.into()));
|
||||
delta.add(DeltaOperation::Delete(3));
|
||||
delta.add(retain);
|
||||
delta.add(DeltaOperation::Retain(5.into()));
|
||||
delta.add(DeltaOperation::Delete(3));
|
||||
|
||||
let json = serde_json::to_string(&delta).unwrap();
|
||||
eprintln!("{}", json);
|
||||
let json = serde_json::to_string(&delta).unwrap();
|
||||
eprintln!("{}", json);
|
||||
|
||||
let delta_from_json = DeltaOperations::from_json(&json).unwrap();
|
||||
assert_eq!(delta_from_json, delta);
|
||||
let delta_from_json = DeltaOperations::from_json(&json).unwrap();
|
||||
assert_eq!(delta_from_json, delta);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_deserialize_test() {
|
||||
let json = r#"[
|
||||
let json = r#"[
|
||||
{"retain":2,"attributes":{"italic":true}},
|
||||
{"retain":2,"attributes":{"italic":123}},
|
||||
{"retain":2,"attributes":{"italic":true,"bold":true}},
|
||||
{"retain":2,"attributes":{"italic":true,"bold":true}}
|
||||
]"#;
|
||||
let delta = DeltaTextOperations::from_json(json).unwrap();
|
||||
eprintln!("{}", delta);
|
||||
let delta = DeltaTextOperations::from_json(json).unwrap();
|
||||
eprintln!("{}", delta);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_deserialize_null_test() {
|
||||
let json = r#"[
|
||||
let json = r#"[
|
||||
{"retain":7,"attributes":{"bold":null}}
|
||||
]"#;
|
||||
let delta1 = DeltaTextOperations::from_json(json).unwrap();
|
||||
let delta1 = DeltaTextOperations::from_json(json).unwrap();
|
||||
|
||||
let mut attribute = BuildInTextAttribute::Bold(true);
|
||||
attribute.clear();
|
||||
let mut attribute = BuildInTextAttribute::Bold(true);
|
||||
attribute.clear();
|
||||
|
||||
let delta2 = DeltaOperationBuilder::new()
|
||||
.retain_with_attributes(7, attribute.into())
|
||||
.build();
|
||||
let delta2 = DeltaOperationBuilder::new()
|
||||
.retain_with_attributes(7, attribute.into())
|
||||
.build();
|
||||
|
||||
assert_eq!(delta2.json_str(), r#"[{"retain":7,"attributes":{"bold":null}}]"#);
|
||||
assert_eq!(delta1, delta2);
|
||||
assert_eq!(
|
||||
delta2.json_str(),
|
||||
r#"[{"retain":7,"attributes":{"bold":null}}]"#
|
||||
);
|
||||
assert_eq!(delta1, delta2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_insert_serde_test() {
|
||||
let mut document = ClientDocument::new::<EmptyDocument>();
|
||||
document.insert(0, "\n").unwrap();
|
||||
document.insert(0, "123").unwrap();
|
||||
let json = document.get_operations_json();
|
||||
assert_eq!(r#"[{"insert":"123\n"}]"#, json);
|
||||
assert_eq!(
|
||||
r#"[{"insert":"123\n"}]"#,
|
||||
ClientDocument::from_json(&json).unwrap().get_operations_json()
|
||||
);
|
||||
let mut document = ClientDocument::new::<EmptyDocument>();
|
||||
document.insert(0, "\n").unwrap();
|
||||
document.insert(0, "123").unwrap();
|
||||
let json = document.get_operations_json();
|
||||
assert_eq!(r#"[{"insert":"123\n"}]"#, json);
|
||||
assert_eq!(
|
||||
r#"[{"insert":"123\n"}]"#,
|
||||
ClientDocument::from_json(&json)
|
||||
.unwrap()
|
||||
.get_operations_json()
|
||||
);
|
||||
}
|
||||
|
@ -4,370 +4,386 @@ use lib_ot::core::{Interval, NEW_LINE, WHITESPACE};
|
||||
|
||||
#[test]
|
||||
fn history_insert_undo() {
|
||||
let ops = vec![Insert(0, "123", 0), Undo(0), AssertDocJson(0, r#"[{"insert":"\n"}]"#)];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_insert_undo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, "456", 0),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, "456", 0),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_insert_redo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_insert_redo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, "456", 3),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertStr(0, "123456\n"),
|
||||
AssertDocJson(0, r#"[{"insert":"123456\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123456\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, "456", 3),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertStr(0, "123456\n"),
|
||||
AssertDocJson(0, r#"[{"insert":"123456\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123456\n"}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_bold_undo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_bold_undo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_bold_redo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#" [{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#" [{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#,
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_bold_redo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#,
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_delete_undo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertDocJson(0, r#"[{"insert":"123"}]"#),
|
||||
Delete(0, Interval::new(0, 3)),
|
||||
AssertDocJson(0, r#"[]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<EmptyDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertDocJson(0, r#"[{"insert":"123"}]"#),
|
||||
Delete(0, Interval::new(0, 3)),
|
||||
AssertDocJson(0, r#"[]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<EmptyDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_delete_undo_2() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Delete(0, Interval::new(0, 1)),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Delete(0, Interval::new(0, 1)),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
{"insert":"23","attributes":{"bold":true}},
|
||||
{"insert":"\n"}]
|
||||
"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_delete_undo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Delete(0, Interval::new(0, 1)),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Delete(0, Interval::new(0, 1)),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
{"insert":"23","attributes":{"bold":true}},
|
||||
{"insert":"\n"}]
|
||||
"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
{"insert":"123","attributes":{"bold":true}},
|
||||
{"insert":"\n"}]
|
||||
"#,
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_delete_redo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Delete(0, Interval::new(0, 3)),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Undo(0),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Delete(0, Interval::new(0, 3)),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Undo(0),
|
||||
Redo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_replace_undo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Replace(0, Interval::new(0, 2), "ab"),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Replace(0, Interval::new(0, 2), "ab"),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
{"insert":"ab"},
|
||||
{"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
|
||||
"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_replace_undo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Replace(0, Interval::new(0, 2), "ab"),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Replace(0, Interval::new(0, 2), "ab"),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
{"insert":"ab"},
|
||||
{"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
|
||||
"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123","attributes":{"bold":true}},{"insert":"\n"}]"#,
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_replace_redo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Replace(0, Interval::new(0, 2), "ab"),
|
||||
Undo(0),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
let ops = vec![
|
||||
Insert(0, "123", 0),
|
||||
Bold(0, Interval::new(0, 3), true),
|
||||
Replace(0, Interval::new(0, 2), "ab"),
|
||||
Undo(0),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[
|
||||
{"insert":"ab"},
|
||||
{"insert":"3","attributes":{"bold":true}},{"insert":"\n"}]
|
||||
"#,
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
),
|
||||
];
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_header_added_undo() {
|
||||
let ops = vec![
|
||||
Insert(0, "123456", 0),
|
||||
Header(0, Interval::new(0, 6), 1),
|
||||
Insert(0, "\n", 3),
|
||||
Insert(0, "\n", 4),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123"},{"insert":"\n\n","attributes":{"header":1}},{"insert":"456"},{"insert":"\n","attributes":{"header":1}}]"#,
|
||||
),
|
||||
];
|
||||
let ops = vec![
|
||||
Insert(0, "123456", 0),
|
||||
Header(0, Interval::new(0, 6), 1),
|
||||
Insert(0, "\n", 3),
|
||||
Insert(0, "\n", 4),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123"},{"insert":"\n\n","attributes":{"header":1}},{"insert":"456"},{"insert":"\n","attributes":{"header":1}}]"#,
|
||||
),
|
||||
];
|
||||
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_link_added_undo() {
|
||||
let site = "https://appflowy.io";
|
||||
let ops = vec![
|
||||
Insert(0, site, 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Link(0, Interval::new(0, site.len()), site),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io"}},{"insert":"\n"}]"#,
|
||||
),
|
||||
];
|
||||
let site = "https://appflowy.io";
|
||||
let ops = vec![
|
||||
Insert(0, site, 0),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Link(0, Interval::new(0, site.len()), site),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io"}},{"insert":"\n"}]"#,
|
||||
),
|
||||
];
|
||||
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_link_auto_format_undo_with_lagging() {
|
||||
let site = "https://appflowy.io";
|
||||
let ops = vec![
|
||||
Insert(0, site, 0),
|
||||
AssertDocJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, WHITESPACE, site.len()),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io/"}},{"insert":" \n"}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
|
||||
];
|
||||
let site = "https://appflowy.io";
|
||||
let ops = vec![
|
||||
Insert(0, site, 0),
|
||||
AssertDocJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, WHITESPACE, site.len()),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io/"}},{"insert":" \n"}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
|
||||
];
|
||||
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_bullet_undo() {
|
||||
let ops = vec![
|
||||
Insert(0, "1", 0),
|
||||
Bullet(0, Interval::new(0, 1), true),
|
||||
Insert(0, NEW_LINE, 1),
|
||||
Insert(0, "2", 2),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
];
|
||||
let ops = vec![
|
||||
Insert(0, "1", 0),
|
||||
Bullet(0, Interval::new(0, 1), true),
|
||||
Insert(0, NEW_LINE, 1),
|
||||
Insert(0, "2", 2),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
];
|
||||
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_bullet_undo_with_lagging() {
|
||||
let ops = vec![
|
||||
Insert(0, "1", 0),
|
||||
Bullet(0, Interval::new(0, 1), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, NEW_LINE, 1),
|
||||
Insert(0, "2", 2),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}}]"#),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
];
|
||||
let ops = vec![
|
||||
Insert(0, "1", 0),
|
||||
Bullet(0, Interval::new(0, 1), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, NEW_LINE, 1),
|
||||
Insert(0, "2", 2),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(0, r#"[{"insert":"\n"}]"#),
|
||||
Redo(0),
|
||||
Redo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
];
|
||||
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_undo_attribute_on_merge_between_line() {
|
||||
let ops = vec![
|
||||
Insert(0, "123456", 0),
|
||||
Bullet(0, Interval::new(0, 6), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, NEW_LINE, 3),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Delete(0, Interval::new(3, 4)), // delete the newline
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
];
|
||||
let ops = vec![
|
||||
Insert(0, "123456", 0),
|
||||
Bullet(0, Interval::new(0, 6), true),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
Insert(0, NEW_LINE, 3),
|
||||
Wait(RECORD_THRESHOLD),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Delete(0, Interval::new(3, 4)), // delete the newline
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
Undo(0),
|
||||
AssertDocJson(
|
||||
0,
|
||||
r#"[{"insert":"123"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
|
||||
),
|
||||
];
|
||||
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
TestBuilder::new().run_scripts::<NewlineDocument>(ops);
|
||||
}
|
||||
|
@ -3,22 +3,22 @@ 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;
|
||||
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;
|
||||
}
|
||||
|
@ -8,109 +8,123 @@ use lib_ot::text_delta::DeltaTextOperations;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub enum EditScript {
|
||||
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,
|
||||
},
|
||||
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 {
|
||||
pub sdk: FlowySDKTest,
|
||||
pub editor: Arc<AppFlowyDocumentEditor>,
|
||||
pub sdk: FlowySDKTest,
|
||||
pub editor: Arc<AppFlowyDocumentEditor>,
|
||||
}
|
||||
|
||||
impl DocumentEditorTest {
|
||||
pub async fn new() -> Self {
|
||||
let version = DocumentVersionPB::V1;
|
||||
let sdk = FlowySDKTest::new(version.clone());
|
||||
let _ = sdk.init_user().await;
|
||||
pub async fn new() -> Self {
|
||||
let version = DocumentVersionPB::V1;
|
||||
let sdk = FlowySDKTest::new(version.clone());
|
||||
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(),
|
||||
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_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::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
|
||||
.apply_transaction(Transaction::from_operations(vec![operation]))
|
||||
.await
|
||||
.unwrap();
|
||||
},
|
||||
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();
|
||||
|
||||
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_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::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
|
||||
.apply_transaction(Transaction::from_operations(vec![operation]))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
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 } => {
|
||||
//
|
||||
let content = self.editor.get_content(true).await.unwrap();
|
||||
assert_eq!(content, expected);
|
||||
}
|
||||
}
|
||||
assert_eq!(content, expected);
|
||||
},
|
||||
EditScript::AssertPrettyContent { expected } => {
|
||||
//
|
||||
let content = self.editor.get_content(true).await.unwrap();
|
||||
assert_eq!(content, expected);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,16 +5,18 @@ use lib_ot::text_delta::DeltaTextOperationBuilder;
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_initialize_test() {
|
||||
let scripts = vec![AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text"}]}}"#,
|
||||
}];
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text"}]}}"#,
|
||||
}];
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_insert_text_test() {
|
||||
let delta = DeltaTextOperationBuilder::new().insert("Hello world").build();
|
||||
let expected = r#"{
|
||||
let delta = DeltaTextOperationBuilder::new()
|
||||
.insert("Hello world")
|
||||
.build();
|
||||
let expected = r#"{
|
||||
"document": {
|
||||
"type": "editor",
|
||||
"children": [
|
||||
@ -32,27 +34,29 @@ async fn document_insert_text_test() {
|
||||
]
|
||||
}
|
||||
}"#;
|
||||
let scripts = vec![
|
||||
InsertText {
|
||||
path: vec![0, 0].into(),
|
||||
delta,
|
||||
},
|
||||
AssertPrettyContent { expected },
|
||||
];
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![
|
||||
InsertText {
|
||||
path: vec![0, 0].into(),
|
||||
delta,
|
||||
},
|
||||
AssertPrettyContent { expected },
|
||||
];
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_update_text_test() {
|
||||
let test = DocumentEditorTest::new().await;
|
||||
let hello_world = "Hello world".to_string();
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new().insert(&hello_world).build(),
|
||||
},
|
||||
AssertPrettyContent {
|
||||
expected: r#"{
|
||||
let test = DocumentEditorTest::new().await;
|
||||
let hello_world = "Hello world".to_string();
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new()
|
||||
.insert(&hello_world)
|
||||
.build(),
|
||||
},
|
||||
AssertPrettyContent {
|
||||
expected: r#"{
|
||||
"document": {
|
||||
"type": "editor",
|
||||
"children": [
|
||||
@ -67,21 +71,21 @@ async fn document_update_text_test() {
|
||||
]
|
||||
}
|
||||
}"#,
|
||||
},
|
||||
];
|
||||
},
|
||||
];
|
||||
|
||||
test.run_scripts(scripts).await;
|
||||
test.run_scripts(scripts).await;
|
||||
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new()
|
||||
.retain(hello_world.len())
|
||||
.insert(", AppFlowy")
|
||||
.build(),
|
||||
},
|
||||
AssertPrettyContent {
|
||||
expected: r#"{
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new()
|
||||
.retain(hello_world.len())
|
||||
.insert(", AppFlowy")
|
||||
.build(),
|
||||
},
|
||||
AssertPrettyContent {
|
||||
expected: r#"{
|
||||
"document": {
|
||||
"type": "editor",
|
||||
"children": [
|
||||
@ -96,14 +100,14 @@ async fn document_update_text_test() {
|
||||
]
|
||||
}
|
||||
}"#,
|
||||
},
|
||||
];
|
||||
test.run_scripts(scripts).await;
|
||||
},
|
||||
];
|
||||
test.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_delete_text_test() {
|
||||
let expected = r#"{
|
||||
let expected = r#"{
|
||||
"document": {
|
||||
"type": "editor",
|
||||
"children": [
|
||||
@ -118,39 +122,43 @@ async fn document_delete_text_test() {
|
||||
]
|
||||
}
|
||||
}"#;
|
||||
let hello_world = "Hello world".to_string();
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new().insert(&hello_world).build(),
|
||||
},
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new().retain(5).delete(6).build(),
|
||||
},
|
||||
AssertPrettyContent { expected },
|
||||
];
|
||||
let hello_world = "Hello world".to_string();
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new()
|
||||
.insert(&hello_world)
|
||||
.build(),
|
||||
},
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new().retain(5).delete(6).build(),
|
||||
},
|
||||
AssertPrettyContent { expected },
|
||||
];
|
||||
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn document_delete_node_test() {
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new().insert("Hello world").build(),
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text","delta":[{"insert":"Hello world"}]}]}}"#,
|
||||
},
|
||||
Delete {
|
||||
path: vec![0, 0].into(),
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor"}}"#,
|
||||
},
|
||||
];
|
||||
let scripts = vec![
|
||||
UpdateText {
|
||||
path: vec![0, 0].into(),
|
||||
delta: DeltaTextOperationBuilder::new()
|
||||
.insert("Hello world")
|
||||
.build(),
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor","children":[{"type":"text","delta":[{"insert":"Hello world"}]}]}}"#,
|
||||
},
|
||||
Delete {
|
||||
path: vec![0, 0].into(),
|
||||
},
|
||||
AssertContent {
|
||||
expected: r#"{"document":{"type":"editor"}}"#,
|
||||
},
|
||||
];
|
||||
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
DocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
}
|
||||
|
@ -4,102 +4,126 @@ use lib_ot::core::{count_utf16_code_units, Interval};
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_current_rev_id_check() {
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
AssertCurrentRevId(1),
|
||||
InsertText("2", 1),
|
||||
AssertCurrentRevId(2),
|
||||
InsertText("3", 2),
|
||||
AssertCurrentRevId(3),
|
||||
AssertNextSyncRevId(None),
|
||||
AssertJson(r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
AssertCurrentRevId(1),
|
||||
InsertText("2", 1),
|
||||
AssertCurrentRevId(2),
|
||||
InsertText("3", 2),
|
||||
AssertCurrentRevId(3),
|
||||
AssertNextSyncRevId(None),
|
||||
AssertJson(r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_state_check() {
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
AssertRevisionState(1, RevisionState::Ack),
|
||||
AssertRevisionState(2, RevisionState::Ack),
|
||||
AssertRevisionState(3, RevisionState::Ack),
|
||||
AssertJson(r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
AssertRevisionState(1, RevisionState::Ack),
|
||||
AssertRevisionState(2, RevisionState::Ack),
|
||||
AssertRevisionState(3, RevisionState::Ack),
|
||||
AssertJson(r#"[{"insert":"123\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_insert_test() {
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
AssertJson(r#"[{"insert":"123\n"}]"#),
|
||||
AssertNextSyncRevId(None),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
AssertJson(r#"[{"insert":"123\n"}]"#),
|
||||
AssertNextSyncRevId(None),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_insert_in_chinese() {
|
||||
let s = "好".to_owned();
|
||||
let offset = count_utf16_code_units(&s);
|
||||
let scripts = vec![
|
||||
InsertText("你", 0),
|
||||
InsertText("好", offset),
|
||||
AssertJson(r#"[{"insert":"你好\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let s = "好".to_owned();
|
||||
let offset = count_utf16_code_units(&s);
|
||||
let scripts = vec![
|
||||
InsertText("你", 0),
|
||||
InsertText("好", offset),
|
||||
AssertJson(r#"[{"insert":"你好\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_insert_with_emoji() {
|
||||
let s = "😁".to_owned();
|
||||
let offset = count_utf16_code_units(&s);
|
||||
let scripts = vec![
|
||||
InsertText("😁", 0),
|
||||
InsertText("☺️", offset),
|
||||
AssertJson(r#"[{"insert":"😁☺️\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let s = "😁".to_owned();
|
||||
let offset = count_utf16_code_units(&s);
|
||||
let scripts = vec![
|
||||
InsertText("😁", 0),
|
||||
InsertText("☺️", offset),
|
||||
AssertJson(r#"[{"insert":"😁☺️\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_delete_in_english() {
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
Delete(Interval::new(0, 2)),
|
||||
AssertJson(r#"[{"insert":"3\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
Delete(Interval::new(0, 2)),
|
||||
AssertJson(r#"[{"insert":"3\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_delete_in_chinese() {
|
||||
let s = "好".to_owned();
|
||||
let offset = count_utf16_code_units(&s);
|
||||
let scripts = vec![
|
||||
InsertText("你", 0),
|
||||
InsertText("好", offset),
|
||||
Delete(Interval::new(0, offset)),
|
||||
AssertJson(r#"[{"insert":"好\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let s = "好".to_owned();
|
||||
let offset = count_utf16_code_units(&s);
|
||||
let scripts = vec![
|
||||
InsertText("你", 0),
|
||||
InsertText("好", offset),
|
||||
Delete(Interval::new(0, offset)),
|
||||
AssertJson(r#"[{"insert":"好\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn text_block_sync_replace_test() {
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
Replace(Interval::new(0, 3), "abc"),
|
||||
AssertJson(r#"[{"insert":"abc\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new().await.run_scripts(scripts).await;
|
||||
let scripts = vec![
|
||||
InsertText("1", 0),
|
||||
InsertText("2", 1),
|
||||
InsertText("3", 2),
|
||||
Replace(Interval::new(0, 3), "abc"),
|
||||
AssertJson(r#"[{"insert":"abc\n"}]"#),
|
||||
];
|
||||
DeltaDocumentEditorTest::new()
|
||||
.await
|
||||
.run_scripts(scripts)
|
||||
.await;
|
||||
}
|
||||
|
@ -7,83 +7,90 @@ use std::sync::Arc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
|
||||
pub enum EditorScript {
|
||||
InsertText(&'static str, usize),
|
||||
Delete(Interval),
|
||||
Replace(Interval, &'static str),
|
||||
InsertText(&'static str, usize),
|
||||
Delete(Interval),
|
||||
Replace(Interval, &'static str),
|
||||
|
||||
AssertRevisionState(i64, RevisionState),
|
||||
AssertNextSyncRevId(Option<i64>),
|
||||
AssertCurrentRevId(i64),
|
||||
AssertJson(&'static str),
|
||||
AssertRevisionState(i64, RevisionState),
|
||||
AssertNextSyncRevId(Option<i64>),
|
||||
AssertCurrentRevId(i64),
|
||||
AssertJson(&'static str),
|
||||
}
|
||||
|
||||
pub struct DeltaDocumentEditorTest {
|
||||
pub sdk: FlowySDKTest,
|
||||
pub editor: Arc<DeltaDocumentEditor>,
|
||||
pub sdk: FlowySDKTest,
|
||||
pub editor: Arc<DeltaDocumentEditor>,
|
||||
}
|
||||
|
||||
impl DeltaDocumentEditorTest {
|
||||
pub async fn new() -> Self {
|
||||
let sdk = FlowySDKTest::default();
|
||||
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<DeltaDocumentEditor>>() {
|
||||
None => panic!(),
|
||||
Some(editor) => editor.clone(),
|
||||
};
|
||||
Self { sdk, editor }
|
||||
}
|
||||
pub async fn new() -> Self {
|
||||
let sdk = FlowySDKTest::default();
|
||||
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<DeltaDocumentEditor>>()
|
||||
{
|
||||
None => panic!(),
|
||||
Some(editor) => editor.clone(),
|
||||
};
|
||||
Self { sdk, editor }
|
||||
}
|
||||
|
||||
pub async fn run_scripts(mut self, scripts: Vec<EditorScript>) {
|
||||
for script in scripts {
|
||||
self.run_script(script).await;
|
||||
pub async fn run_scripts(mut self, scripts: Vec<EditorScript>) {
|
||||
for script in scripts {
|
||||
self.run_script(script).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_script(&mut self, script: EditorScript) {
|
||||
let rev_manager = self.editor.rev_manager();
|
||||
let cache = rev_manager.revision_cache().await;
|
||||
let _user_id = self.sdk.user_session.user_id().unwrap();
|
||||
|
||||
match script {
|
||||
EditorScript::InsertText(s, offset) => {
|
||||
self.editor.insert(offset, s).await.unwrap();
|
||||
},
|
||||
EditorScript::Delete(interval) => {
|
||||
self.editor.delete(interval).await.unwrap();
|
||||
},
|
||||
EditorScript::Replace(interval, s) => {
|
||||
self.editor.replace(interval, s).await.unwrap();
|
||||
},
|
||||
EditorScript::AssertRevisionState(rev_id, state) => {
|
||||
let record = cache.get(rev_id).await.unwrap();
|
||||
assert_eq!(record.state, state);
|
||||
},
|
||||
EditorScript::AssertCurrentRevId(rev_id) => {
|
||||
assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
|
||||
},
|
||||
EditorScript::AssertNextSyncRevId(rev_id) => {
|
||||
let next_revision = rev_manager.next_sync_revision().await.unwrap();
|
||||
if rev_id.is_none() {
|
||||
assert!(next_revision.is_none(), "Next revision should be None");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_script(&mut self, script: EditorScript) {
|
||||
let rev_manager = self.editor.rev_manager();
|
||||
let cache = rev_manager.revision_cache().await;
|
||||
let _user_id = self.sdk.user_session.user_id().unwrap();
|
||||
|
||||
match script {
|
||||
EditorScript::InsertText(s, offset) => {
|
||||
self.editor.insert(offset, s).await.unwrap();
|
||||
}
|
||||
EditorScript::Delete(interval) => {
|
||||
self.editor.delete(interval).await.unwrap();
|
||||
}
|
||||
EditorScript::Replace(interval, s) => {
|
||||
self.editor.replace(interval, s).await.unwrap();
|
||||
}
|
||||
EditorScript::AssertRevisionState(rev_id, state) => {
|
||||
let record = cache.get(rev_id).await.unwrap();
|
||||
assert_eq!(record.state, state);
|
||||
}
|
||||
EditorScript::AssertCurrentRevId(rev_id) => {
|
||||
assert_eq!(self.editor.rev_manager().rev_id(), rev_id);
|
||||
}
|
||||
EditorScript::AssertNextSyncRevId(rev_id) => {
|
||||
let next_revision = rev_manager.next_sync_revision().await.unwrap();
|
||||
if rev_id.is_none() {
|
||||
assert!(next_revision.is_none(), "Next revision should be None");
|
||||
return;
|
||||
}
|
||||
let next_revision = next_revision.unwrap();
|
||||
let mut notify = rev_manager.ack_notify();
|
||||
let _ = notify.recv().await;
|
||||
assert_eq!(next_revision.rev_id, rev_id.unwrap());
|
||||
}
|
||||
EditorScript::AssertJson(expected) => {
|
||||
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,);
|
||||
eprintln!("❌ receive: {}", delta.json_str());
|
||||
}
|
||||
assert_eq!(expected_delta, delta);
|
||||
}
|
||||
let next_revision = next_revision.unwrap();
|
||||
let mut notify = rev_manager.ack_notify();
|
||||
let _ = notify.recv().await;
|
||||
assert_eq!(next_revision.rev_id, rev_id.unwrap());
|
||||
},
|
||||
EditorScript::AssertJson(expected) => {
|
||||
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,);
|
||||
eprintln!("❌ receive: {}", delta.json_str());
|
||||
}
|
||||
sleep(Duration::from_millis(TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS)).await;
|
||||
assert_eq!(expected_delta, delta);
|
||||
},
|
||||
}
|
||||
sleep(Duration::from_millis(TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS)).await;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user