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

Code before:
    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 {
        assert_eq!(buffer.move_n_words_forward(i, 2), 8);
    }
    assert_eq!(buffer.move_n_words_forward(4, 2), 15);
    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);
    }
}


Review comment (suggestion):
This is a bit hard to read. Does this mean you try the command from offset 0, 1, 2, 3, and move 2 words forward? Calling the loop variable something else (`start_offset`) would help.

Code after:

    #[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);
            assert_eq!(buffer.move_word_backward(offset), 0);
        }

        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);