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

374 lines
10 KiB
Rust
Raw Normal View History

2021-08-06 00:40:45 +00:00
pub mod helper;
use crate::helper::{TestOp::*, *};
2021-08-18 05:12:45 +00:00
use flowy_ot::{
client::{FlowyDoc, PlainDoc, RECORD_THRESHOLD},
2021-08-18 05:12:45 +00:00
core::{Interval, NEW_LINE, WHITESPACE},
};
2021-08-06 00:40:45 +00:00
#[test]
2021-08-18 05:12:45 +00:00
fn history_insert_undo() {
2021-09-13 07:51:13 +00:00
let ops = vec![Insert(0, "123", 0), Undo(0), AssertOpsJson(0, r#"[{"insert":"\n"}]"#)];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-06 00:40:45 +00:00
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_insert_undo_with_lagging() {
2021-08-06 00:40:45 +00:00
let ops = vec![
Insert(0, "123", 0),
Wait(RECORD_THRESHOLD),
2021-08-06 00:40:45 +00:00
Insert(0, "456", 0),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-06 00:40:45 +00:00
}
2021-08-06 14:25:09 +00:00
#[test]
2021-08-18 05:12:45 +00:00
fn history_insert_redo() {
2021-08-06 14:25:09 +00:00
let ops = vec![
Insert(0, "123", 0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
Redo(0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-06 14:25:09 +00:00
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_insert_redo_with_lagging() {
2021-08-06 14:25:09 +00:00
let ops = vec![
Insert(0, "123", 0),
Wait(RECORD_THRESHOLD),
2021-08-06 14:25:09 +00:00
Insert(0, "456", 3),
Wait(RECORD_THRESHOLD),
2021-08-06 14:25:09 +00:00
AssertStr(0, "123456\n"),
AssertOpsJson(0, r#"[{"insert":"123456\n"}]"#),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
Redo(0),
AssertOpsJson(0, r#"[{"insert":"123456\n"}]"#),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-06 14:25:09 +00:00
}
2021-08-07 01:19:18 +00:00
#[test]
2021-08-18 05:12:45 +00:00
fn history_bold_undo() {
2021-08-07 01:19:18 +00:00
let ops = vec![
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_bold_undo_with_lagging() {
let ops = vec![
Insert(0, "123", 0),
Wait(RECORD_THRESHOLD),
Bold(0, Interval::new(0, 3), true),
Undo(0),
2021-08-07 01:19:18 +00:00
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-07 01:19:18 +00:00
}
2021-08-08 14:29:16 +00:00
#[test]
2021-08-18 05:12:45 +00:00
fn history_bold_redo() {
2021-08-08 14:29:16 +00:00
let ops = vec![
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
2021-08-08 14:29:16 +00:00
Redo(0),
2021-09-13 07:51:13 +00:00
AssertOpsJson(0, r#" [{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#),
2021-08-08 14:29:16 +00:00
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-08 14:29:16 +00:00
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_bold_redo_with_lagging() {
let ops = vec![
Insert(0, "123", 0),
Wait(RECORD_THRESHOLD),
Bold(0, Interval::new(0, 3), true),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"123\n"}]"#),
Redo(0),
2021-09-13 07:51:13 +00:00
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_delete_undo() {
let ops = vec![
Insert(0, "123", 0),
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
Delete(0, Interval::new(0, 3)),
AssertOpsJson(0, r#"[]"#),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
];
TestBuilder::new().run_script::<PlainDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_delete_undo_2() {
let ops = vec![
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
Delete(0, Interval::new(0, 1)),
AssertOpsJson(
0,
r#"[
{"insert":"23","attributes":{"bold":"true"}},
{"insert":"\n"}]
"#,
),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_delete_undo_with_lagging() {
let ops = vec![
Insert(0, "123", 0),
Wait(RECORD_THRESHOLD),
Bold(0, Interval::new(0, 3), true),
Wait(RECORD_THRESHOLD),
Delete(0, Interval::new(0, 1)),
AssertOpsJson(
0,
r#"[
{"insert":"23","attributes":{"bold":"true"}},
{"insert":"\n"}]
"#,
),
Undo(0),
AssertOpsJson(
0,
r#"[
{"insert":"123","attributes":{"bold":"true"}},
{"insert":"\n"}]
"#,
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_delete_redo() {
let ops = vec![
Insert(0, "123", 0),
2021-08-14 08:44:39 +00:00
Wait(RECORD_THRESHOLD),
Delete(0, Interval::new(0, 3)),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
Undo(0),
Redo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_replace_undo() {
let ops = vec![
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
Replace(0, Interval::new(0, 2), "ab"),
AssertOpsJson(
0,
r#"[
{"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
"#,
),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_replace_undo_with_lagging() {
let ops = vec![
Insert(0, "123", 0),
Wait(RECORD_THRESHOLD),
Bold(0, Interval::new(0, 3), true),
Wait(RECORD_THRESHOLD),
Replace(0, Interval::new(0, 2), "ab"),
AssertOpsJson(
0,
r#"[
{"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
"#,
),
Undo(0),
2021-09-13 07:51:13 +00:00
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
#[test]
2021-08-18 05:12:45 +00:00
fn history_replace_redo() {
let ops = vec![
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
Replace(0, Interval::new(0, 2), "ab"),
Undo(0),
Redo(0),
AssertOpsJson(
0,
r#"[
{"insert":"ab"},
{"insert":"3","attributes":{"bold":"true"}},{"insert":"\n"}]
"#,
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
}
2021-08-16 07:02:57 +00:00
#[test]
2021-08-18 05:12:45 +00:00
fn history_header_added_undo() {
2021-08-16 07:02:57 +00:00
let ops = vec![
Insert(0, "123456", 0),
2021-09-13 07:51:13 +00:00
Header(0, Interval::new(0, 6), 1),
2021-08-16 07:02:57 +00:00
Insert(0, "\n", 3),
Insert(0, "\n", 4),
Undo(0),
2021-08-18 05:12:45 +00:00
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
2021-08-16 07:02:57 +00:00
Redo(0),
AssertOpsJson(
0,
2021-09-22 06:42:14 +00:00
r#"[{"insert":"123"},{"insert":"\n\n","attributes":{"header":1}},{"insert":"456"},{"insert":"\n","attributes":{"header":1}}]"#,
2021-08-16 07:02:57 +00:00
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-16 07:02:57 +00:00
}
2021-08-16 15:07:40 +00:00
#[test]
2021-08-18 05:12:45 +00:00
fn history_link_added_undo() {
2021-08-16 15:07:40 +00:00
let site = "https://appflowy.io";
let ops = vec![
Insert(0, site, 0),
Wait(RECORD_THRESHOLD),
2021-09-13 07:51:13 +00:00
Link(0, Interval::new(0, site.len()), site),
2021-08-16 15:07:40 +00:00
Undo(0),
AssertOpsJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
Redo(0),
AssertOpsJson(
0,
r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io"}},{"insert":"\n"}]"#,
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-16 15:07:40 +00:00
}
2021-08-18 05:12:45 +00:00
#[test]
fn history_link_auto_format_undo_with_lagging() {
let site = "https://appflowy.io";
let ops = vec![
Insert(0, site, 0),
AssertOpsJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
Wait(RECORD_THRESHOLD),
Insert(0, WHITESPACE, site.len()),
AssertOpsJson(
0,
r#"[{"insert":"https://appflowy.io","attributes":{"link":"https://appflowy.io/"}},{"insert":" \n"}]"#,
),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"https://appflowy.io\n"}]"#),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-18 05:12:45 +00:00
}
#[test]
fn history_bullet_undo() {
let ops = vec![
Insert(0, "1", 0),
Bullet(0, Interval::new(0, 1), true),
Insert(0, NEW_LINE, 1),
Insert(0, "2", 2),
AssertOpsJson(
0,
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
2021-08-18 05:12:45 +00:00
),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
Redo(0),
AssertOpsJson(
0,
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
2021-08-18 05:12:45 +00:00
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-18 05:12:45 +00:00
}
#[test]
fn history_bullet_undo_with_lagging() {
let ops = vec![
Insert(0, "1", 0),
Bullet(0, Interval::new(0, 1), true),
Wait(RECORD_THRESHOLD),
Insert(0, NEW_LINE, 1),
Insert(0, "2", 2),
Wait(RECORD_THRESHOLD),
AssertOpsJson(
0,
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
2021-08-18 05:12:45 +00:00
),
Undo(0),
AssertOpsJson(0, r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}}]"#),
2021-08-18 05:12:45 +00:00
Undo(0),
AssertOpsJson(0, r#"[{"insert":"\n"}]"#),
Redo(0),
Redo(0),
AssertOpsJson(
0,
r#"[{"insert":"1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"2"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
2021-08-18 05:12:45 +00:00
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-18 05:12:45 +00:00
}
#[test]
fn history_undo_attribute_on_merge_between_line() {
let ops = vec![
Insert(0, "123456", 0),
Bullet(0, Interval::new(0, 6), true),
Wait(RECORD_THRESHOLD),
Insert(0, NEW_LINE, 3),
Wait(RECORD_THRESHOLD),
AssertOpsJson(
0,
r#"[{"insert":"123"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
2021-08-18 05:12:45 +00:00
),
Delete(0, Interval::new(3, 4)), // delete the newline
AssertOpsJson(0, r#"[{"insert":"123456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#),
2021-08-18 05:12:45 +00:00
Undo(0),
AssertOpsJson(
0,
r#"[{"insert":"123"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"456"},{"insert":"\n","attributes":{"list":"bullet"}}]"#,
2021-08-18 05:12:45 +00:00
),
];
TestBuilder::new().run_script::<FlowyDoc>(ops);
2021-08-18 05:12:45 +00:00
}