use note_model::write::Block; use super::commands::{self as c, Command, Cx}; use super::doc::Kind; use super::history::History; use super::marks::{MarkType, Marks}; use super::pos::Selection; use super::testutil::*; const BACKSPACE: &[Command] = &[c::delete_selection, c::join_backward, c::select_node_backward]; const ENTER: &[Command] = &[c::split_list_item, c::newline_in_code, c::create_paragraph_near, c::lift_empty_block, c::split_block]; fn run(m: &super::model::Model, sel: Selection, cmds: &[Command]) -> (bool, Selection) { let mut cx = Cx::new(m, sel); let ran = c::chain(&mut cx, cmds, true); (ran, cx.sel) } #[test] fn insert_text_keeps_marks_exact() { let m = build(vec![pi(vec![t("ab"), strong("cd")])]); // At the end of a strong run with plain marks requested: the new text is plain. m.insert(pos(&m, 0, 4), "x", &Marks::none()); // Inside the plain run, but asking for strong. m.insert(pos(&m, 0, 1), "y", &Marks { strong: true, ..Marks::none() }); assert_eq!(dump(&m), "p(a{s:y}b{s:cd}x)"); } #[test] fn delete_range_across_blocks_and_lists() { let m = build(vec![p("one"), ul(vec![vec![p("two")], vec![p("three")]]), p("four")]); c::delete_range(&m, pos(&m, 0, 2), pos(&m, 2, 2)); assert_eq!(dump(&m), "p(onree) p(four)"); let m = build(vec![quote(vec![p("a"), p("b")]), p("c")]); c::delete_range(&m, pos(&m, 1, 1), pos(&m, 2, 0)); assert_eq!(dump(&m), "quote(p(a) p(bc))"); } #[test] fn split_heading() { let m = build(vec![h(2, "title")]); run(&m, at(&m, 0, 5), ENTER); run(&m, at(&m, 0, 2), ENTER); assert_eq!(dump(&m), "h2(ti) h2(tle) p()"); run(&m, at(&m, 0, 0), ENTER); assert_eq!(dump(&m), "p() h2(ti) h2(tle) p()"); } #[test] fn code_block_merges_lines_and_splits_back() { let m = build(vec![p("let a"), pi(vec![t(" b"), br(), strong("c")]), p("d")]); let mut cx = Cx::new(&m, Selection::Text { anchor: pos(&m, 0, 0), head: pos(&m, 1, 1) }); assert!(c::toggle_block(&mut cx, Kind::CodeBlock, true)); assert_eq!(dump(&m), "code(let a\n b\nc) p(d)"); let mut cx = Cx::new(&m, at(&m, 0, 2)); assert!(c::toggle_block(&mut cx, Kind::CodeBlock, true)); assert_eq!(dump(&m), "p(let a) p( b) p(c) p(d)"); } #[test] fn backspace() { let m = build(vec![p("a"), p("b")]); assert!(!run(&m, at(&m, 0, 0), BACKSPACE).0); let m = build(vec![p("a"), hr(), p("b")]); let (_, sel) = run(&m, at(&m, 1, 0), BACKSPACE); assert_eq!(dump(&m), "p(a) p(b)"); let (_, sel) = run(&m, sel, BACKSPACE); assert_eq!(dump(&m), "p(ab)"); assert_eq!(sel, at(&m, 0, 1)); let m = build(vec![ul(vec![vec![p("a")], vec![p("b")]])]); run(&m, at(&m, 1, 0), BACKSPACE); assert_eq!(dump(&m), "ul(li(p(a) p(b)))"); let m = build(vec![ul(vec![vec![p("a")], vec![p("b")]])]); run(&m, at(&m, 0, 0), BACKSPACE); assert_eq!(dump(&m), "p(a) ul(li(p(b)))"); let m = build(vec![p("x"), quote(vec![p("a"), p("b")])]); run(&m, at(&m, 1, 0), BACKSPACE); assert_eq!(dump(&m), "p(x) p(a) quote(p(b))"); // After a list: the paragraph moves into its last item. let m = build(vec![ul(vec![vec![p("a")]]), p("b")]); run(&m, at(&m, 1, 0), BACKSPACE); assert_eq!(dump(&m), "ul(li(p(a) p(b)))"); } #[test] fn enter_in_lists() { let m = build(vec![ul(vec![vec![p("ab")]])]); let (_, sel) = run(&m, at(&m, 0, 1), ENTER); assert_eq!(dump(&m), "ul(li(p(a)) li(p(b)))"); assert_eq!(sel, at(&m, 1, 0)); // Enter at the end makes an empty item; Enter again lifts it out. let (_, sel) = run(&m, at(&m, 1, 1), ENTER); assert_eq!(dump(&m), "ul(li(p(a)) li(p(b)) li(p()))"); run(&m, sel, ENTER); assert_eq!(dump(&m), "ul(li(p(a)) li(p(b))) p()"); } #[test] fn list_items_lift_and_sink() { let m = build(vec![ul(vec![vec![p("a"), ul(vec![vec![p("b")], vec![p("c")]])]])]); let mut cx = Cx::new(&m, at(&m, 1, 1)); assert!(c::lift_list_item(&mut cx, true)); assert_eq!(dump(&m), "ul(li(p(a)) li(p(b) ul(li(p(c)))))"); let mut cx = Cx::new(&m, at(&m, 2, 0)); assert!(!c::sink_list_item(&mut cx, false), "c's item has no previous sibling"); let mut cx = Cx::new(&m, at(&m, 1, 0)); assert!(c::sink_list_item(&mut cx, true)); assert_eq!(dump(&m), "ul(li(p(a) ul(li(p(b) ul(li(p(c)))))))"); } #[test] fn toggle_list_quote_and_mark() { let m = build(vec![p("one"), p("two")]); let mut cx = Cx::new(&m, Selection::Text { anchor: pos(&m, 0, 1), head: pos(&m, 1, 2) }); assert!(c::toggle_list(&mut cx, Kind::BulletList, true)); assert_eq!(dump(&m), "ul(li(p(one)) li(p(two)))"); assert!(c::toggle_list(&mut cx, Kind::OrderedList, true)); assert_eq!(dump(&m), "ol(li(p(one)) li(p(two)))"); assert!(c::toggle_quote(&mut cx, true)); assert_eq!(dump(&m), "quote(ol(li(p(one)) li(p(two))))"); assert!(c::toggle_quote(&mut cx, true)); assert_eq!(dump(&m), "ol(li(p(one)) li(p(two)))"); let mut cx = Cx::new(&m, Selection::Text { anchor: pos(&m, 0, 0), head: pos(&m, 1, 3) }); c::toggle_mark(&mut cx, MarkType::Strong, true); assert_eq!(dump(&m), "ol(li(p({s:one})) li(p({s:two})))"); c::toggle_mark(&mut cx, MarkType::Strong, true); assert_eq!(dump(&m), "ol(li(p(one)) li(p(two)))"); } #[test] fn paste_and_copy() { use note_model::write::Inline; let m = build(vec![p("abcd")]); let mut cx = Cx::new(&m, at(&m, 0, 2)); let blocks = [Block::Paragraph(vec![t("X")]), Block::Rule, Block::Paragraph(vec![t("Y")])]; assert!(c::insert_blocks(&mut cx, &blocks)); assert_eq!(dump(&m), "p(abX) hr p(Ycd)"); assert_eq!(cx.sel, at(&m, 1, 1)); let src = build(vec![p("one"), ul(vec![vec![p("two")]]), p("three")]); let blocks = c::selected_blocks(&src, &Selection::Text { anchor: pos(&src, 0, 1), head: pos(&src, 2, 2) }); let dst = build(vec![p("[]")]); let mut cx = Cx::new(&dst, at(&dst, 0, 1)); c::insert_blocks(&mut cx, &blocks); assert_eq!(dump(&dst), "p([ne) ul(li(p(two))) p(th])"); let _ = Inline::HardBreak; } #[test] fn undo_redo() { let m = build(vec![p("ab"), ul(vec![vec![pi(vec![strong("x")])], vec![p("y")]])]); let before = dump(&m); let mut h = History::default(); let edit = |h: &mut History, sel: Selection, f: &dyn Fn(&mut Cx), now: f64| { let mut cx = Cx::new(&m, sel.clone()); f(&mut cx); h.push(&m, &sel, &cx.sel, now); }; // Type, close the step, delete the typed char: undo restores it, then removes it. edit(&mut h, at(&m, 0, 1), &|cx| assert!(c::insert_text(cx, "Q")), 0.0); h.checkpoint(); edit(&mut h, at(&m, 0, 2), &|cx| assert!(c::chain(cx, &[c::delete_selection, c::join_backward], true) || { cx.m.delete(cx.sel.cursor().unwrap() - 1, cx.sel.cursor().unwrap()); true }), 10.0); assert_eq!(dump(&m), before); h.undo(&m); assert!(dump(&m).starts_with("p(aQb)")); h.undo(&m); assert_eq!(dump(&m), before); h.redo(&m); h.redo(&m); assert_eq!(dump(&m), before); // Select all, delete, undo: everything back with its marks and lists. edit(&mut h, Selection::All, &|cx| assert!(c::delete_selection(cx, true)), 5000.0); assert_eq!(dump(&m), "p()"); assert_eq!(h.undo(&m), Some(Selection::All)); assert_eq!(dump(&m), before); } #[test] fn anchor_survives_remote_insert_before() { let m = build(vec![pi(vec![t("ab"), br(), strong("cd")])]); let a = m.anchor(4); let other = loro::LoroDoc::new(); other.import(&m.doc.export(loro::ExportMode::Snapshot).unwrap()).unwrap(); other.get_text("body").insert(0, "xy").unwrap(); other.commit(); m.import(&other.export(loro::ExportMode::updates(&m.doc.oplog_vv())).unwrap()).unwrap(); assert_eq!(m.resolve(&a), 6); assert_eq!(dump(&m), "p(xyab|{s:cd})"); } /// A 5 MB paragraph: Enter and Backspace in its middle stay cheap. #[test] #[ignore] fn huge_block_split_join() { let big = "lorem ipsum ".repeat(450_000); let m = build(vec![p(&big)]); let mid = big.len() / 2; let t0 = std::time::Instant::now(); let (_, sel) = run(&m, Selection::caret(mid), ENTER); let split = t0.elapsed(); let t1 = std::time::Instant::now(); run(&m, sel, BACKSPACE); let join = t1.elapsed(); eprintln!("split {split:?} join {join:?}"); assert!(join.as_millis() < 50, "join {join:?}"); assert_eq!(m.len(), 1); }