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

506 lines
15 KiB
Rust
Raw Normal View History

pub mod helper;
2021-08-04 07:09:04 +00:00
use crate::helper::{TestOp::*, *};
2021-08-03 23:55:54 +00:00
use flowy_ot::core::Interval;
2021-08-01 08:39:32 +00:00
2021-08-03 15:13:20 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_insert_text() {
2021-08-03 15:13:20 +00:00
let ops = vec![
Insert(0, "123", 0),
Insert(0, "456", 3),
AssertOpsJson(0, r#"[{"insert":"123456"}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_insert_text_at_head() {
2021-08-03 15:13:20 +00:00
let ops = vec![
Insert(0, "123", 0),
Insert(0, "456", 0),
AssertOpsJson(0, r#"[{"insert":"456123"}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_insert_text_at_middle() {
2021-08-03 15:13:20 +00:00
let ops = vec![
Insert(0, "123", 0),
2021-08-03 23:55:54 +00:00
Insert(0, "456", 1),
AssertOpsJson(0, r#"[{"insert":"145623"}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 23:55:54 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_insert_text_with_attr() {
2021-08-03 23:55:54 +00:00
let ops = vec![
Insert(0, "145", 0),
Insert(0, "23", 1),
Bold(0, Interval::new(0, 2), true),
AssertOpsJson(
0,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"345"}]"#,
),
2021-08-15 15:30:54 +00:00
Insert(0, "abc", 1),
AssertOpsJson(
0,
r#"[{"insert":"1abc2","attributes":{"bold":"true"}},{"insert":"345"}]"#,
),
2021-08-03 15:13:20 +00:00
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
2021-08-13 08:39:32 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold() {
2021-08-13 08:39:32 +00:00
let ops = vec![
Insert(0, "123456", 0),
Bold(0, Interval::new(3, 5), true),
AssertOpsJson(
0,
r#"[
{"insert":"123"},
{"insert":"45","attributes":{"bold":"true"}},
{"insert":"6"}
]"#,
),
];
OpTester::new().run_script(ops);
}
2021-08-01 08:39:32 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_and_invert_all() {
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "123", 0),
Bold(0, Interval::new(0, 3), true),
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#),
Bold(0, Interval::new(0, 3), false),
AssertOpsJson(0, r#"[{"insert":"123"}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_and_invert_partial_suffix() {
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "1234", 0),
Bold(0, Interval::new(0, 4), true),
AssertOpsJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#),
Bold(0, Interval::new(2, 4), false),
AssertOpsJson(
0,
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34"}]"#,
),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_and_invert_partial_suffix2() {
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "1234", 0),
Bold(0, Interval::new(0, 4), true),
AssertOpsJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#),
2021-08-03 08:20:25 +00:00
Bold(0, Interval::new(2, 4), false),
AssertOpsJson(
0,
2021-08-03 08:20:25 +00:00
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34"}]"#,
),
2021-08-03 08:20:25 +00:00
Bold(0, Interval::new(2, 4), true),
AssertOpsJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
}
2021-08-02 15:08:24 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_with_new_line() {
let ops = vec![
Insert(0, "123456", 0),
Bold(0, Interval::new(0, 6), true),
AssertOpsJson(
0,
r#"[{"insert":"123456","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
),
Insert(0, "\n", 3),
AssertOpsJson(
0,
r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n"},{"insert":"456","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
),
Insert(0, "\n", 4),
AssertOpsJson(
0,
r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\n\n"},{"insert":"456","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
),
Insert(0, "a", 4),
AssertOpsJson(
0,
r#"[{"insert":"123","attributes":{"bold":"true"}},{"insert":"\na\n"},{"insert":"456","attributes":{"bold":"true"}},{"insert":"\n"}]"#,
),
];
OpTester::new().run_script_with_newline(ops);
}
#[test]
fn attributes_add_bold_and_invert_partial_prefix() {
2021-08-02 15:08:24 +00:00
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "1234", 0),
2021-08-02 15:08:24 +00:00
Bold(0, Interval::new(0, 4), true),
2021-08-03 08:20:25 +00:00
AssertOpsJson(0, r#"[{"insert":"1234","attributes":{"bold":"true"}}]"#),
Bold(0, Interval::new(0, 2), false),
2021-08-02 15:08:24 +00:00
AssertOpsJson(
0,
2021-08-03 08:20:25 +00:00
r#"[{"insert":"12"},{"insert":"34","attributes":{"bold":"true"}}]"#,
2021-08-02 15:08:24 +00:00
),
2021-08-03 08:20:25 +00:00
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 08:20:25 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_consecutive() {
2021-08-03 08:20:25 +00:00
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "1234", 0),
2021-08-03 08:20:25 +00:00
Bold(0, Interval::new(0, 1), true),
2021-08-02 15:08:24 +00:00
AssertOpsJson(
0,
2021-08-03 08:20:25 +00:00
r#"[{"insert":"1","attributes":{"bold":"true"}},{"insert":"234"}]"#,
2021-08-02 15:08:24 +00:00
),
2021-08-03 08:20:25 +00:00
Bold(0, Interval::new(1, 2), true),
2021-08-02 15:08:24 +00:00
AssertOpsJson(
0,
2021-08-03 08:20:25 +00:00
r#"[{"insert":"12","attributes":{"bold":"true"}},{"insert":"34"}]"#,
2021-08-02 15:08:24 +00:00
),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-02 15:08:24 +00:00
}
2021-08-03 08:20:25 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_italic() {
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "1234", 0),
Bold(0, Interval::new(0, 4), true),
2021-08-03 08:20:25 +00:00
Italic(0, Interval::new(0, 4), true),
AssertOpsJson(
0,
2021-08-15 13:11:48 +00:00
r#"[{"insert":"1234","attributes":{"italic":"true","bold":"true"}},{"insert":"\n"}]"#,
2021-08-03 08:20:25 +00:00
),
2021-08-03 15:13:20 +00:00
Insert(0, "5678", 4),
2021-08-03 08:20:25 +00:00
AssertOpsJson(
0,
2021-08-15 13:11:48 +00:00
r#"[{"insert":"12345678","attributes":{"bold":"true","italic":"true"}},{"insert":"\n"}]"#,
),
];
2021-08-15 13:11:48 +00:00
OpTester::new().run_script_with_newline(ops);
}
2021-08-03 15:13:20 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_italic2() {
2021-08-03 15:13:20 +00:00
let ops = vec![
Insert(0, "123456", 0),
Bold(0, Interval::new(0, 6), true),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Italic(0, Interval::new(0, 2), true),
AssertOpsJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"3456","attributes":{"bold":"true"}}]
"#,
2021-08-03 15:13:20 +00:00
),
Italic(0, Interval::new(4, 6), true),
AssertOpsJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}]
"#,
2021-08-03 15:13:20 +00:00
),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_italic3() {
2021-08-03 15:13:20 +00:00
let ops = vec![
Insert(0, "123456789", 0),
Bold(0, Interval::new(0, 5), true),
Italic(0, Interval::new(0, 2), true),
AssertOpsJson(
0,
r#"[
{"insert":"12","attributes":{"bold":"true","italic":"true"}},
{"insert":"345","attributes":{"bold":"true"}},{"insert":"6789"}]
"#,
2021-08-03 15:13:20 +00:00
),
Italic(0, Interval::new(2, 4), true),
AssertOpsJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}},
{"insert":"5","attributes":{"bold":"true"}},
{"insert":"6789"}]
"#,
2021-08-03 15:13:20 +00:00
),
Bold(0, Interval::new(7, 9), true),
AssertOpsJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}},
{"insert":"5","attributes":{"bold":"true"}},
{"insert":"67"},
{"insert":"89","attributes":{"bold":"true"}}]
"#,
2021-08-03 15:13:20 +00:00
),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_add_bold_italic_delete() {
2021-08-03 15:13:20 +00:00
let ops = vec![
Insert(0, "123456789", 0),
Bold(0, Interval::new(0, 5), true),
Italic(0, Interval::new(0, 2), true),
AssertOpsJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"345","attributes":{"bold":"true"}},{"insert":"6789"}]
"#,
),
2021-08-03 15:13:20 +00:00
Italic(0, Interval::new(2, 4), true),
AssertOpsJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}}
,{"insert":"5","attributes":{"bold":"true"}},{"insert":"6789"}]"#,
),
Bold(0, Interval::new(7, 9), true),
AssertOpsJson(
0,
r#"[
{"insert":"1234","attributes":{"bold":"true","italic":"true"}},
{"insert":"5","attributes":{"bold":"true"}},{"insert":"67"},
{"insert":"89","attributes":{"bold":"true"}}]
"#,
),
Delete(0, Interval::new(0, 5)),
AssertOpsJson(
0,
r#"[{"insert":"67"},{"insert":"89","attributes":{"bold":"true"}}]"#,
2021-08-03 15:13:20 +00:00
),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_merge_inserted_text_with_same_attribute() {
let ops = vec![
InsertBold(0, "123", Interval::new(0, 3)),
AssertOpsJson(0, r#"[{"insert":"123","attributes":{"bold":"true"}}]"#),
InsertBold(0, "456", Interval::new(3, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_compose_attr_attributes_with_attr_attributes_test() {
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
2021-08-03 15:13:20 +00:00
InsertBold(1, "7", Interval::new(0, 1)),
AssertOpsJson(1, r#"[{"insert":"7","attributes":{"bold":"true"}}]"#),
Transform(0, 1),
2021-08-03 15:13:20 +00:00
AssertOpsJson(0, r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#),
AssertOpsJson(1, r#"[{"insert":"1234567","attributes":{"bold":"true"}}]"#),
];
2021-08-03 15:13:20 +00:00
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_compose_attr_attributes_with_attr_attributes_test2() {
let ops = vec![
2021-08-03 15:13:20 +00:00
Insert(0, "123456", 0),
Bold(0, Interval::new(0, 6), true),
Italic(0, Interval::new(0, 2), true),
Italic(0, Interval::new(4, 6), true),
AssertOpsJson(
0,
r#"[
{"insert":"12","attributes":{"bold":"true","italic":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}}]
"#,
2021-08-03 15:13:20 +00:00
),
InsertBold(1, "7", Interval::new(0, 1)),
AssertOpsJson(1, r#"[{"insert":"7","attributes":{"bold":"true"}}]"#),
Transform(0, 1),
2021-08-03 15:13:20 +00:00
AssertOpsJson(
0,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}},
{"insert":"7","attributes":{"bold":"true"}}]
"#,
2021-08-03 15:13:20 +00:00
),
AssertOpsJson(
1,
r#"[
{"insert":"12","attributes":{"italic":"true","bold":"true"}},
{"insert":"34","attributes":{"bold":"true"}},
{"insert":"56","attributes":{"italic":"true","bold":"true"}},
{"insert":"7","attributes":{"bold":"true"}}]
"#,
2021-08-03 15:13:20 +00:00
),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-01 08:39:32 +00:00
}
2021-08-03 08:20:25 +00:00
2021-08-03 15:13:20 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_compose_attr_attributes_with_no_attr_attributes_test() {
2021-08-11 09:18:10 +00:00
let expected = r#"[{"insert":"123456","attributes":{"bold":"true"}},{"insert":"7"}]"#;
2021-08-03 15:13:20 +00:00
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Insert(1, "7", 0),
AssertOpsJson(1, r#"[{"insert":"7"}]"#),
Transform(0, 1),
AssertOpsJson(0, expected),
AssertOpsJson(1, expected),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 15:13:20 +00:00
}
2021-08-03 08:20:25 +00:00
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_replace_heading() {
2021-08-03 08:20:25 +00:00
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Delete(0, Interval::new(0, 2)),
AssertOpsJson(0, r#"[{"insert":"3456","attributes":{"bold":"true"}}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 08:20:25 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_replace_trailing() {
2021-08-03 08:20:25 +00:00
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Delete(0, Interval::new(5, 6)),
AssertOpsJson(0, r#"[{"insert":"12345","attributes":{"bold":"true"}}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 08:20:25 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_replace_middle() {
2021-08-03 08:20:25 +00:00
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Delete(0, Interval::new(0, 2)),
AssertOpsJson(0, r#"[{"insert":"3456","attributes":{"bold":"true"}}]"#),
Delete(0, Interval::new(2, 4)),
AssertOpsJson(0, r#"[{"insert":"34","attributes":{"bold":"true"}}]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 08:20:25 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_replace_all() {
2021-08-03 08:20:25 +00:00
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Delete(0, Interval::new(0, 6)),
AssertOpsJson(0, r#"[]"#),
];
2021-08-04 07:09:04 +00:00
OpTester::new().run_script(ops);
2021-08-03 08:20:25 +00:00
}
#[test]
2021-08-15 13:11:48 +00:00
fn attributes_replace_with_text() {
let ops = vec![
InsertBold(0, "123456", Interval::new(0, 6)),
AssertOpsJson(0, r#"[{"insert":"123456","attributes":{"bold":"true"}}]"#),
Replace(0, Interval::new(0, 3), "ab"),
AssertOpsJson(
0,
2021-08-14 08:44:39 +00:00
r#"[{"insert":"ab"},{"insert":"456","attributes":{"bold":"true"}}]"#,
),
];
OpTester::new().run_script(ops);
}
2021-08-15 13:11:48 +00:00
#[test]
fn attributes_add_header() {
let ops = vec![
Insert(0, "123456", 0),
Header(0, Interval::new(0, 6), 1, true),
AssertOpsJson(
0,
r#"[{"insert":"123456"},{"insert":"\n","attributes":{"header":"1"}}]"#,
),
Insert(0, "\n", 3),
AssertOpsJson(
0,
r#"[{"insert":"123"},{"insert":"\n","attributes":{"header":"1"}},{"insert":"456"},{"insert":"\n","attributes":{"header":"1"}}]"#,
),
];
OpTester::new().run_script_with_newline(ops);
}
#[test]
fn attributes_header_add_newline() {
let ops = vec![
Insert(0, "123456", 0),
Header(0, Interval::new(0, 6), 1, true),
Insert(0, "\n", 6),
AssertOpsJson(
0,
r#"[{"insert":"123456"},{"insert":"\n","attributes":{"header":"1"}},{"insert":"\n"}]"#,
),
];
OpTester::new().run_script_with_newline(ops);
}
#[test]
fn attributes_header_add_newline_2() {
let ops = vec![
Insert(0, "123456", 0),
Header(0, Interval::new(0, 6), 1, true),
Insert(0, "\n", 3),
AssertOpsJson(
0,
r#"[{"insert":"123"},{"insert":"\n","attributes":{"header":"1"}},{"insert":"456"},{"insert":"\n","attributes":{"header":"1"}}]"#,
),
Insert(0, "\n", 4),
AssertOpsJson(
0,
r#"[{"insert":"123"},{"insert":"\n\n","attributes":{"header":"1"}},{"insert":"456"},{"insert":"\n","attributes":{"header":"1"}}]"#,
),
Insert(0, "\n", 4),
AssertOpsJson(
0,
r#"[{"insert":"123"},{"insert":"\n\n","attributes":{"header":"1"}},{"insert":"\n456"},{"insert":"\n","attributes":{"header":"1"}}]"#,
),
];
OpTester::new().run_script_with_newline(ops);
}