Repository: lapce/lapce
File: lapce-core/src/buffer/test.rs (Rust)

Code before:
use xi_rope::Rope;

use crate::{editor::EditType, selection::Selection};

use super::Buffer;

#[test]
fn test_is_pristine() {
    let mut buffer = Buffer::new("");
    buffer.init_content(Rope::from("abc"));
    buffer.edit(&[(Selection::caret(0), "d")], EditType::InsertChars);
    buffer.edit(&[(Selection::caret(0), "e")], EditType::InsertChars);
    buffer.do_undo();
    buffer.do_undo();
    assert!(buffer.is_pristine());
}

#[test]
fn test_move_by_word() {
    let mut buffer = Buffer::new("");
    // Cannot move anywhere in an empty buffer.
    assert_eq!(buffer.move_word_forward(0), 0);
    assert_eq!(buffer.move_n_words_forward(0, 0), 0);
    assert_eq!(buffer.move_n_words_forward(0, 2), 0);
    assert_eq!(buffer.move_word_backward(0), 0);
    assert_eq!(buffer.move_n_words_backward(0, 2), 0);
    assert_eq!(buffer.move_n_words_backward(0, 0), 0);

    // Add some content to the buffer.
    buffer.init_content(Rope::from("one two three  four  "));
    //                            ->012345678901234567890<-

    // 0 count don't move at all.
    assert_eq!(buffer.move_n_words_forward(0, 0), 0);
    assert_eq!(buffer.move_n_words_backward(0, 0), 0);

    for i in 0..4 {
        assert_eq!(buffer.move_word_forward(i), 4);
        assert_eq!(buffer.move_word_backward(i), 0);
    }

    assert_eq!(buffer.move_word_forward(4), 8);
    assert_eq!(buffer.move_word_backward(4), 0);

    let end = buffer.len() - 1;
    for i in 0..4 {

Review comment (bug):
Alright, I don't want you to change anything about this now, but a general advice I try to follow is to name my test functions according to what they are testing, in detail. This comment would be a perfect name for a single test case, if the tests were grouped in a per-command module :)

This way, a test failure would be immediately helpful (`...move_by_word_cannot_move_anywhere_in_an_empty_buffer: FAILED`)

Code after:
use super::Buffer;

mod editing {
    use super::*;
    use crate::{editor::EditType, selection::Selection};
    use xi_rope::Rope;

    #[test]
    fn is_pristine() {
        let mut buffer = Buffer::new("");
        buffer.init_content(Rope::from("abc"));
        buffer.edit(&[(Selection::caret(0), "d")], EditType::InsertChars);
        buffer.edit(&[(Selection::caret(0), "e")], EditType::InsertChars);
        buffer.do_undo();
        buffer.do_undo();
        assert!(buffer.is_pristine());
    }
}

mod motion {
    use super::*;

    #[test]
    fn cannot_move_in_empty_buffer() {
        let buffer = Buffer::new("");
        assert_eq!(buffer.move_word_forward(0), 0);
        assert_eq!(buffer.move_n_words_forward(0, 2), 0);

        assert_eq!(buffer.move_word_backward(0), 0);
        assert_eq!(buffer.move_n_words_backward(0, 2), 0);

        assert_eq!(buffer.move_n_wordends_forward(0, 2, false), 0);
        assert_eq!(buffer.move_n_wordends_forward(0, 2, true), 0);
    }

    #[test]
    fn on_word_boundary_in_either_direction() {
        let buffer = Buffer::new("one two three  four  ");
        //                      ->012345678901234567890<-

        // 0 count does not move.
        assert_eq!(buffer.move_n_words_forward(0, 0), 0);
        assert_eq!(buffer.move_n_words_backward(0, 0), 0);

        for offset in 0..4 {
            assert_eq!(buffer.move_word_forward(offset), 4);