github.com/aretext/aretext@v1.3.0/selection/selector_test.go (about) 1 package selection 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 TestSelector(t *testing.T) { 13 testCases := []struct { 14 name string 15 inputString string 16 mode Mode 17 initialCursorPos uint64 18 finalCursorPos uint64 19 expectedRegion Region 20 }{ 21 { 22 name: "no selection", 23 inputString: "abcd", 24 mode: ModeNone, 25 initialCursorPos: 1, 26 finalCursorPos: 3, 27 expectedRegion: Region{}, 28 }, 29 { 30 name: "charwise, no cursor movement", 31 inputString: "abcdefghijklmnop", 32 mode: ModeChar, 33 initialCursorPos: 3, 34 finalCursorPos: 3, 35 expectedRegion: Region{ 36 StartPos: 3, 37 EndPos: 4, 38 }, 39 }, 40 { 41 name: "charwise, cursor movement forward", 42 inputString: "abcdefghijklmnop", 43 mode: ModeChar, 44 initialCursorPos: 3, 45 finalCursorPos: 6, 46 expectedRegion: Region{ 47 StartPos: 3, 48 EndPos: 7, 49 }, 50 }, 51 { 52 name: "charwise, cursor movement forward to end of document", 53 inputString: "abcd", 54 mode: ModeChar, 55 initialCursorPos: 2, 56 finalCursorPos: 4, 57 expectedRegion: Region{ 58 StartPos: 2, 59 EndPos: 4, 60 }, 61 }, 62 { 63 name: "charwise, cursor movement backward", 64 inputString: "abcdefghijklmnop", 65 mode: ModeChar, 66 initialCursorPos: 6, 67 finalCursorPos: 3, 68 expectedRegion: Region{ 69 StartPos: 3, 70 EndPos: 7, 71 }, 72 }, 73 { 74 name: "charwise, cursor movement backward to start of document", 75 inputString: "abcdefghijklmnop", 76 mode: ModeChar, 77 initialCursorPos: 6, 78 finalCursorPos: 0, 79 expectedRegion: Region{ 80 StartPos: 0, 81 EndPos: 7, 82 }, 83 }, 84 { 85 name: "charwise select last empty line", 86 inputString: "abc\n", 87 mode: ModeChar, 88 initialCursorPos: 4, 89 finalCursorPos: 4, 90 expectedRegion: Region{ 91 StartPos: 4, 92 EndPos: 4, 93 }, 94 }, 95 { 96 name: "linewise, no cursor movement", 97 inputString: "abcd\nefgh\nijkl\nmnop", 98 mode: ModeLine, 99 initialCursorPos: 6, 100 finalCursorPos: 6, 101 expectedRegion: Region{ 102 StartPos: 5, 103 EndPos: 9, 104 }, 105 }, 106 { 107 name: "linewise, cursor movement in same line", 108 inputString: "abcd\nefgh\nijkl\nmnop", 109 mode: ModeLine, 110 initialCursorPos: 6, 111 finalCursorPos: 8, 112 expectedRegion: Region{ 113 StartPos: 5, 114 EndPos: 9, 115 }, 116 }, 117 { 118 name: "linewise, cursor movement to line below", 119 inputString: "abcd\nefgh\nijkl\nmnop", 120 mode: ModeLine, 121 initialCursorPos: 6, 122 finalCursorPos: 15, 123 expectedRegion: Region{ 124 StartPos: 5, 125 EndPos: 19, 126 }, 127 }, 128 { 129 name: "linewise, cursor movement to line above", 130 inputString: "abcd\nefgh\nijkl\nmnop", 131 mode: ModeLine, 132 initialCursorPos: 15, 133 finalCursorPos: 6, 134 expectedRegion: Region{ 135 StartPos: 5, 136 EndPos: 19, 137 }, 138 }, 139 { 140 name: "linewise, cursor movement to blank line", 141 inputString: "abcd\n\n\nefgh", 142 mode: ModeLine, 143 initialCursorPos: 2, 144 finalCursorPos: 5, 145 expectedRegion: Region{ 146 StartPos: 0, 147 EndPos: 5, 148 }, 149 }, 150 { 151 name: "linewise select last empty line", 152 inputString: "abc\n", 153 mode: ModeLine, 154 initialCursorPos: 4, 155 finalCursorPos: 4, 156 expectedRegion: Region{ 157 StartPos: 4, 158 EndPos: 4, 159 }, 160 }, 161 } 162 163 for _, tc := range testCases { 164 t.Run(tc.name, func(t *testing.T) { 165 tree, err := text.NewTreeFromString(tc.inputString) 166 require.NoError(t, err) 167 var s Selector 168 s.Start(tc.mode, tc.initialCursorPos) 169 r := s.Region(tree, tc.finalCursorPos) 170 assert.Equal(t, tc.expectedRegion, r) 171 }) 172 } 173 }