2021-08-05 07:05:20 +00:00
|
|
|
use derive_more::Display;
|
2021-08-05 12:05:40 +00:00
|
|
|
use flowy_ot::{
|
|
|
|
client::{transform, Document},
|
|
|
|
core::*,
|
|
|
|
};
|
2021-07-31 12:53:45 +00:00
|
|
|
use rand::{prelude::*, Rng as WrappedRng};
|
2021-08-02 10:35:25 +00:00
|
|
|
use std::sync::Once;
|
2021-07-31 12:53:45 +00:00
|
|
|
|
2021-08-05 07:05:20 +00:00
|
|
|
#[derive(Clone, Debug, Display)]
|
2021-08-04 07:09:04 +00:00
|
|
|
pub enum TestOp {
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "Insert")]
|
2021-08-03 15:13:20 +00:00
|
|
|
Insert(usize, &'static str, usize),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
// delta_i, s, start, length,
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "InsertBold")]
|
2021-08-02 10:35:25 +00:00
|
|
|
InsertBold(usize, &'static str, Interval),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
// delta_i, start, length, enable
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "Bold")]
|
2021-08-02 10:35:25 +00:00
|
|
|
Bold(usize, Interval, bool),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
|
|
|
#[display(fmt = "Delete")]
|
2021-08-03 08:20:25 +00:00
|
|
|
Delete(usize, Interval),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
|
|
|
#[display(fmt = "Italic")]
|
2021-08-02 15:08:24 +00:00
|
|
|
Italic(usize, Interval, bool),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
|
|
|
#[display(fmt = "Transform")]
|
2021-08-02 10:35:25 +00:00
|
|
|
Transform(usize, usize),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
// invert the delta_a base on the delta_b
|
2021-08-05 14:52:19 +00:00
|
|
|
#[display(fmt = "Invert")]
|
|
|
|
Invert(usize, usize),
|
|
|
|
|
2021-08-05 11:55:28 +00:00
|
|
|
#[display(fmt = "Undo")]
|
2021-08-05 14:52:19 +00:00
|
|
|
Undo(usize),
|
|
|
|
|
|
|
|
#[display(fmt = "Redo")]
|
|
|
|
Redo(usize),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
|
|
|
#[display(fmt = "AssertOpsJson")]
|
2021-08-02 10:35:25 +00:00
|
|
|
AssertOpsJson(usize, &'static str),
|
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
pub struct OpTester {
|
2021-08-05 12:05:40 +00:00
|
|
|
documents: Vec<Document>,
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
impl OpTester {
|
2021-08-02 10:35:25 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2021-08-02 15:08:24 +00:00
|
|
|
std::env::set_var("RUST_LOG", "info");
|
2021-08-02 10:35:25 +00:00
|
|
|
env_logger::init();
|
|
|
|
});
|
|
|
|
|
2021-08-05 12:05:40 +00:00
|
|
|
let mut documents = Vec::with_capacity(2);
|
2021-08-02 10:35:25 +00:00
|
|
|
for _ in 0..2 {
|
2021-08-05 12:05:40 +00:00
|
|
|
documents.push(Document::new());
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
2021-08-05 12:05:40 +00:00
|
|
|
Self { documents }
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
pub fn run_op(&mut self, op: &TestOp) {
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("***************** 😈{} *******************", &op);
|
2021-08-02 10:35:25 +00:00
|
|
|
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];
|
|
|
|
document.edit(*index, s);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Delete(delta_i, interval) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
|
|
|
document.delete(*interval);
|
2021-08-03 08:20:25 +00:00
|
|
|
},
|
2021-08-05 12:05:40 +00:00
|
|
|
TestOp::InsertBold(delta_i, s, interval) => {
|
|
|
|
let document = &mut self.documents[*delta_i];
|
|
|
|
document.edit(interval.start, s);
|
|
|
|
document.format(*interval, Attribute::Bold, true);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Bold(delta_i, interval, enable) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
|
|
|
document.format(*interval, Attribute::Bold, *enable);
|
2021-08-02 15:08:24 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Italic(delta_i, interval, enable) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
|
|
|
document.format(*interval, Attribute::Italic, *enable);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Transform(delta_a_i, delta_b_i) => {
|
2021-08-05 14:52:19 +00:00
|
|
|
transform(
|
|
|
|
&mut self.documents[*delta_a_i],
|
|
|
|
&mut self.documents[*delta_b_i],
|
|
|
|
);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-05 14:52:19 +00:00
|
|
|
TestOp::Invert(delta_a_i, delta_b_i) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let delta_a = &self.documents[*delta_a_i].data();
|
|
|
|
let delta_b = &self.documents[*delta_b_i].data();
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("Invert: ");
|
|
|
|
log::debug!("a: {}", delta_a.to_json());
|
|
|
|
log::debug!("b: {}", delta_b.to_json());
|
2021-08-04 08:59:21 +00:00
|
|
|
|
|
|
|
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
|
|
|
let undo = b_prime.invert_delta(&delta_a);
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
let new_delta = delta_a.compose(&b_prime).unwrap();
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("new delta: {}", new_delta.to_json());
|
|
|
|
log::debug!("undo delta: {}", undo.to_json());
|
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
|
|
|
|
2021-08-05 07:05:20 +00:00
|
|
|
log::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);
|
2021-08-04 08:59:21 +00:00
|
|
|
|
2021-08-05 12:05:40 +00:00
|
|
|
self.documents[*delta_a_i].set_data(new_delta_after_undo);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-05 14:52:19 +00:00
|
|
|
TestOp::Undo(delta_i) => {
|
|
|
|
self.documents[*delta_i].undo();
|
|
|
|
},
|
|
|
|
TestOp::Redo(delta_i) => {
|
|
|
|
self.documents[*delta_i].redo();
|
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::AssertOpsJson(delta_i, expected) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let delta_i_json = self.documents[*delta_i].to_json();
|
2021-08-02 15:08:24 +00:00
|
|
|
|
2021-08-03 08:20:25 +00:00
|
|
|
let expected_delta: Delta = serde_json::from_str(expected).unwrap();
|
|
|
|
let target_delta: Delta = serde_json::from_str(&delta_i_json).unwrap();
|
|
|
|
|
|
|
|
if expected_delta != target_delta {
|
2021-08-03 15:13:20 +00:00
|
|
|
log::error!("✅ expect: {}", expected,);
|
|
|
|
log::error!("❌ receive: {}", delta_i_json);
|
2021-08-02 15:08:24 +00:00
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
assert_eq!(target_delta, expected_delta);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
pub fn run_script(&mut self, script: Vec<TestOp>) {
|
2021-08-03 08:20:25 +00:00
|
|
|
for (_i, op) in script.iter().enumerate() {
|
2021-08-02 10:35:25 +00:00
|
|
|
self.run_op(op);
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
pub fn debug_print_delta(delta: &Delta) {
|
2021-08-04 07:09:04 +00:00
|
|
|
eprintln!("😁 {}", serde_json::to_string(delta).unwrap());
|
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 {
|
|
|
|
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(|_| self.0.gen::<char>()).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
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::Empty);
|
|
|
|
},
|
|
|
|
f if f < 0.4 => {
|
|
|
|
delta.delete(i as u64);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
delta.retain(i as u64, Attributes::Empty);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.0.gen_range(0.0, 1.0) < 0.3 {
|
|
|
|
delta.insert(&("1".to_owned() + &self.gen_string(10)), Attributes::Empty);
|
|
|
|
}
|
|
|
|
delta
|
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|