github.com/aretext/aretext@v1.3.0/state/view_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/aretext/aretext/text"
    10  )
    11  
    12  func TestScrollViewByNumLines(t *testing.T) {
    13  	testCases := []struct {
    14  		name               string
    15  		inputString        string
    16  		initialView        viewState
    17  		direction          ScrollDirection
    18  		numLines           uint64
    19  		expectedtextOrigin uint64
    20  	}{
    21  		{
    22  			name:               "empty, scroll up",
    23  			inputString:        "",
    24  			initialView:        viewState{textOrigin: 0, height: 100, width: 100},
    25  			direction:          ScrollDirectionBackward,
    26  			numLines:           1,
    27  			expectedtextOrigin: 0,
    28  		},
    29  		{
    30  			name:               "empty, scroll down",
    31  			inputString:        "",
    32  			initialView:        viewState{textOrigin: 0, height: 100, width: 100},
    33  			direction:          ScrollDirectionForward,
    34  			numLines:           1,
    35  			expectedtextOrigin: 0,
    36  		},
    37  		{
    38  			name:               "scroll up",
    39  			inputString:        "ab\ncd\nef\ngh\nij\nkl\nmn",
    40  			initialView:        viewState{textOrigin: 12, height: 2, width: 100},
    41  			direction:          ScrollDirectionBackward,
    42  			numLines:           3,
    43  			expectedtextOrigin: 3,
    44  		},
    45  		{
    46  			name:               "scroll down",
    47  			inputString:        "ab\ncd\nef\ngh\nij\nkl\nmn",
    48  			initialView:        viewState{textOrigin: 3, height: 2, width: 100},
    49  			direction:          ScrollDirectionForward,
    50  			numLines:           3,
    51  			expectedtextOrigin: 12,
    52  		},
    53  		{
    54  			name:               "scroll down to last line",
    55  			inputString:        "ab\ncd\nef\ngh\nij\nkl\nmn",
    56  			initialView:        viewState{textOrigin: 0, height: 6, width: 100},
    57  			numLines:           10,
    58  			expectedtextOrigin: 12,
    59  		},
    60  		{
    61  			name:               "scroll down view taller than document",
    62  			inputString:        "ab\ncd\nef\ngh",
    63  			initialView:        viewState{textOrigin: 0, height: 100, width: 100},
    64  			numLines:           1,
    65  			expectedtextOrigin: 0,
    66  		},
    67  	}
    68  
    69  	for _, tc := range testCases {
    70  		t.Run(tc.name, func(t *testing.T) {
    71  			textTree, err := text.NewTreeFromString(tc.inputString)
    72  			require.NoError(t, err)
    73  			state := NewEditorState(100, 100, nil, nil)
    74  			state.documentBuffer.textTree = textTree
    75  			state.documentBuffer.view = tc.initialView
    76  			ScrollViewByNumLines(state, tc.direction, tc.numLines)
    77  			assert.Equal(t, tc.expectedtextOrigin, state.documentBuffer.view.textOrigin)
    78  		})
    79  	}
    80  }