AppFlowy/rust-lib/flowy-document/tests/editor/mod.rs

317 lines
12 KiB
Rust
Raw Normal View History

mod attribute_test;
mod op_test;
mod serde_test;
mod undo_redo_test;
use derive_more::Display;
use flowy_document::services::doc::{CustomDocument, Document};
use flowy_ot::core::*;
2021-07-31 12:53:45 +00:00
use rand::{prelude::*, Rng as WrappedRng};
2021-08-10 09:08:47 +00:00
use std::{sync::Once, time::Duration};
2021-07-31 12:53:45 +00:00
2021-09-15 07:01:24 +00:00
const LEVEL: &'static str = "debug";
2021-08-16 09:00:39 +00:00
#[derive(Clone, Debug, Display)]
2021-08-04 07:09:04 +00:00
pub enum TestOp {
#[display(fmt = "Insert")]
2021-08-03 15:13:20 +00:00
Insert(usize, &'static str, usize),
// 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),
#[display(fmt = "Delete")]
2021-08-03 08:20:25 +00:00
Delete(usize, Interval),
#[display(fmt = "Replace")]
Replace(usize, Interval, &'static str),
#[display(fmt = "Italic")]
2021-08-02 15:08:24 +00:00
Italic(usize, Interval, bool),
2021-08-15 13:11:48 +00:00
#[display(fmt = "Header")]
2021-09-13 07:51:13 +00:00
Header(usize, Interval, usize),
2021-08-15 13:11:48 +00:00
2021-08-16 07:02:57 +00:00
#[display(fmt = "Link")]
2021-09-13 07:51:13 +00:00
Link(usize, Interval, &'static str),
2021-08-16 07:02:57 +00:00
2021-08-16 15:07:40 +00:00
#[display(fmt = "Bullet")]
Bullet(usize, Interval, bool),
#[display(fmt = "Transform")]
Transform(usize, usize),
2021-09-26 08:39:57 +00:00
#[display(fmt = "TransformPrime")]
TransformPrime(usize, usize),
// invert the delta_a base on the delta_b
2021-08-05 14:52:19 +00:00
#[display(fmt = "Invert")]
Invert(usize, usize),
#[display(fmt = "Undo")]
2021-08-05 14:52:19 +00:00
Undo(usize),
#[display(fmt = "Redo")]
Redo(usize),
#[display(fmt = "Wait")]
Wait(usize),
2021-08-06 14:25:09 +00:00
#[display(fmt = "AssertStr")]
AssertStr(usize, &'static str),
2021-09-26 08:39:57 +00:00
#[display(fmt = "AssertDocJson")]
AssertDocJson(usize, &'static str),
#[display(fmt = "AssertPrimeJson")]
AssertPrimeJson(usize, &'static str),
#[display(fmt = "DocComposeDelta")]
DocComposeDelta(usize, usize),
#[display(fmt = "ApplyPrimeDelta")]
DocComposePrime(usize, usize),
}
pub struct TestBuilder {
2021-08-05 12:05:40 +00:00
documents: Vec<Document>,
2021-09-26 08:39:57 +00:00
deltas: Vec<Option<Delta>>,
primes: Vec<Option<Delta>>,
}
impl TestBuilder {
pub fn new() -> Self {
static INIT: Once = Once::new();
INIT.call_once(|| {
2021-08-08 14:29:16 +00:00
color_eyre::install().unwrap();
2021-08-16 09:00:39 +00:00
std::env::set_var("RUST_LOG", LEVEL);
env_logger::init();
});
2021-09-26 08:39:57 +00:00
Self {
documents: vec![],
deltas: vec![],
primes: vec![],
}
}
fn run_op(&mut self, op: &TestOp) {
2021-11-03 07:37:38 +00:00
tracing::trace!("***************** 😈{} *******************", &op);
match op {
2021-08-04 07:09:04 +00:00
TestOp::Insert(delta_i, s, index) => {
2021-08-05 12:05:40 +00:00
let document = &mut self.documents[*delta_i];
2021-09-26 08:39:57 +00:00
let delta = document.insert(*index, s).unwrap();
2021-11-03 07:37:38 +00:00
tracing::debug!("Insert delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
},
2021-08-16 07:02:57 +00:00
TestOp::Delete(delta_i, iv) => {
2021-08-05 12:05:40 +00:00
let document = &mut self.documents[*delta_i];
2021-09-26 08:39:57 +00:00
let delta = document.replace(*iv, "").unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("Delete delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
},
2021-08-16 07:02:57 +00:00
TestOp::Replace(delta_i, iv, s) => {
let document = &mut self.documents[*delta_i];
2021-09-26 08:39:57 +00:00
let delta = document.replace(*iv, s).unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("Replace delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
2021-08-03 08:20:25 +00:00
},
2021-08-16 07:02:57 +00:00
TestOp::InsertBold(delta_i, s, iv) => {
2021-08-05 12:05:40 +00:00
let document = &mut self.documents[*delta_i];
2021-08-16 07:02:57 +00:00
document.insert(iv.start, s).unwrap();
2021-09-13 07:51:13 +00:00
document.format(*iv, Attribute::Bold(true)).unwrap();
},
2021-08-16 07:02:57 +00:00
TestOp::Bold(delta_i, iv, enable) => {
2021-08-05 12:05:40 +00:00
let document = &mut self.documents[*delta_i];
2021-09-13 07:51:13 +00:00
let attribute = Attribute::Bold(*enable);
2021-09-26 08:39:57 +00:00
let delta = document.format(*iv, attribute).unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("Bold delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
2021-08-02 15:08:24 +00:00
},
2021-08-16 07:02:57 +00:00
TestOp::Italic(delta_i, iv, enable) => {
2021-08-05 12:05:40 +00:00
let document = &mut self.documents[*delta_i];
2021-08-11 09:18:10 +00:00
let attribute = match *enable {
2021-09-13 07:51:13 +00:00
true => Attribute::Italic(true),
false => Attribute::Italic(false),
2021-08-15 13:11:48 +00:00
};
2021-09-26 08:39:57 +00:00
let delta = document.format(*iv, attribute).unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("Italic delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
2021-08-15 13:11:48 +00:00
},
2021-09-13 07:51:13 +00:00
TestOp::Header(delta_i, iv, level) => {
2021-08-15 13:11:48 +00:00
let document = &mut self.documents[*delta_i];
2021-09-13 07:51:13 +00:00
let attribute = Attribute::Header(*level);
2021-09-26 08:39:57 +00:00
let delta = document.format(*iv, attribute).unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("Header delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
2021-08-16 07:02:57 +00:00
},
2021-09-13 07:51:13 +00:00
TestOp::Link(delta_i, iv, link) => {
2021-08-16 07:02:57 +00:00
let document = &mut self.documents[*delta_i];
2021-09-13 07:51:13 +00:00
let attribute = Attribute::Link(link.to_owned());
2021-09-26 08:39:57 +00:00
let delta = document.format(*iv, attribute).unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("Link delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
},
2021-08-16 15:07:40 +00:00
TestOp::Bullet(delta_i, iv, enable) => {
let document = &mut self.documents[*delta_i];
2021-09-16 01:30:51 +00:00
let attribute = Attribute::Bullet(*enable);
2021-09-26 08:39:57 +00:00
let delta = document.format(*iv, attribute).unwrap();
2021-11-03 07:37:38 +00:00
tracing::debug!("Bullet delta: {}", delta.to_json());
2021-09-26 08:39:57 +00:00
self.deltas.insert(*delta_i, Some(delta));
2021-08-16 15:07:40 +00:00
},
2021-08-04 07:09:04 +00:00
TestOp::Transform(delta_a_i, delta_b_i) => {
2021-08-06 00:40:45 +00:00
let (a_prime, b_prime) = self.documents[*delta_a_i]
.delta()
.transform(&self.documents[*delta_b_i].delta())
2021-08-06 00:40:45 +00:00
.unwrap();
2021-11-03 07:37:38 +00:00
tracing::trace!("a:{:?},b:{:?}", a_prime, b_prime);
2021-08-06 00:40:45 +00:00
let data_left = self.documents[*delta_a_i].delta().compose(&b_prime).unwrap();
let data_right = self.documents[*delta_b_i].delta().compose(&a_prime).unwrap();
2021-08-06 00:40:45 +00:00
self.documents[*delta_a_i].set_delta(data_left);
self.documents[*delta_b_i].set_delta(data_right);
},
2021-09-26 08:39:57 +00:00
TestOp::TransformPrime(a_doc_index, b_doc_index) => {
let (prime_left, prime_right) = self.documents[*a_doc_index]
.delta()
.transform(&self.documents[*b_doc_index].delta())
.unwrap();
self.primes.insert(*a_doc_index, Some(prime_left));
self.primes.insert(*b_doc_index, Some(prime_right));
},
2021-08-05 14:52:19 +00:00
TestOp::Invert(delta_a_i, delta_b_i) => {
let delta_a = &self.documents[*delta_a_i].delta();
let delta_b = &self.documents[*delta_b_i].delta();
2021-11-03 07:37:38 +00:00
tracing::debug!("Invert: ");
tracing::debug!("a: {}", delta_a.to_json());
tracing::debug!("b: {}", delta_b.to_json());
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
2021-08-06 14:25:09 +00:00
let undo = b_prime.invert(&delta_a);
let new_delta = delta_a.compose(&b_prime).unwrap();
2021-11-03 07:37:38 +00:00
tracing::debug!("new delta: {}", new_delta.to_json());
tracing::debug!("undo delta: {}", undo.to_json());
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
2021-11-03 07:37:38 +00:00
tracing::debug!("inverted delta a: {}", new_delta_after_undo.to_string());
2021-08-05 12:05:40 +00:00
assert_eq!(delta_a, &&new_delta_after_undo);
self.documents[*delta_a_i].set_delta(new_delta_after_undo);
},
2021-08-05 14:52:19 +00:00
TestOp::Undo(delta_i) => {
2021-08-06 00:40:45 +00:00
self.documents[*delta_i].undo().unwrap();
2021-08-05 14:52:19 +00:00
},
TestOp::Redo(delta_i) => {
2021-08-06 00:40:45 +00:00
self.documents[*delta_i].redo().unwrap();
2021-08-05 14:52:19 +00:00
},
TestOp::Wait(mills_sec) => {
std::thread::sleep(Duration::from_millis(*mills_sec as u64));
},
2021-08-06 14:25:09 +00:00
TestOp::AssertStr(delta_i, expected) => {
2021-09-23 07:49:10 +00:00
assert_eq!(&self.documents[*delta_i].to_plain_string(), expected);
2021-08-06 14:25:09 +00:00
},
2021-09-26 08:39:57 +00:00
TestOp::AssertDocJson(delta_i, expected) => {
let delta_json = self.documents[*delta_i].to_json();
2021-08-03 08:20:25 +00:00
let expected_delta: Delta = serde_json::from_str(expected).unwrap();
2021-09-26 08:39:57 +00:00
let target_delta: Delta = serde_json::from_str(&delta_json).unwrap();
2021-08-03 08:20:25 +00:00
if expected_delta != target_delta {
2021-08-03 15:13:20 +00:00
log::error!("✅ expect: {}", expected,);
2021-09-26 08:39:57 +00:00
log::error!("❌ receive: {}", delta_json);
2021-08-02 15:08:24 +00:00
}
2021-08-03 08:20:25 +00:00
assert_eq!(target_delta, expected_delta);
},
2021-09-26 08:39:57 +00:00
TestOp::AssertPrimeJson(doc_i, expected) => {
let prime_json = self.primes[*doc_i].as_ref().unwrap().to_json();
let expected_prime: Delta = serde_json::from_str(expected).unwrap();
let target_prime: Delta = serde_json::from_str(&prime_json).unwrap();
if expected_prime != target_prime {
log::error!("✅ expect prime: {}", expected,);
log::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();
2021-09-30 09:24:02 +00:00
self.documents[*doc_index].compose_delta(delta).unwrap();
2021-09-26 08:39:57 +00:00
},
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].delta().compose(delta).unwrap();
self.documents[*doc_index].set_delta(new_delta);
},
}
}
pub fn run_script<C: CustomDocument>(mut self, script: Vec<TestOp>) {
self.documents = vec![Document::new::<C>(), Document::new::<C>()];
2021-09-26 08:39:57 +00:00
self.primes = vec![None, None];
self.deltas = vec![None, None];
2021-08-03 08:20:25 +00:00
for (_i, op) in script.iter().enumerate() {
self.run_op(op);
}
}
2021-08-03 08:20:25 +00:00
}
pub struct Rng(StdRng);
impl Default for Rng {
fn default() -> Self { Rng(StdRng::from_rng(thread_rng()).unwrap()) }
}
impl Rng {
2021-09-23 07:49:10 +00:00
#[allow(dead_code)]
2021-08-03 08:20:25 +00:00
pub fn from_seed(seed: [u8; 32]) -> Self { Rng(StdRng::from_seed(seed)) }
2021-09-13 07:51:13 +00:00
pub fn gen_string(&mut self, len: usize) -> String { (0..len).map(|_| self.0.gen::<char>()).collect() }
2021-08-03 08:20:25 +00:00
pub fn gen_delta(&mut self, s: &str) -> Delta {
let mut delta = Delta::default();
loop {
2021-08-04 07:09:04 +00:00
let left = s.chars().count() - delta.base_len;
2021-08-03 08:20:25 +00:00
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), Attributes::default());
2021-08-03 08:20:25 +00:00
},
f if f < 0.4 => {
2021-08-06 15:06:27 +00:00
delta.delete(i);
2021-08-03 08:20:25 +00:00
},
_ => {
2021-08-11 09:18:10 +00:00
delta.retain(i, Attributes::default());
2021-08-03 08:20:25 +00:00
},
}
}
if self.0.gen_range(0.0, 1.0) < 0.3 {
2021-09-13 07:51:13 +00:00
delta.insert(&("1".to_owned() + &self.gen_string(10)), Attributes::default());
2021-08-03 08:20:25 +00:00
}
delta
}
}