aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/TextBufferImplTest.java
blob: 15fb36dc3d570d4f2b9597f0f25d949b2e7df9ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.hosted.node.admin.task.util.editor;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TextBufferImplTest {
    private final TextBufferImpl textBuffer = new TextBufferImpl();

    @Test
    void testWrite() {
        assertEquals("", textBuffer.getString());
        assertWrite(2, 0, "foo\nbar\n",
                0, 0, "foo\nbar\n");

        assertWrite(1, 6, "fofirst\nsecondo\nbar\n",
                0, 2, "first\nsecond");

        assertWrite(3, 1, "fofirst\nsecondo\nbar\na",
                3, 0, "a");
        assertWrite(4, 0, "fofirst\nsecondo\nbar\na\n",
                3, 1, "\n");
    }

    @Test
    void testDelete() {
        write(0, 0, "foo\nbar\nzoo\n");
        delete(0, 2, 2, 1);
        assertEquals("fooo\n", textBuffer.getString());

        delete(0, 4, 1, 0);
        assertEquals("fooo", textBuffer.getString());

        delete(0, 0, 0, 4);
        assertEquals("", textBuffer.getString());

        delete(0, 0, 0, 0);
        assertEquals("", textBuffer.getString());
    }

    private void assertWrite(int expectedLineIndex, int expectedColumnIndex, String expectedString,
                             int lineIndex, int columnIndex, String text) {
        Position position = write(lineIndex, columnIndex, text);
        assertEquals(new Position(expectedLineIndex, expectedColumnIndex), position);
        assertEquals(expectedString, textBuffer.getString());
    }

    private Position write(int lineIndex, int columnIndex, String text) {
        return textBuffer.write(new Position(lineIndex, columnIndex), text);
    }

    private void delete(int startLineIndex, int startColumnIndex,
                        int endLineIndex, int endColumnIndex) {
        textBuffer.delete(new Position(startLineIndex, startColumnIndex),
                new Position(endLineIndex, endColumnIndex));
    }
}