github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/edit/ui/buffer_test.go (about) 1 package ui 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 var cellsWidthTests = []struct { 9 cells []Cell 10 wantWidth int 11 }{ 12 {[]Cell{}, 0}, 13 {[]Cell{{"a", 1, ""}, {"好", 2, ""}}, 3}, 14 } 15 16 func TestCellsWidth(t *testing.T) { 17 for _, test := range cellsWidthTests { 18 if width := CellsWidth(test.cells); width != test.wantWidth { 19 t.Errorf("cellsWidth(%v) = %v, want %v", 20 test.cells, width, test.wantWidth) 21 } 22 } 23 } 24 25 var makeSpacingTests = []struct { 26 n int 27 want []Cell 28 }{ 29 {0, []Cell{}}, 30 {1, []Cell{{" ", 1, ""}}}, 31 {4, []Cell{{" ", 1, ""}, {" ", 1, ""}, {" ", 1, ""}, {" ", 1, ""}}}, 32 } 33 34 func TestMakeSpacing(t *testing.T) { 35 for _, test := range makeSpacingTests { 36 if got := makeSpacing(test.n); !reflect.DeepEqual(got, test.want) { 37 t.Errorf("makeSpacing(%v) = %v, want %v", test.n, got, test.want) 38 } 39 } 40 } 41 42 var compareCellsTests = []struct { 43 cells1 []Cell 44 cells2 []Cell 45 wantEqual bool 46 wantIndex int 47 }{ 48 {[]Cell{}, []Cell{}, true, 0}, 49 {[]Cell{}, []Cell{{"a", 1, ""}}, false, 0}, 50 { 51 []Cell{{"a", 1, ""}, {"好", 2, ""}, {"b", 1, ""}}, 52 []Cell{{"a", 1, ""}, {"好", 2, ""}, {"c", 1, ""}}, 53 false, 2, 54 }, 55 { 56 []Cell{{"a", 1, ""}, {"好", 2, ""}, {"b", 1, ""}}, 57 []Cell{{"a", 1, ""}, {"好", 2, "1"}, {"c", 1, ""}}, 58 false, 1, 59 }, 60 } 61 62 func TestCompareCells(t *testing.T) { 63 for _, test := range compareCellsTests { 64 equal, index := CompareCells(test.cells1, test.cells2) 65 if equal != test.wantEqual || index != test.wantIndex { 66 t.Errorf("compareCells(%v, %v) = (%v, %v), want (%v, %v)", 67 test.cells1, test.cells2, 68 equal, index, test.wantEqual, test.wantIndex) 69 } 70 } 71 } 72 73 var bufferCursorTests = []struct { 74 buf *Buffer 75 want Pos 76 }{ 77 {NewBuffer(10), Pos{0, 0}}, 78 {NewBuffer(10).SetLines([]Cell{{"a", 1, ""}}, []Cell{{"好", 2, ""}}), 79 Pos{1, 2}}, 80 } 81 82 func TestBufferCursor(t *testing.T) { 83 for _, test := range bufferCursorTests { 84 if p := test.buf.Cursor(); p != test.want { 85 t.Errorf("(%v).cursor() = %v, want %v", test.buf, p, test.want) 86 } 87 } 88 } 89 90 var buffersHeighTests = []struct { 91 buffers []*Buffer 92 want int 93 }{ 94 {nil, 0}, 95 {[]*Buffer{NewBuffer(10)}, 1}, 96 {[]*Buffer{ 97 NewBuffer(10).SetLines([]Cell{}, []Cell{}), 98 NewBuffer(10), 99 NewBuffer(10).SetLines([]Cell{}, []Cell{})}, 100 5}, 101 } 102 103 func TestBuffersHeight(t *testing.T) { 104 for _, test := range buffersHeighTests { 105 if h := BuffersHeight(test.buffers...); h != test.want { 106 t.Errorf("buffersHeight(%v...) = %v, want %v", 107 test.buffers, h, test.want) 108 } 109 } 110 } 111 112 var bufferWritesTests = []struct { 113 buf *Buffer 114 text string 115 style string 116 want *Buffer 117 }{ 118 // Writing nothing. 119 {NewBuffer(10), "", "", NewBuffer(10)}, 120 // Writing a single rune. 121 {NewBuffer(10), "a", "1", NewBuffer(10).SetLines([]Cell{{"a", 1, "1"}})}, 122 // Writing control character. 123 {NewBuffer(10), "\033", "", 124 NewBuffer(10).SetLines( 125 []Cell{{"^[", 2, styleForControlChar.String()}}, 126 )}, 127 // Writing styled control character. 128 {NewBuffer(10), "a\033b", "1", 129 NewBuffer(10).SetLines( 130 []Cell{ 131 {"a", 1, "1"}, 132 {"^[", 2, "1;" + styleForControlChar.String()}, 133 {"b", 1, "1"}, 134 }, 135 )}, 136 // Writing text containing a newline. 137 {NewBuffer(10), "a\nb", "1", 138 NewBuffer(10).SetLines( 139 []Cell{{"a", 1, "1"}}, []Cell{{"b", 1, "1"}}, 140 )}, 141 // Writing text containing a newline when there is indent. 142 {NewBuffer(10).SetIndent(2), "a\nb", "1", 143 NewBuffer(10).SetIndent(2).SetLines( 144 []Cell{{"a", 1, "1"}}, 145 []Cell{{" ", 1, ""}, {" ", 1, ""}, {"b", 1, "1"}}, 146 )}, 147 // Writing long text that triggers wrapping. 148 {NewBuffer(4), "aaaab", "1", 149 NewBuffer(4).SetLines( 150 []Cell{{"a", 1, "1"}, {"a", 1, "1"}, {"a", 1, "1"}, {"a", 1, "1"}}, 151 []Cell{{"b", 1, "1"}}, 152 )}, 153 // Writing long text that triggers wrapping when there is indent. 154 {NewBuffer(4).SetIndent(2), "aaaab", "1", 155 NewBuffer(4).SetIndent(2).SetLines( 156 []Cell{{"a", 1, "1"}, {"a", 1, "1"}, {"a", 1, "1"}, {"a", 1, "1"}}, 157 []Cell{{" ", 1, ""}, {" ", 1, ""}, {"b", 1, "1"}}, 158 )}, 159 // Writing long text that triggers eager wrapping. 160 {NewBuffer(4).SetIndent(2).SetEagerWrap(true), "aaaa", "1", 161 NewBuffer(4).SetIndent(2).SetEagerWrap(true).SetLines( 162 []Cell{{"a", 1, "1"}, {"a", 1, "1"}, {"a", 1, "1"}, {"a", 1, "1"}}, 163 []Cell{{" ", 1, ""}, {" ", 1, ""}}, 164 )}, 165 } 166 167 // TestBufferWrites tests buffer.writes by calling writes on a buffer and see if 168 // the buffer matches what is expected. 169 func TestBufferWrites(t *testing.T) { 170 for _, test := range bufferWritesTests { 171 b := test.buf 172 b.WriteString(test.text, test.style) 173 if !reflect.DeepEqual(b, test.want) { 174 t.Errorf("buf.writes(%q, %q) makes it %v, want %v", 175 test.text, test.style, b, test.want) 176 } 177 } 178 } 179 180 var bufferTrimToLinesTests = []struct { 181 buf *Buffer 182 low int 183 high int 184 want *Buffer 185 }{ 186 { 187 NewBuffer(10).SetLines( 188 []Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}, 189 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, 190 ), 0, 2, 191 NewBuffer(10).SetLines( 192 []Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}, 193 ), 194 }, 195 // With dot. 196 { 197 NewBuffer(10).SetLines( 198 []Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}, 199 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, 200 ).SetDot(Pos{1, 1}), 1, 3, 201 NewBuffer(10).SetLines( 202 []Cell{{"b", 1, ""}}, []Cell{{"c", 1, ""}}, 203 ).SetDot(Pos{0, 1}), 204 }, 205 // With dot that is going to be trimmed away. 206 { 207 NewBuffer(10).SetLines( 208 []Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}, 209 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, 210 ).SetDot(Pos{0, 1}), 1, 3, 211 NewBuffer(10).SetLines( 212 []Cell{{"b", 1, ""}}, []Cell{{"c", 1, ""}}, 213 ).SetDot(Pos{0, 1}), 214 }, 215 } 216 217 func TestBufferTrimToLines(t *testing.T) { 218 for _, test := range bufferTrimToLinesTests { 219 b := test.buf 220 b.TrimToLines(test.low, test.high) 221 if !reflect.DeepEqual(b, test.want) { 222 t.Errorf("buf.trimToLines(%v, %v) makes it %v, want %v", 223 test.low, test.high, b, test.want) 224 } 225 } 226 } 227 228 var bufferExtendTests = []struct { 229 buf *Buffer 230 buf2 *Buffer 231 moveDot bool 232 want *Buffer 233 }{ 234 { 235 NewBuffer(10).SetLines([]Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}), 236 NewBuffer(11).SetLines([]Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}), 237 false, 238 NewBuffer(10).SetLines( 239 []Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}, 240 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, 241 ), 242 }, 243 // Moving dot. 244 { 245 NewBuffer(10).SetLines([]Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}), 246 NewBuffer(11).SetLines( 247 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, 248 ).SetDot(Pos{1, 1}), 249 true, 250 NewBuffer(10).SetLines( 251 []Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}, 252 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, 253 ).SetDot(Pos{3, 1}), 254 }, 255 } 256 257 func TestExtend(t *testing.T) { 258 for _, test := range bufferExtendTests { 259 b := test.buf 260 b.Extend(test.buf2, test.moveDot) 261 if !reflect.DeepEqual(b, test.want) { 262 t.Errorf("buf.extend(%v, %v) makes it %v, want %v", 263 test.buf2, test.moveDot, b, test.want) 264 } 265 } 266 } 267 268 var bufferExtendRightTests = []struct { 269 buf *Buffer 270 buf2 *Buffer 271 w int 272 want *Buffer 273 }{ 274 // No padding, equal height. 275 { 276 NewBuffer(10).SetLines([]Cell{{"a", 1, ""}}, []Cell{}), 277 NewBuffer(11).SetLines([]Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}), 278 0, 279 NewBuffer(10).SetLines( 280 []Cell{{"a", 1, ""}, {"c", 1, ""}}, 281 []Cell{{"d", 1, ""}}, 282 ), 283 }, 284 // With padding. 285 { 286 NewBuffer(10).SetLines([]Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}), 287 NewBuffer(11).SetLines([]Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}), 288 2, 289 NewBuffer(10).SetLines( 290 []Cell{{"a", 1, ""}, {" ", 1, ""}, {"c", 1, ""}}, 291 []Cell{{"b", 1, ""}, {" ", 1, ""}, {"d", 1, ""}}, 292 ), 293 }, 294 // buf is higher. 295 { 296 NewBuffer(10).SetLines( 297 []Cell{{"a", 1, ""}}, 298 []Cell{{"b", 1, ""}}, 299 []Cell{{"x", 1, ""}}, 300 ), 301 NewBuffer(11).SetLines([]Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}), 302 1, 303 NewBuffer(10).SetLines( 304 []Cell{{"a", 1, ""}, {"c", 1, ""}}, 305 []Cell{{"b", 1, ""}, {"d", 1, ""}}, 306 []Cell{{"x", 1, ""}}, 307 ), 308 }, 309 // buf2 is higher. 310 { 311 NewBuffer(10).SetLines([]Cell{{"a", 1, ""}}, []Cell{{"b", 1, ""}}), 312 NewBuffer(11).SetLines( 313 []Cell{{"c", 1, ""}}, []Cell{{"d", 1, ""}}, []Cell{{"e", 1, ""}}, 314 ), 315 1, 316 NewBuffer(10).SetLines( 317 []Cell{{"a", 1, ""}, {"c", 1, ""}}, 318 []Cell{{"b", 1, ""}, {"d", 1, ""}}, 319 []Cell{{" ", 1, ""}, {"e", 1, ""}}, 320 ), 321 }, 322 } 323 324 func TestExtendRight(t *testing.T) { 325 for _, test := range bufferExtendRightTests { 326 b := test.buf 327 b.ExtendRight(test.buf2, test.w) 328 if !reflect.DeepEqual(b, test.want) { 329 t.Errorf("buf.extendRight(%v, %v) makes it %v, want %v", 330 test.buf2, test.w, b, test.want) 331 } 332 } 333 }