github.com/aretext/aretext@v1.3.0/state/locator_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/selection" 10 "github.com/aretext/aretext/text" 11 ) 12 13 func TestSelectionEndLocator(t *testing.T) { 14 testCases := []struct { 15 name string 16 originalText string 17 selectionMode selection.Mode 18 selectionStartPos uint64 19 selectionEndPos uint64 20 newText string 21 cursorPos uint64 22 expectedEndPos uint64 23 }{ 24 { 25 name: "charwise, empty document to non-empty document", 26 originalText: "", 27 selectionMode: selection.ModeChar, 28 selectionStartPos: 0, 29 selectionEndPos: 0, 30 newText: "ijklmnop", 31 cursorPos: 0, 32 expectedEndPos: 0, 33 }, 34 { 35 name: "charwise, non-empty document to empty document", 36 originalText: "abcdefgh", 37 selectionMode: selection.ModeChar, 38 selectionStartPos: 0, 39 selectionEndPos: 2, 40 newText: "", 41 cursorPos: 0, 42 expectedEndPos: 0, 43 }, 44 { 45 name: "charwise, same line", 46 originalText: "abcdefgh", 47 selectionMode: selection.ModeChar, 48 selectionStartPos: 1, 49 selectionEndPos: 3, 50 newText: "ijklmnop", 51 cursorPos: 4, 52 expectedEndPos: 7, 53 }, 54 { 55 name: "charwise, same line, next line shorter", 56 originalText: "abcdefgh", 57 selectionMode: selection.ModeChar, 58 selectionStartPos: 1, 59 selectionEndPos: 3, 60 newText: "ijklmn", 61 cursorPos: 4, 62 expectedEndPos: 6, 63 }, 64 { 65 name: "charwise, to end of first line", 66 originalText: "abcd\nefgh\nijkl", 67 selectionMode: selection.ModeChar, 68 selectionStartPos: 1, 69 selectionEndPos: 3, 70 newText: "ijklm\nnopqrstuv\nwxyz", 71 cursorPos: 7, 72 expectedEndPos: 10, 73 }, 74 { 75 name: "charwise, past end of first line", 76 originalText: "abcd\nefgh\nijkl", 77 selectionMode: selection.ModeChar, 78 selectionStartPos: 1, 79 selectionEndPos: 4, 80 newText: "ijklm\nnopqrstuv\nwxyz", 81 cursorPos: 7, 82 expectedEndPos: 16, 83 }, 84 { 85 name: "charwise, to start of next line", 86 originalText: "abcd\nefgh\nijkl", 87 selectionMode: selection.ModeChar, 88 selectionStartPos: 1, 89 selectionEndPos: 5, 90 newText: "ijklm\nnopqrstuv\nwxyz", 91 cursorPos: 7, 92 expectedEndPos: 17, 93 }, 94 { 95 name: "charwise, past start of next line", 96 originalText: "abcd\nefgh\nijkl", 97 selectionMode: selection.ModeChar, 98 selectionStartPos: 1, 99 selectionEndPos: 7, 100 newText: "ijklm\nnopqrstuv\nwxyz", 101 cursorPos: 7, 102 expectedEndPos: 19, 103 }, 104 { 105 name: "charwise, past start of next line, next line shorter", 106 originalText: "abcd\nefgh\nijkl", 107 selectionMode: selection.ModeChar, 108 selectionStartPos: 1, 109 selectionEndPos: 7, 110 newText: "ijklm\nnopqrstuv\nwx\nyz", 111 cursorPos: 7, 112 expectedEndPos: 18, 113 }, 114 { 115 name: "charwise, past start of next line, next line eof", 116 originalText: "abcd\nefgh\nijkl", 117 selectionMode: selection.ModeChar, 118 selectionStartPos: 1, 119 selectionEndPos: 7, 120 newText: "ijklm\nnopqrstuv\nw", 121 cursorPos: 7, 122 expectedEndPos: 17, 123 }, 124 { 125 name: "charwise, past start of next line, new line is last line", 126 originalText: "abcd\nefgh\nijkl", 127 selectionMode: selection.ModeChar, 128 selectionStartPos: 3, 129 selectionEndPos: 5, 130 newText: "ijklm\nnopq", 131 cursorPos: 8, 132 expectedEndPos: 10, 133 }, 134 { 135 name: "linewise, empty to non-empty document", 136 originalText: "", 137 selectionMode: selection.ModeLine, 138 selectionStartPos: 0, 139 selectionEndPos: 0, 140 newText: "ijklm\nnopq", 141 cursorPos: 1, 142 expectedEndPos: 5, 143 }, 144 { 145 name: "linewise, non-empty to empty document", 146 originalText: "abcdefgh\nhijkl", 147 selectionMode: selection.ModeLine, 148 selectionStartPos: 0, 149 selectionEndPos: 8, 150 newText: "", 151 cursorPos: 0, 152 expectedEndPos: 0, 153 }, 154 { 155 name: "linewise, single line to longer single line", 156 originalText: "abc", 157 selectionMode: selection.ModeLine, 158 selectionStartPos: 0, 159 selectionEndPos: 3, 160 newText: "efghijkl", 161 cursorPos: 0, 162 expectedEndPos: 8, 163 }, 164 { 165 name: "linewise, single line to shorter single line", 166 originalText: "abcdefgh", 167 selectionMode: selection.ModeLine, 168 selectionStartPos: 0, 169 selectionEndPos: 8, 170 newText: "efg", 171 cursorPos: 0, 172 expectedEndPos: 3, 173 }, 174 { 175 name: "linewise, select to line below", 176 originalText: "abcd\nefgh\nijkl", 177 selectionMode: selection.ModeLine, 178 selectionStartPos: 0, 179 selectionEndPos: 5, 180 newText: "mn\nop\nqr", 181 cursorPos: 0, 182 expectedEndPos: 5, 183 }, 184 { 185 name: "linewise, select past end of document", 186 originalText: "abcd\nefgh\nijkl", 187 selectionMode: selection.ModeLine, 188 selectionStartPos: 0, 189 selectionEndPos: 5, 190 newText: "mn\nop", 191 cursorPos: 3, 192 expectedEndPos: 5, 193 }, 194 } 195 196 for _, tc := range testCases { 197 t.Run(tc.name, func(t *testing.T) { 198 textTree, err := text.NewTreeFromString(tc.originalText) 199 require.NoError(t, err) 200 selector := &selection.Selector{} 201 selector.Start(tc.selectionMode, tc.selectionStartPos) 202 cursorPos := tc.selectionEndPos 203 endLoc := SelectionEndLocator(textTree, cursorPos, selector) 204 205 newTextTree, err := text.NewTreeFromString(tc.newText) 206 require.NoError(t, err) 207 endPos := endLoc(LocatorParams{ 208 TextTree: newTextTree, 209 CursorPos: tc.cursorPos, 210 }) 211 assert.Equal(t, tc.expectedEndPos, endPos) 212 }) 213 } 214 }