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

Code before:
    for i in 0..5 {
        assert_eq!(buffer.move_n_words_backward(end - i, 2), 8)
    }
    assert_eq!(buffer.move_n_words_backward(end - 6, 2), 4);

    assert_eq!(buffer.move_n_words_forward(0, 2), 8);
    assert_eq!(buffer.move_n_words_forward(0, 3), 15);
    assert_eq!(buffer.move_n_words_backward(end, 2), 8);
    assert_eq!(buffer.move_n_words_backward(end, 3), 4);

    // FIXME: see #501 for possible issues in WordCursor::next_boundary()
    //
    // Trying to move beyond the buffer end.  The cursor will stay there.
    for i in 0..end {
        assert_eq!(buffer.move_n_words_forward(i, 100), end + 1);
    }

    // In the other direction.
    for i in 0..end {
        assert_eq!(buffer.move_n_words_backward(end - i, 100), 0);
    }
}

#[test]
fn test_move_n_wordends_forward() {
    fn v(buf: &Buffer, off: usize, n: usize, ins: bool, end: usize) {
        assert_eq!(buf.move_n_wordends_forward(off, n, ins), end);
    }

    let buffer = Buffer::new("");
    // Cannot move anywhere in an empty buffer.
    v(&buffer, 0, 1, false, 0);
    v(&buffer, 0, 1, true, 0);

    let buffer = Buffer::new("one two three  four  ");
    //                      ->012345678901234567890<-

    v(&buffer, 0, 0, false, 0);
    v(&buffer, 0, 0, true, 0);

    for i in 0..2 {
        v(&buffer, i, 1, false, 2);
        v(&buffer, i, 2, false, 6);
        v(&buffer, i, 3, false, 12);

        v(&buffer, i, 10, false, 21);

        // In Mode::Insert, returns 1 pass the word end.
        v(&buffer, i, 1, true, 3);
        v(&buffer, i, 2, true, 7);
        v(&buffer, i, 3, true, 13);

Review comment (suggestion):
I think I would split the test into two - one for `ins = false`, one for `ins = true`. Also the name of the function doesn't really help the reader :)

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

        let end = buffer.len() - 1;
        for offset in 0..4 {
            assert_eq!(buffer.move_n_words_forward(offset, 2), 8);
        }
        assert_eq!(buffer.move_n_words_forward(4, 2), 15);
        for offset in 0..5 {
            assert_eq!(buffer.move_n_words_backward(end - offset, 2), 8)
        }
        assert_eq!(buffer.move_n_words_backward(end - 6, 2), 4);

        assert_eq!(buffer.move_n_words_forward(0, 2), 8);
        assert_eq!(buffer.move_n_words_forward(0, 3), 15);
        assert_eq!(buffer.move_n_words_backward(end, 2), 8);
        assert_eq!(buffer.move_n_words_backward(end, 3), 4);

        // FIXME: see #501 for possible issues in WordCursor::next_boundary()
        //
        // Trying to move beyond the buffer end.  The cursor will stay there.
        for offset in 0..end {
            assert_eq!(buffer.move_n_words_forward(offset, 100), end + 1);
        }

        // In the other direction.
        for offset in 0..end {
            assert_eq!(buffer.move_n_words_backward(end - offset, 100), 0);
        }
    }

    mod on_word_end_forward {
        use super::*;

        #[test]
        fn non_insertion_mode() {
            // To save some keystrokes.
            fn v(buf: &Buffer, off: usize, n: usize, end: usize) {
                assert_eq!(buf.move_n_wordends_forward(off, n, false), end);
            }

            let buffer = Buffer::new("one two three  four  ");
            //                      ->012345678901234567890<-

            // 0 count does not move.
            v(&buffer, 0, 0, 0);

            for offset in 0..2 {
                v(&buffer, offset, 1, 2);
                v(&buffer, offset, 2, 6);
                v(&buffer, offset, 3, 12);