2021-08-01 08:39:32 +00:00
|
|
|
pub mod helper;
|
|
|
|
|
2021-07-31 12:53:45 +00:00
|
|
|
use bytecount::num_chars;
|
|
|
|
use flowy_ot::{
|
2021-07-31 15:22:17 +00:00
|
|
|
attributes::*,
|
2021-07-31 12:53:45 +00:00
|
|
|
delta::Delta,
|
2021-08-01 02:45:15 +00:00
|
|
|
operation::{OpBuilder, Operation},
|
2021-07-31 12:53:45 +00:00
|
|
|
};
|
2021-08-01 08:39:32 +00:00
|
|
|
use helper::*;
|
2021-07-31 12:53:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lengths() {
|
|
|
|
let mut delta = Delta::default();
|
|
|
|
assert_eq!(delta.base_len, 0);
|
|
|
|
assert_eq!(delta.target_len, 0);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(5, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.base_len, 5);
|
|
|
|
assert_eq!(delta.target_len, 5);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.insert("abc", None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.base_len, 5);
|
|
|
|
assert_eq!(delta.target_len, 8);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(2, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.base_len, 7);
|
|
|
|
assert_eq!(delta.target_len, 10);
|
|
|
|
delta.delete(2);
|
|
|
|
assert_eq!(delta.base_len, 9);
|
|
|
|
assert_eq!(delta.target_len, 10);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn sequence() {
|
|
|
|
let mut delta = Delta::default();
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(5, None);
|
|
|
|
delta.retain(0, None);
|
2021-08-01 02:45:15 +00:00
|
|
|
delta.insert("appflowy", None);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.insert("", None);
|
2021-07-31 12:53:45 +00:00
|
|
|
delta.delete(3);
|
|
|
|
delta.delete(0);
|
|
|
|
assert_eq!(delta.ops.len(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 15:22:17 +00:00
|
|
|
fn apply_1000() {
|
2021-07-31 12:53:45 +00:00
|
|
|
for _ in 0..1000 {
|
|
|
|
let mut rng = Rng::default();
|
|
|
|
let s = rng.gen_string(50);
|
|
|
|
let delta = rng.gen_delta(&s);
|
|
|
|
assert_eq!(num_chars(s.as_bytes()), delta.base_len);
|
|
|
|
assert_eq!(delta.apply(&s).unwrap().chars().count(), delta.target_len);
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 15:22:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn apply() {
|
|
|
|
let s = "hello world,".to_owned();
|
|
|
|
let mut delta_a = Delta::default();
|
|
|
|
delta_a.insert(&s, None);
|
|
|
|
|
|
|
|
let mut delta_b = Delta::default();
|
|
|
|
delta_b.retain(s.len() as u64, None);
|
|
|
|
delta_b.insert("appflowy", None);
|
|
|
|
|
|
|
|
let after_a = delta_a.apply("").unwrap();
|
|
|
|
let after_b = delta_b.apply(&after_a).unwrap();
|
|
|
|
assert_eq!("hello world,appflowy", &after_b);
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:45:15 +00:00
|
|
|
#[test]
|
|
|
|
fn base_len_test() {
|
|
|
|
let mut delta_a = Delta::default();
|
|
|
|
delta_a.insert("a", None);
|
|
|
|
delta_a.insert("b", None);
|
|
|
|
delta_a.insert("c", None);
|
|
|
|
|
|
|
|
let s = "hello world,".to_owned();
|
|
|
|
delta_a.delete(s.len() as u64);
|
|
|
|
let after_a = delta_a.apply(&s).unwrap();
|
|
|
|
|
|
|
|
delta_a.insert("d", None);
|
|
|
|
assert_eq!("abc", &after_a);
|
|
|
|
}
|
|
|
|
|
2021-07-31 12:53:45 +00:00
|
|
|
#[test]
|
|
|
|
fn invert() {
|
|
|
|
for _ in 0..1000 {
|
|
|
|
let mut rng = Rng::default();
|
|
|
|
let s = rng.gen_string(50);
|
|
|
|
let delta_a = rng.gen_delta(&s);
|
|
|
|
let delta_b = delta_a.invert(&s);
|
|
|
|
assert_eq!(delta_a.base_len, delta_b.target_len);
|
|
|
|
assert_eq!(delta_a.target_len, delta_b.base_len);
|
|
|
|
assert_eq!(delta_b.apply(&delta_a.apply(&s).unwrap()).unwrap(), s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn empty_ops() {
|
|
|
|
let mut delta = Delta::default();
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(0, None);
|
|
|
|
delta.insert("", None);
|
2021-07-31 12:53:45 +00:00
|
|
|
delta.delete(0);
|
|
|
|
assert_eq!(delta.ops.len(), 0);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn eq() {
|
|
|
|
let mut delta_a = Delta::default();
|
|
|
|
delta_a.delete(1);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta_a.insert("lo", None);
|
|
|
|
delta_a.retain(2, None);
|
|
|
|
delta_a.retain(3, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
let mut delta_b = Delta::default();
|
|
|
|
delta_b.delete(1);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta_b.insert("l", None);
|
|
|
|
delta_b.insert("o", None);
|
|
|
|
delta_b.retain(5, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta_a, delta_b);
|
|
|
|
delta_a.delete(1);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta_b.retain(1, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_ne!(delta_a, delta_b);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn ops_merging() {
|
|
|
|
let mut delta = Delta::default();
|
|
|
|
assert_eq!(delta.ops.len(), 0);
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(2, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 1);
|
2021-08-01 02:45:15 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::retain(2).build()));
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(3, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 1);
|
2021-08-01 02:45:15 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::retain(5).build()));
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.insert("abc", None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 2);
|
2021-08-01 08:39:32 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::insert("abc").build()));
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.insert("xyz", None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 2);
|
2021-08-01 08:39:32 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::insert("abcxyz").build()));
|
2021-07-31 12:53:45 +00:00
|
|
|
delta.delete(1);
|
|
|
|
assert_eq!(delta.ops.len(), 3);
|
2021-08-01 02:45:15 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::delete(1).build()));
|
2021-07-31 12:53:45 +00:00
|
|
|
delta.delete(1);
|
|
|
|
assert_eq!(delta.ops.len(), 3);
|
2021-08-01 02:45:15 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::delete(2).build()));
|
2021-07-31 12:53:45 +00:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn is_noop() {
|
|
|
|
let mut delta = Delta::default();
|
|
|
|
assert!(delta.is_noop());
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(5, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert!(delta.is_noop());
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.retain(3, None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert!(delta.is_noop());
|
2021-07-31 15:22:17 +00:00
|
|
|
delta.insert("lorem", None);
|
2021-07-31 12:53:45 +00:00
|
|
|
assert!(!delta.is_noop());
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn compose() {
|
|
|
|
for _ in 0..1000 {
|
|
|
|
let mut rng = Rng::default();
|
|
|
|
let s = rng.gen_string(20);
|
|
|
|
let a = rng.gen_delta(&s);
|
|
|
|
let after_a = a.apply(&s).unwrap();
|
|
|
|
assert_eq!(a.target_len, num_chars(after_a.as_bytes()));
|
|
|
|
|
|
|
|
let b = rng.gen_delta(&after_a);
|
|
|
|
let after_b = b.apply(&after_a).unwrap();
|
|
|
|
assert_eq!(b.target_len, num_chars(after_b.as_bytes()));
|
|
|
|
|
|
|
|
let ab = a.compose(&b).unwrap();
|
|
|
|
assert_eq!(ab.target_len, b.target_len);
|
|
|
|
let after_ab = ab.apply(&s).unwrap();
|
|
|
|
assert_eq!(after_b, after_ab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn transform() {
|
|
|
|
for _ in 0..1000 {
|
|
|
|
let mut rng = Rng::default();
|
|
|
|
let s = rng.gen_string(20);
|
|
|
|
let a = rng.gen_delta(&s);
|
|
|
|
let b = rng.gen_delta(&s);
|
|
|
|
let (a_prime, b_prime) = a.transform(&b).unwrap();
|
|
|
|
let ab_prime = a.compose(&b_prime).unwrap();
|
|
|
|
let ba_prime = b.compose(&a_prime).unwrap();
|
|
|
|
let after_ab_prime = ab_prime.apply(&s).unwrap();
|
|
|
|
let after_ba_prime = ba_prime.apply(&s).unwrap();
|
|
|
|
assert_eq!(ab_prime, ba_prime);
|
|
|
|
assert_eq!(after_ab_prime, after_ba_prime);
|
|
|
|
}
|
|
|
|
}
|