2021-11-27 11:19:41 +00:00
|
|
|
#![allow(clippy::module_inception)]
|
2021-09-22 15:21:44 +00:00
|
|
|
mod attribute_test;
|
|
|
|
mod op_test;
|
|
|
|
mod serde_test;
|
|
|
|
mod undo_redo_test;
|
|
|
|
|
2022-03-26 12:27:32 +00:00
|
|
|
use derive_more::Display;
|
2022-03-19 08:52:28 +00:00
|
|
|
use flowy_sync::client_document::{ClientDocument, InitialDocumentText};
|
2021-12-07 11:59:08 +00:00
|
|
|
use lib_ot::{
|
|
|
|
core::*,
|
|
|
|
rich_text::{RichTextAttribute, RichTextAttributes, RichTextDelta},
|
|
|
|
};
|
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
|
|
|
|
2022-04-12 03:13:35 +00:00
|
|
|
const LEVEL: &str = "info";
|
2021-08-16 09:00:39 +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
|
|
|
|
2021-08-10 03:22:57 +00:00
|
|
|
#[display(fmt = "Replace")]
|
|
|
|
Replace(usize, Interval, &'static str),
|
|
|
|
|
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
|
|
|
|
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),
|
|
|
|
|
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-09-26 08:39:57 +00:00
|
|
|
#[display(fmt = "TransformPrime")]
|
|
|
|
TransformPrime(usize, usize),
|
|
|
|
|
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
|
|
|
|
2021-08-10 03:22:57 +00:00
|
|
|
#[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),
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 08:45:36 +00:00
|
|
|
pub struct TestBuilder {
|
2022-01-12 09:08:50 +00:00
|
|
|
documents: Vec<ClientDocument>,
|
2021-12-07 02:39:01 +00:00
|
|
|
deltas: Vec<Option<RichTextDelta>>,
|
|
|
|
primes: Vec<Option<RichTextDelta>>,
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 08:45:36 +00:00
|
|
|
impl TestBuilder {
|
2021-08-02 10:35:25 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2022-01-10 15:45:59 +00:00
|
|
|
let _ = color_eyre::install();
|
2021-08-16 09:00:39 +00:00
|
|
|
std::env::set_var("RUST_LOG", LEVEL);
|
2021-08-02 10:35:25 +00:00
|
|
|
});
|
|
|
|
|
2021-09-26 08:39:57 +00:00
|
|
|
Self {
|
|
|
|
documents: vec![],
|
|
|
|
deltas: vec![],
|
|
|
|
primes: vec![],
|
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 08:45:36 +00:00
|
|
|
fn run_op(&mut self, op: &TestOp) {
|
2021-11-03 07:37:38 +00:00
|
|
|
tracing::trace!("***************** 😈{} *******************", &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];
|
2021-09-26 08:39:57 +00:00
|
|
|
let delta = document.insert(*index, s).unwrap();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::debug!("Insert delta: {}", delta.to_delta_str());
|
2021-11-03 05:52:33 +00:00
|
|
|
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::trace!("Delete delta: {}", delta.to_delta_str());
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-16 07:02:57 +00:00
|
|
|
TestOp::Replace(delta_i, iv, s) => {
|
2021-08-10 03:22:57 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-09-26 08:39:57 +00:00
|
|
|
let delta = document.replace(*iv, s).unwrap();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::trace!("Replace delta: {}", delta.to_delta_str());
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +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-12-07 02:39:01 +00:00
|
|
|
document.format(*iv, RichTextAttribute::Bold(true)).unwrap();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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-12-07 02:39:01 +00:00
|
|
|
let attribute = RichTextAttribute::Bold(*enable);
|
2021-09-26 08:39:57 +00:00
|
|
|
let delta = document.format(*iv, attribute).unwrap();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::trace!("Bold delta: {}", delta.to_delta_str());
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +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-12-07 02:39:01 +00:00
|
|
|
true => RichTextAttribute::Italic(true),
|
|
|
|
false => RichTextAttribute::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();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::trace!("Italic delta: {}", delta.to_delta_str());
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +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-12-07 02:39:01 +00:00
|
|
|
let attribute = RichTextAttribute::Header(*level);
|
2021-09-26 08:39:57 +00:00
|
|
|
let delta = document.format(*iv, attribute).unwrap();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::trace!("Header delta: {}", delta.to_delta_str());
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +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-12-07 02:39:01 +00:00
|
|
|
let attribute = RichTextAttribute::Link(link.to_owned());
|
2021-09-26 08:39:57 +00:00
|
|
|
let delta = document.format(*iv, attribute).unwrap();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::trace!("Link delta: {}", delta.to_delta_str());
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-16 15:07:40 +00:00
|
|
|
TestOp::Bullet(delta_i, iv, enable) => {
|
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-12-07 02:39:01 +00:00
|
|
|
let attribute = RichTextAttribute::Bullet(*enable);
|
2021-09-26 08:39:57 +00:00
|
|
|
let delta = document.format(*iv, attribute).unwrap();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::debug!("Bullet delta: {}", delta.to_delta_str());
|
2021-11-03 05:52:33 +00:00
|
|
|
|
2021-09-26 08:39:57 +00:00
|
|
|
self.deltas.insert(*delta_i, Some(delta));
|
2022-01-23 04:14:00 +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]
|
2021-09-25 13:47:02 +00:00
|
|
|
.delta()
|
2022-01-24 09:35:58 +00:00
|
|
|
.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
|
|
|
|
2021-09-25 13:47:02 +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
|
|
|
|
2021-09-25 13:47:02 +00:00
|
|
|
self.documents[*delta_a_i].set_delta(data_left);
|
|
|
|
self.documents[*delta_b_i].set_delta(data_right);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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()
|
2022-01-24 09:35:58 +00:00
|
|
|
.transform(self.documents[*b_doc_index].delta())
|
2021-09-26 08:39:57 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
self.primes.insert(*a_doc_index, Some(prime_left));
|
|
|
|
self.primes.insert(*b_doc_index, Some(prime_right));
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-05 14:52:19 +00:00
|
|
|
TestOp::Invert(delta_a_i, delta_b_i) => {
|
2021-09-25 13:47:02 +00:00
|
|
|
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: ");
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::debug!("a: {}", delta_a.to_delta_str());
|
|
|
|
tracing::debug!("b: {}", delta_b.to_delta_str());
|
2021-08-04 08:59:21 +00:00
|
|
|
|
|
|
|
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
2022-01-24 09:35:58 +00:00
|
|
|
let undo = b_prime.invert(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();
|
2022-03-05 14:30:42 +00:00
|
|
|
tracing::debug!("new delta: {}", new_delta.to_delta_str());
|
|
|
|
tracing::debug!("undo delta: {}", undo.to_delta_str());
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
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 07:05:20 +00:00
|
|
|
|
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-09-25 13:47:02 +00:00
|
|
|
self.documents[*delta_a_i].set_delta(new_delta_after_undo);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-10 03:22:57 +00:00
|
|
|
TestOp::Wait(mills_sec) => {
|
|
|
|
std::thread::sleep(Duration::from_millis(*mills_sec as u64));
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-06 14:25:09 +00:00
|
|
|
|
2021-09-26 08:39:57 +00:00
|
|
|
TestOp::AssertDocJson(delta_i, expected) => {
|
2022-03-05 14:30:42 +00:00
|
|
|
let delta_json = self.documents[*delta_i].delta_str();
|
2021-12-07 02:39:01 +00:00
|
|
|
let expected_delta: RichTextDelta = serde_json::from_str(expected).unwrap();
|
|
|
|
let target_delta: RichTextDelta = 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);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-09-26 08:39:57 +00:00
|
|
|
|
|
|
|
TestOp::AssertPrimeJson(doc_i, expected) => {
|
2022-03-05 14:30:42 +00:00
|
|
|
let prime_json = self.primes[*doc_i].as_ref().unwrap().to_delta_str();
|
2021-12-07 02:39:01 +00:00
|
|
|
let expected_prime: RichTextDelta = serde_json::from_str(expected).unwrap();
|
|
|
|
let target_prime: RichTextDelta = serde_json::from_str(&prime_json).unwrap();
|
2021-09-26 08:39:57 +00:00
|
|
|
|
|
|
|
if expected_prime != target_prime {
|
|
|
|
log::error!("✅ expect prime: {}", expected,);
|
|
|
|
log::error!("❌ receive prime: {}", prime_json);
|
|
|
|
}
|
|
|
|
assert_eq!(target_prime, expected_prime);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-09-26 08:39:57 +00:00
|
|
|
TestOp::DocComposeDelta(doc_index, delta_i) => {
|
|
|
|
let delta = self.deltas.get(*delta_i).unwrap().as_ref().unwrap();
|
2021-11-12 13:44:26 +00:00
|
|
|
self.documents[*doc_index].compose_delta(delta.clone()).unwrap();
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
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);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:34:42 +00:00
|
|
|
pub fn run_scripts<C: InitialDocumentText>(mut self, scripts: Vec<TestOp>) {
|
2022-01-12 09:08:50 +00:00
|
|
|
self.documents = vec![ClientDocument::new::<C>(), ClientDocument::new::<C>()];
|
2021-09-26 08:39:57 +00:00
|
|
|
self.primes = vec![None, None];
|
|
|
|
self.deltas = vec![None, None];
|
2021-12-09 03:00:05 +00:00
|
|
|
for (_i, op) in scripts.iter().enumerate() {
|
2021-08-02 10:35:25 +00:00
|
|
|
self.run_op(op);
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Rng(StdRng);
|
|
|
|
|
|
|
|
impl Default for Rng {
|
2022-01-23 04:14:00 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Rng(StdRng::from_rng(thread_rng()).unwrap())
|
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng {
|
2021-09-23 07:49:10 +00:00
|
|
|
#[allow(dead_code)]
|
2022-01-23 04:14:00 +00:00
|
|
|
pub fn from_seed(seed: [u8; 32]) -> Self {
|
|
|
|
Rng(StdRng::from_seed(seed))
|
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
|
2021-11-19 05:13:50 +00:00
|
|
|
pub fn gen_string(&mut self, len: usize) -> String {
|
|
|
|
(0..len)
|
|
|
|
.map(|_| {
|
|
|
|
let c = self.0.gen::<char>();
|
|
|
|
format!("{:x}", c as u32)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
|
2021-12-07 02:39:01 +00:00
|
|
|
pub fn gen_delta(&mut self, s: &str) -> RichTextDelta {
|
|
|
|
let mut delta = RichTextDelta::default();
|
2022-01-08 10:21:13 +00:00
|
|
|
let s = FlowyStr::from(s);
|
2021-08-03 08:20:25 +00:00
|
|
|
loop {
|
2022-01-08 10:21:13 +00:00
|
|
|
let left = s.utf16_size() - delta.utf16_base_len;
|
2021-08-03 08:20:25 +00:00
|
|
|
if left == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let i = if left == 1 {
|
|
|
|
1
|
|
|
|
} else {
|
2022-04-28 13:53:28 +00:00
|
|
|
1 + self.0.gen_range(0..std::cmp::min(left - 1, 20))
|
2021-08-03 08:20:25 +00:00
|
|
|
};
|
2022-04-28 13:53:28 +00:00
|
|
|
match self.0.gen_range(0.0..1.0) {
|
2021-08-03 08:20:25 +00:00
|
|
|
f if f < 0.2 => {
|
2021-12-07 02:39:01 +00:00
|
|
|
delta.insert(&self.gen_string(i), RichTextAttributes::default());
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
f if f < 0.4 => {
|
2021-08-06 15:06:27 +00:00
|
|
|
delta.delete(i);
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
_ => {
|
2021-12-07 02:39:01 +00:00
|
|
|
delta.retain(i, RichTextAttributes::default());
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 13:53:28 +00:00
|
|
|
if self.0.gen_range(0.0..1.0) < 0.3 {
|
2021-12-07 02:39:01 +00:00
|
|
|
delta.insert(&("1".to_owned() + &self.gen_string(10)), RichTextAttributes::default());
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
delta
|
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|