AppFlowy/rust-lib/flowy-ot/tests/invert_test.rs

243 lines
7.0 KiB
Rust
Raw Normal View History

2021-08-04 07:09:04 +00:00
pub mod helper;
use crate::helper::{TestOp::*, *};
2021-08-08 14:29:16 +00:00
use flowy_ot::core::{Builder, Delta, Interval};
2021-08-04 07:09:04 +00:00
#[test]
fn delta_invert_no_attribute_delta() {
2021-08-04 07:09:04 +00:00
let mut delta = Delta::default();
2021-08-08 14:29:16 +00:00
delta.add(Builder::insert("123").build());
2021-08-04 07:09:04 +00:00
let mut change = Delta::default();
2021-08-08 14:29:16 +00:00
change.add(Builder::retain(3).build());
change.add(Builder::insert("456").build());
2021-08-06 14:25:09 +00:00
let undo = change.invert(&delta);
2021-08-04 07:09:04 +00:00
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![
2021-08-04 14:33:16 +00:00
Insert(0, "123", 0),
Insert(1, "4567", 0),
2021-08-05 14:52:19 +00:00
Invert(0, 1),
2021-08-04 14:33:16 +00:00
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
];
OpTester::new().run_script(ops);
}
#[test]
fn delta_invert_attribute_delta_with_no_attribute_delta() {
2021-08-04 14:33:16 +00:00
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),
2021-08-05 14:52:19 +00:00
Invert(0, 1),
2021-08-04 14:33:16 +00:00
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#),
];
OpTester::new().run_script(ops);
}
#[test]
fn delta_invert_attribute_delta_with_no_attribute_delta2() {
2021-08-04 14:33:16 +00:00
let ops = vec![
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
Insert(0, "456", 3),
2021-08-08 14:29:16 +00:00
AssertOpsJson(
0,
r#"[
{"insert":"123456","attributes":{"bold":"true"}}]
"#,
),
2021-08-04 14:33:16 +00:00
Italic(0, Interval::new(2, 4), true),
AssertOpsJson(
0,
2021-08-08 14:29:16 +00:00
r#"[
{"insert":"12","attributes":{"bold":"true"}},
{"insert":"34","attributes":{"bold":"true","italic":"true"}},
{"insert":"56","attributes":{"bold":"true"}}
]"#,
2021-08-04 14:33:16 +00:00
),
Insert(1, "abc", 0),
2021-08-05 14:52:19 +00:00
Invert(0, 1),
AssertOpsJson(
0,
2021-08-08 14:29:16 +00:00
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"}]"#,
),
2021-08-05 14:52:19 +00:00
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,
2021-08-06 15:06:27 +00:00
r#"[{"insert":"a","attributes":{"bold":"true"}},{"insert":"bc","attributes":
{"bold":"true","italic":"true"}},{"insert":"d","attributes":{"bold":"true"
}}]"#,
),
2021-08-05 14:52:19 +00:00
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,
2021-08-08 14:29:16 +00:00
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,
2021-08-08 14:29:16 +00:00
r#"[
{"insert":"a","attributes":{"bold":"true"}},
{"insert":"bc","attributes":{"bold":"true","italic":"true"}},
{"insert":"d","attributes":{"bold":"true"}}
]"#,
),
2021-08-05 14:52:19 +00:00
Invert(0, 1),
AssertOpsJson(
0,
2021-08-08 14:29:16 +00:00
r#"[
{"insert":"12","attributes":{"bold":"true"}},
{"insert":"34","attributes":{"bold":"true","italic":"true"}},
{"insert":"56","attributes":{"bold":"true"}}
]"#,
),
];
OpTester::new().run_script(ops);
}
2021-08-04 07:09:04 +00:00
#[test]
fn delta_get_ops_in_interval_1() {
let mut delta = Delta::default();
2021-08-08 14:29:16 +00:00
let insert_a = Builder::insert("123").build();
let insert_b = Builder::insert("4").build();
2021-08-04 07:09:04 +00:00
delta.add(insert_a.clone());
delta.add(insert_b.clone());
assert_eq!(
2021-08-06 15:06:27 +00:00
delta.ops_in_interval(Interval::new(0, 4)),
2021-08-04 07:09:04 +00:00
vec![delta.ops.last().unwrap().clone()]
);
}
#[test]
fn delta_get_ops_in_interval_2() {
let mut delta = Delta::default();
2021-08-08 14:29:16 +00:00
let insert_a = Builder::insert("123").build();
let insert_b = Builder::insert("4").build();
let insert_c = Builder::insert("5").build();
let retain_a = Builder::retain(3).build();
2021-08-04 07:09:04 +00:00
delta.add(insert_a.clone());
delta.add(retain_a.clone());
delta.add(insert_b.clone());
delta.add(insert_c.clone());
2021-08-08 14:29:16 +00:00
assert_eq!(
delta.ops_in_interval(Interval::new(0, 2)),
vec![Builder::insert("12").build()]
);
2021-08-04 07:09:04 +00:00
assert_eq!(
delta.ops_in_interval(Interval::new(0, 3)),
vec![insert_a.clone()]
);
assert_eq!(
delta.ops_in_interval(Interval::new(0, 4)),
2021-08-08 14:29:16 +00:00
vec![insert_a.clone(), Builder::retain(1).build()]
);
assert_eq!(
delta.ops_in_interval(Interval::new(0, 6)),
2021-08-04 07:09:04 +00:00
vec![insert_a.clone(), retain_a.clone()]
);
assert_eq!(
delta.ops_in_interval(Interval::new(0, 7)),
2021-08-08 14:29:16 +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();
let insert_a = Builder::insert("123456").build();
delta.add(insert_a.clone());
assert_eq!(
delta.ops_in_interval(Interval::new(3, 6)),
vec![Builder::insert("456").build()]
);
}
#[test]
fn delta_get_ops_in_interval_4() {
let mut delta = Delta::default();
let insert_a = Builder::insert("12").build();
let insert_b = Builder::insert("34").build();
let insert_c = Builder::insert("56").build();
delta.ops.push(insert_a.clone());
delta.ops.push(insert_b.clone());
delta.ops.push(insert_c.clone());
assert_eq!(delta.ops_in_interval(Interval::new(0, 2)), vec![insert_a]);
assert_eq!(delta.ops_in_interval(Interval::new(2, 4)), vec![insert_b]);
assert_eq!(delta.ops_in_interval(Interval::new(4, 6)), vec![insert_c]);
assert_eq!(
delta.ops_in_interval(Interval::new(2, 5)),
vec![Builder::insert("34").build(), Builder::insert("5").build()]
2021-08-04 07:09:04 +00:00
);
}