2021-08-01 08:39:32 +00:00
|
|
|
pub mod helper;
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
use crate::helper::TestOp::*;
|
2021-07-31 12:53:45 +00:00
|
|
|
use bytecount::num_chars;
|
2021-08-03 23:55:54 +00:00
|
|
|
use flowy_ot::core::*;
|
2021-08-01 08:39:32 +00:00
|
|
|
use helper::*;
|
2021-07-31 12:53:45 +00:00
|
|
|
|
2021-08-10 03:22:57 +00:00
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_1() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("123").build();
|
|
|
|
let insert_b = OpBuilder::insert("4").build();
|
2021-08-10 03:22:57 +00:00
|
|
|
|
|
|
|
delta.add(insert_a.clone());
|
|
|
|
delta.add(insert_b.clone());
|
|
|
|
|
2021-08-11 03:42:46 +00:00
|
|
|
let mut iterator = DeltaIter::from_interval(&delta, Interval::new(0, 4));
|
2021-08-11 00:40:58 +00:00
|
|
|
assert_eq!(iterator.ops(), delta.ops);
|
2021-08-10 03:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_2() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("123").build();
|
|
|
|
let insert_b = OpBuilder::insert("4").build();
|
|
|
|
let insert_c = OpBuilder::insert("5").build();
|
|
|
|
let retain_a = OpBuilder::retain(3).build();
|
2021-08-10 03:22:57 +00:00
|
|
|
|
|
|
|
delta.add(insert_a.clone());
|
|
|
|
delta.add(retain_a.clone());
|
|
|
|
delta.add(insert_b.clone());
|
|
|
|
delta.add(insert_c.clone());
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(0, 2)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![OpBuilder::insert("12").build()]
|
2021-08-10 03:22:57 +00:00
|
|
|
);
|
|
|
|
|
2021-08-13 06:13:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
DeltaIter::from_interval(&delta, Interval::new(1, 3)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![OpBuilder::insert("23").build()]
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
|
|
|
|
2021-08-10 03:22:57 +00:00
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(0, 3)).ops(),
|
2021-08-10 03:22:57 +00:00
|
|
|
vec![insert_a.clone()]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(0, 4)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![insert_a.clone(), OpBuilder::retain(1).build()]
|
2021-08-10 03:22:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(0, 6)).ops(),
|
2021-08-10 03:22:57 +00:00
|
|
|
vec![insert_a.clone(), retain_a.clone()]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(0, 7)).ops(),
|
2021-08-10 03:22:57 +00:00
|
|
|
vec![insert_a.clone(), retain_a.clone(), insert_b.clone()]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_3() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("123456").build();
|
2021-08-10 03:22:57 +00:00
|
|
|
delta.add(insert_a.clone());
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(3, 5)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![OpBuilder::insert("45").build()]
|
2021-08-10 03:22:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_4() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("12").build();
|
|
|
|
let insert_b = OpBuilder::insert("34").build();
|
|
|
|
let insert_c = OpBuilder::insert("56").build();
|
2021-08-10 03:22:57 +00:00
|
|
|
|
|
|
|
delta.ops.push(insert_a.clone());
|
|
|
|
delta.ops.push(insert_b.clone());
|
|
|
|
delta.ops.push(insert_c.clone());
|
|
|
|
|
2021-08-11 00:40:58 +00:00
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(0, 2)).ops(),
|
2021-08-11 00:40:58 +00:00
|
|
|
vec![insert_a]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(2, 4)).ops(),
|
2021-08-11 00:40:58 +00:00
|
|
|
vec![insert_b]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(4, 6)).ops(),
|
2021-08-11 00:40:58 +00:00
|
|
|
vec![insert_c]
|
|
|
|
);
|
2021-08-10 03:22:57 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(2, 5)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![
|
|
|
|
OpBuilder::insert("34").build(),
|
|
|
|
OpBuilder::insert("5").build()
|
|
|
|
]
|
2021-08-10 03:22:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_5() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("123456").build();
|
|
|
|
let insert_b = OpBuilder::insert("789").build();
|
2021-08-10 03:22:57 +00:00
|
|
|
delta.ops.push(insert_a.clone());
|
|
|
|
delta.ops.push(insert_b.clone());
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(4, 8)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![
|
|
|
|
OpBuilder::insert("56").build(),
|
|
|
|
OpBuilder::insert("78").build()
|
|
|
|
]
|
2021-08-10 03:22:57 +00:00
|
|
|
);
|
|
|
|
|
2021-08-13 06:13:31 +00:00
|
|
|
// assert_eq!(
|
|
|
|
// DeltaIter::from_interval(&delta, Interval::new(8, 9)).ops(),
|
|
|
|
// vec![Builder::insert("9").build()]
|
|
|
|
// );
|
2021-08-10 03:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_6() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("12345678").build();
|
2021-08-10 03:22:57 +00:00
|
|
|
delta.add(insert_a.clone());
|
|
|
|
assert_eq!(
|
2021-08-11 03:42:46 +00:00
|
|
|
DeltaIter::from_interval(&delta, Interval::new(4, 6)).ops(),
|
2021-08-13 10:16:52 +00:00
|
|
|
vec![OpBuilder::insert("56").build()]
|
2021-08-10 03:22:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-13 06:13:31 +00:00
|
|
|
#[test]
|
|
|
|
fn delta_get_ops_in_interval_7() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("12345").build();
|
|
|
|
let retain_a = OpBuilder::retain(3).build();
|
2021-08-13 06:13:31 +00:00
|
|
|
|
|
|
|
delta.add(insert_a.clone());
|
|
|
|
delta.add(retain_a.clone());
|
|
|
|
|
|
|
|
let mut iter_1 = DeltaIter::new(&delta);
|
2021-08-14 08:44:39 +00:00
|
|
|
iter_1.seek::<CharMetric>(2);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(iter_1.next_op().unwrap(), OpBuilder::insert("345").build());
|
|
|
|
assert_eq!(iter_1.next_op().unwrap(), OpBuilder::retain(3).build());
|
2021-08-13 06:13:31 +00:00
|
|
|
|
|
|
|
let mut iter_2 = DeltaIter::new(&delta);
|
|
|
|
assert_eq!(
|
2021-08-13 08:39:32 +00:00
|
|
|
iter_2.next_op_with_len(2).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("12").build()
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(iter_2.next_op().unwrap(), OpBuilder::insert("345").build());
|
2021-08-13 06:13:31 +00:00
|
|
|
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(iter_2.next_op().unwrap(), OpBuilder::retain(3).build());
|
2021-08-13 06:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_seek_1() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
let insert_a = OpBuilder::insert("12345").build();
|
|
|
|
let retain_a = OpBuilder::retain(3).build();
|
2021-08-13 06:13:31 +00:00
|
|
|
delta.add(insert_a.clone());
|
|
|
|
delta.add(retain_a.clone());
|
|
|
|
let mut iter = DeltaIter::new(&delta);
|
2021-08-14 08:44:39 +00:00
|
|
|
iter.seek::<OpMetric>(1);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(iter.next_op().unwrap(), OpBuilder::retain(3).build());
|
2021-08-13 06:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_seek_2() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
delta.add(OpBuilder::insert("12345").build());
|
2021-08-13 06:13:31 +00:00
|
|
|
|
|
|
|
let mut iter = DeltaIter::new(&delta);
|
|
|
|
assert_eq!(
|
2021-08-13 08:39:32 +00:00
|
|
|
iter.next_op_with_len(1).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("1").build()
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_seek_3() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
delta.add(OpBuilder::insert("12345").build());
|
2021-08-13 06:13:31 +00:00
|
|
|
|
|
|
|
let mut iter = DeltaIter::new(&delta);
|
|
|
|
assert_eq!(
|
2021-08-13 08:39:32 +00:00
|
|
|
iter.next_op_with_len(2).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("12").build()
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-13 08:39:32 +00:00
|
|
|
iter.next_op_with_len(2).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("34").build()
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-08-13 08:39:32 +00:00
|
|
|
iter.next_op_with_len(2).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("5").build()
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
|
|
|
|
2021-08-13 08:39:32 +00:00
|
|
|
assert_eq!(iter.next_op_with_len(1), None);
|
2021-08-13 06:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_seek_4() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
delta.add(OpBuilder::insert("12345").build());
|
2021-08-13 06:13:31 +00:00
|
|
|
|
|
|
|
let mut iter = DeltaIter::new(&delta);
|
|
|
|
iter.seek::<CharMetric>(3);
|
|
|
|
assert_eq!(
|
2021-08-13 08:39:32 +00:00
|
|
|
iter.next_op_with_len(2).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("45").build()
|
2021-08-13 06:13:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-13 08:39:32 +00:00
|
|
|
#[test]
|
|
|
|
fn delta_next_op_len_test() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
delta.add(OpBuilder::insert("12345").build());
|
2021-08-13 08:39:32 +00:00
|
|
|
|
|
|
|
let mut iter = DeltaIter::new(&delta);
|
|
|
|
iter.seek::<CharMetric>(3);
|
2021-08-14 08:44:39 +00:00
|
|
|
assert_eq!(iter.next_op_len().unwrap(), 2);
|
2021-08-13 08:39:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
iter.next_op_with_len(1).unwrap(),
|
2021-08-13 10:16:52 +00:00
|
|
|
OpBuilder::insert("4").build()
|
2021-08-13 08:39:32 +00:00
|
|
|
);
|
2021-08-14 08:44:39 +00:00
|
|
|
assert_eq!(iter.next_op_len().unwrap(), 1);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(iter.next_op().unwrap(), OpBuilder::insert("5").build());
|
2021-08-13 08:39:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 08:44:39 +00:00
|
|
|
#[test]
|
|
|
|
fn delta_next_op_len_test2() {
|
|
|
|
let mut delta = Delta::default();
|
|
|
|
delta.add(OpBuilder::insert("12345").build());
|
|
|
|
let mut iter = DeltaIter::new(&delta);
|
|
|
|
|
|
|
|
assert_eq!(iter.next_op_len().unwrap(), 5);
|
|
|
|
assert_eq!(
|
|
|
|
iter.next_op_with_len(5).unwrap(),
|
|
|
|
OpBuilder::insert("12345").build()
|
|
|
|
);
|
|
|
|
assert_eq!(iter.next_op_len(), None);
|
|
|
|
}
|
|
|
|
|
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-08-10 06:24:41 +00:00
|
|
|
delta.retain(5, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.base_len, 5);
|
|
|
|
assert_eq!(delta.target_len, 5);
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.insert("abc", Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.base_len, 5);
|
|
|
|
assert_eq!(delta.target_len, 8);
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.retain(2, Attributes::default());
|
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-08-10 06:24:41 +00:00
|
|
|
delta.retain(5, Attributes::default());
|
|
|
|
delta.retain(0, Attributes::default());
|
|
|
|
delta.insert("appflowy", Attributes::default());
|
|
|
|
delta.insert("", Attributes::default());
|
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();
|
2021-08-10 06:24:41 +00:00
|
|
|
delta_a.insert(&s, Attributes::default());
|
2021-07-31 15:22:17 +00:00
|
|
|
|
|
|
|
let mut delta_b = Delta::default();
|
2021-08-10 06:24:41 +00:00
|
|
|
delta_b.retain(s.len(), Attributes::default());
|
|
|
|
delta_b.insert("appflowy", Attributes::default());
|
2021-07-31 15:22:17 +00:00
|
|
|
|
|
|
|
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();
|
2021-08-10 06:24:41 +00:00
|
|
|
delta_a.insert("a", Attributes::default());
|
|
|
|
delta_a.insert("b", Attributes::default());
|
|
|
|
delta_a.insert("c", Attributes::default());
|
2021-08-01 02:45:15 +00:00
|
|
|
|
|
|
|
let s = "hello world,".to_owned();
|
2021-08-06 15:06:27 +00:00
|
|
|
delta_a.delete(s.len());
|
2021-08-01 02:45:15 +00:00
|
|
|
let after_a = delta_a.apply(&s).unwrap();
|
|
|
|
|
2021-08-10 06:24:41 +00:00
|
|
|
delta_a.insert("d", Attributes::default());
|
2021-08-01 02:45:15 +00:00
|
|
|
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);
|
2021-08-06 14:25:09 +00:00
|
|
|
let delta_b = delta_a.invert_str(&s);
|
2021-07-31 12:53:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
|
2021-07-31 12:53:45 +00:00
|
|
|
#[test]
|
|
|
|
fn empty_ops() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.retain(0, Attributes::default());
|
|
|
|
delta.insert("", Attributes::default());
|
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-08-10 06:24:41 +00:00
|
|
|
delta_a.insert("lo", Attributes::default());
|
|
|
|
delta_a.retain(2, Attributes::default());
|
|
|
|
delta_a.retain(3, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
let mut delta_b = Delta::default();
|
|
|
|
delta_b.delete(1);
|
2021-08-10 06:24:41 +00:00
|
|
|
delta_b.insert("l", Attributes::default());
|
|
|
|
delta_b.insert("o", Attributes::default());
|
|
|
|
delta_b.retain(5, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta_a, delta_b);
|
|
|
|
delta_a.delete(1);
|
2021-08-10 06:24:41 +00:00
|
|
|
delta_b.retain(1, Attributes::default());
|
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-08-10 06:24:41 +00:00
|
|
|
delta.retain(2, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 1);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::retain(2).build()));
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.retain(3, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 1);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::retain(5).build()));
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.insert("abc", Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 2);
|
2021-08-13 10:16:52 +00:00
|
|
|
assert_eq!(delta.ops.last(), Some(&OpBuilder::insert("abc").build()));
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.insert("xyz", Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert_eq!(delta.ops.len(), 2);
|
2021-08-13 10:16:52 +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-13 10:16:52 +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-13 10:16:52 +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-08-10 06:24:41 +00:00
|
|
|
delta.retain(5, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert!(delta.is_noop());
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.retain(3, Attributes::default());
|
2021-07-31 12:53:45 +00:00
|
|
|
assert!(delta.is_noop());
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.insert("lorem", Attributes::default());
|
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();
|
|
|
|
assert_eq!(ab_prime, ba_prime);
|
2021-08-02 15:08:24 +00:00
|
|
|
|
2021-08-03 08:20:25 +00:00
|
|
|
let after_ab_prime = ab_prime.apply(&s).unwrap();
|
|
|
|
let after_ba_prime = ba_prime.apply(&s).unwrap();
|
|
|
|
assert_eq!(after_ab_prime, after_ba_prime);
|
2021-07-31 12:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transform2() {
|
|
|
|
let ops = vec![
|
2021-08-03 15:13:20 +00:00
|
|
|
Insert(0, "123", 0),
|
|
|
|
Insert(1, "456", 0),
|
2021-08-02 10:35:25 +00:00
|
|
|
Transform(0, 1),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123456"}]"#),
|
|
|
|
AssertOpsJson(1, r#"[{"insert":"123456"}]"#),
|
|
|
|
];
|
2021-08-04 07:09:04 +00:00
|
|
|
OpTester::new().run_script(ops);
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_transform_test() {
|
|
|
|
let mut a = Delta::default();
|
|
|
|
let mut a_s = String::new();
|
2021-08-03 08:20:25 +00:00
|
|
|
a.insert("123", AttrsBuilder::new().bold(true).build());
|
2021-08-02 10:35:25 +00:00
|
|
|
a_s = a.apply(&a_s).unwrap();
|
2021-08-02 15:08:24 +00:00
|
|
|
assert_eq!(&a_s, "123");
|
2021-08-02 10:35:25 +00:00
|
|
|
|
|
|
|
let mut b = Delta::default();
|
|
|
|
let mut b_s = String::new();
|
2021-08-10 06:24:41 +00:00
|
|
|
b.insert("456", Attributes::default());
|
2021-08-02 15:08:24 +00:00
|
|
|
b_s = b.apply(&b_s).unwrap();
|
|
|
|
assert_eq!(&b_s, "456");
|
2021-08-02 10:35:25 +00:00
|
|
|
|
|
|
|
let (a_prime, b_prime) = a.transform(&b).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
r#"[{"insert":"123","attributes":{"bold":"true"}},{"retain":3}]"#,
|
|
|
|
serde_json::to_string(&a_prime).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
r#"[{"retain":3,"attributes":{"bold":"true"}},{"insert":"456"}]"#,
|
|
|
|
serde_json::to_string(&b_prime).unwrap()
|
|
|
|
);
|
|
|
|
}
|
2021-08-11 00:40:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_no_attribute_delta() {
|
|
|
|
let mut delta = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
delta.add(OpBuilder::insert("123").build());
|
2021-08-11 00:40:58 +00:00
|
|
|
|
|
|
|
let mut change = Delta::default();
|
2021-08-13 10:16:52 +00:00
|
|
|
change.add(OpBuilder::retain(3).build());
|
|
|
|
change.add(OpBuilder::insert("456").build());
|
2021-08-11 00:40:58 +00:00
|
|
|
let undo = change.invert(&delta);
|
|
|
|
|
|
|
|
let new_delta = delta.compose(&change).unwrap();
|
|
|
|
let delta_after_undo = new_delta.compose(&undo).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(delta_after_undo, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_no_attribute_delta2() {
|
|
|
|
let ops = vec![
|
|
|
|
Insert(0, "123", 0),
|
|
|
|
Insert(1, "4567", 0),
|
|
|
|
Invert(0, 1),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
|
|
|
|
];
|
|
|
|
OpTester::new().run_script(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_attribute_delta_with_no_attribute_delta() {
|
|
|
|
let ops = vec![
|
|
|
|
Insert(0, "123", 0),
|
|
|
|
Bold(0, Interval::new(0, 3), true),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#),
|
|
|
|
Insert(1, "4567", 0),
|
|
|
|
Invert(0, 1),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#),
|
|
|
|
];
|
|
|
|
OpTester::new().run_script(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_attribute_delta_with_no_attribute_delta2() {
|
|
|
|
let ops = vec![
|
|
|
|
Insert(0, "123", 0),
|
|
|
|
Bold(0, Interval::new(0, 3), true),
|
|
|
|
Insert(0, "456", 3),
|
|
|
|
AssertOpsJson(
|
|
|
|
0,
|
|
|
|
r#"[
|
|
|
|
{"insert":"123456","attributes":{"bold":"true"}}]
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
Italic(0, Interval::new(2, 4), true),
|
|
|
|
AssertOpsJson(
|
|
|
|
0,
|
|
|
|
r#"[
|
|
|
|
{"insert":"12","attributes":{"bold":"true"}},
|
|
|
|
{"insert":"34","attributes":{"bold":"true","italic":"true"}},
|
|
|
|
{"insert":"56","attributes":{"bold":"true"}}
|
|
|
|
]"#,
|
|
|
|
),
|
|
|
|
Insert(1, "abc", 0),
|
|
|
|
Invert(0, 1),
|
|
|
|
AssertOpsJson(
|
|
|
|
0,
|
|
|
|
r#"[
|
|
|
|
{"insert":"12","attributes":{"bold":"true"}},
|
|
|
|
{"insert":"34","attributes":{"bold":"true","italic":"true"}},
|
|
|
|
{"insert":"56","attributes":{"bold":"true"}}
|
|
|
|
]"#,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
OpTester::new().run_script(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_no_attribute_delta_with_attribute_delta() {
|
|
|
|
let ops = vec![
|
|
|
|
Insert(0, "123", 0),
|
|
|
|
Insert(1, "4567", 0),
|
|
|
|
Bold(1, Interval::new(0, 3), true),
|
|
|
|
AssertOpsJson(
|
|
|
|
1,
|
|
|
|
r#"[{"insert":"456","attributes":{"bold":"true"}},{"insert":"7"}]"#,
|
|
|
|
),
|
|
|
|
Invert(0, 1),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
|
|
|
|
];
|
|
|
|
OpTester::new().run_script(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_no_attribute_delta_with_attribute_delta2() {
|
|
|
|
let ops = vec![
|
|
|
|
Insert(0, "123", 0),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
|
|
|
|
Insert(1, "abc", 0),
|
|
|
|
Bold(1, Interval::new(0, 3), true),
|
|
|
|
Insert(1, "d", 3),
|
|
|
|
Italic(1, Interval::new(1, 3), true),
|
|
|
|
AssertOpsJson(
|
|
|
|
1,
|
|
|
|
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":
|
|
|
|
{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"
|
|
|
|
}}]"#,
|
|
|
|
),
|
|
|
|
Invert(0, 1),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
|
|
|
|
];
|
|
|
|
OpTester::new().run_script(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delta_invert_attribute_delta_with_attribute_delta() {
|
|
|
|
let ops = vec![
|
|
|
|
Insert(0, "123", 0),
|
|
|
|
Bold(0, Interval::new(0, 3), true),
|
|
|
|
Insert(0, "456", 3),
|
|
|
|
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
|
|
|
|
Italic(0, Interval::new(2, 4), true),
|
|
|
|
AssertOpsJson(
|
|
|
|
0,
|
|
|
|
r#"[
|
|
|
|
{"insert":"12","attributes":{"bold":"true"}},
|
|
|
|
{"insert":"34","attributes":{"bold":"true","italic":"true"}},
|
|
|
|
{"insert":"56","attributes":{"bold":"true"}}
|
|
|
|
]"#,
|
|
|
|
),
|
|
|
|
Insert(1, "abc", 0),
|
|
|
|
Bold(1, Interval::new(0, 3), true),
|
|
|
|
Insert(1, "d", 3),
|
|
|
|
Italic(1, Interval::new(1, 3), true),
|
|
|
|
AssertOpsJson(
|
|
|
|
1,
|
|
|
|
r#"[
|
|
|
|
{"insert":"a","attributes":{"bold":"true"}},
|
|
|
|
{"insert":"bc","attributes":{"bold":"true","italic":"true"}},
|
|
|
|
{"insert":"d","attributes":{"bold":"true"}}
|
|
|
|
]"#,
|
|
|
|
),
|
|
|
|
Invert(0, 1),
|
|
|
|
AssertOpsJson(
|
|
|
|
0,
|
|
|
|
r#"[
|
|
|
|
{"insert":"12","attributes":{"bold":"true"}},
|
|
|
|
{"insert":"34","attributes":{"bold":"true","italic":"true"}},
|
|
|
|
{"insert":"56","attributes":{"bold":"true"}}
|
|
|
|
]"#,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
OpTester::new().run_script(ops);
|
|
|
|
}
|