AppFlowy/rust-lib/flowy-ot/tests/helper/mod.rs

255 lines
8.4 KiB
Rust
Raw Normal View History

use derive_more::Display;
2021-08-11 09:18:10 +00:00
use flowy_ot::{
client::Document,
core::{REMOVE_FLAG, *},
};
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
#[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")]
Header(usize, Interval, usize, bool),
#[display(fmt = "Transform")]
Transform(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),
#[display(fmt = "AssertOpsJson")]
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-04 07:09:04 +00:00
impl OpTester {
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-15 13:11:48 +00:00
std::env::set_var("RUST_LOG", "debug");
env_logger::init();
});
2021-08-11 15:34:35 +00:00
Self { documents: vec![] }
}
2021-08-04 07:09:04 +00:00
pub fn run_op(&mut self, op: &TestOp) {
log::debug!("***************** 😈{} *******************", &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-08-14 16:05:18 +00:00
document.insert(*index, s).unwrap();
},
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.replace(*interval, "").unwrap();
},
TestOp::Replace(delta_i, interval, s) => {
let document = &mut self.documents[*delta_i];
document.replace(*interval, s).unwrap();
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];
2021-08-14 16:05:18 +00:00
document.insert(interval.start, s).unwrap();
2021-08-11 09:18:10 +00:00
document
2021-08-15 13:11:48 +00:00
.format(*interval, AttributeKey::Bold.value(true))
2021-08-11 09:18:10 +00:00
.unwrap();
},
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];
2021-08-11 09:18:10 +00:00
let attribute = match *enable {
2021-08-15 13:11:48 +00:00
true => AttributeKey::Bold.value(true),
false => AttributeKey::Bold.remove(),
2021-08-11 09:18:10 +00:00
};
document.format(*interval, attribute).unwrap();
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];
2021-08-11 09:18:10 +00:00
let attribute = match *enable {
2021-08-15 13:11:48 +00:00
true => AttributeKey::Italic.value("true"),
false => AttributeKey::Italic.remove(),
};
document.format(*interval, attribute).unwrap();
},
TestOp::Header(delta_i, interval, level, enable) => {
let document = &mut self.documents[*delta_i];
let attribute = match *enable {
true => AttributeKey::Header.value(level),
false => AttributeKey::Header.remove(),
2021-08-11 09:18:10 +00:00
};
document.format(*interval, attribute).unwrap();
},
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]
.data()
.transform(&self.documents[*delta_b_i].data())
.unwrap();
log::trace!("a:{:?},b:{:?}", a_prime, b_prime);
let data_left = self.documents[*delta_a_i].data().compose(&b_prime).unwrap();
let data_right = self.documents[*delta_b_i].data().compose(&a_prime).unwrap();
self.documents[*delta_a_i].set_data(data_left);
self.documents[*delta_b_i].set_data(data_right);
},
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();
log::debug!("Invert: ");
log::debug!("a: {}", delta_a.to_json());
log::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();
log::debug!("new delta: {}", new_delta.to_json());
log::debug!("undo delta: {}", undo.to_json());
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
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-05 12:05:40 +00:00
self.documents[*delta_a_i].set_data(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) => {
assert_eq!(&self.documents[*delta_i].to_string(), expected);
},
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-04 07:09:04 +00:00
pub fn run_script(&mut self, script: Vec<TestOp>) {
2021-08-11 15:34:35 +00:00
let delta = Delta::new();
self.run(script, delta);
}
pub fn run_script_with_newline(&mut self, script: Vec<TestOp>) {
let mut delta = Delta::new();
delta.insert("\n", Attributes::default());
self.run(script, delta);
}
fn run(&mut self, script: Vec<TestOp>, delta: Delta) {
let mut documents = Vec::with_capacity(2);
for _ in 0..2 {
documents.push(Document::from_delta(delta.clone()));
}
self.documents = documents;
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 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::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 {
delta.insert(
&("1".to_owned() + &self.gen_string(10)),
Attributes::default(),
);
2021-08-03 08:20:25 +00:00
}
delta
}
}