github.com/elves/elvish@v0.15.0/pkg/cli/term/buffer_test.go (about) 1 package term 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", ""}, {"好", ""}}, 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{{" ", ""}}}, 31 {4, []Cell{{" ", ""}, {" ", ""}, {" ", ""}, {" ", ""}}}, 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", ""}}, false, 0}, 50 { 51 []Cell{{"a", ""}, {"好", ""}, {"b", ""}}, 52 []Cell{{"a", ""}, {"好", ""}, {"c", ""}}, 53 false, 2, 54 }, 55 { 56 []Cell{{"a", ""}, {"好", ""}, {"b", ""}}, 57 []Cell{{"a", ""}, {"好", "1"}, {"c", ""}}, 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 { 78 &Buffer{Width: 10, Lines: Lines{Line{}}}, 79 Pos{0, 0}, 80 }, 81 { 82 &Buffer{Width: 10, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"好", ""}}}}, 83 Pos{1, 2}, 84 }, 85 } 86 87 func TestBufferCursor(t *testing.T) { 88 for _, test := range bufferCursorTests { 89 if p := test.buf.Cursor(); p != test.want { 90 t.Errorf("(%v).cursor() = %v, want %v", test.buf, p, test.want) 91 } 92 } 93 } 94 95 var buffersHeighTests = []struct { 96 buffers []*Buffer 97 want int 98 }{ 99 {nil, 0}, 100 {[]*Buffer{NewBuffer(10)}, 1}, 101 { 102 []*Buffer{ 103 {Width: 10, Lines: Lines{Line{}, Line{}}}, 104 {Width: 10, Lines: Lines{Line{}}}, 105 {Width: 10, Lines: Lines{Line{}, Line{}}}, 106 }, 107 5, 108 }, 109 } 110 111 func TestBuffersHeight(t *testing.T) { 112 for _, test := range buffersHeighTests { 113 if h := BuffersHeight(test.buffers...); h != test.want { 114 t.Errorf("buffersHeight(%v...) = %v, want %v", 115 test.buffers, h, test.want) 116 } 117 } 118 } 119 120 var bufferTrimToLinesTests = []struct { 121 buf *Buffer 122 low int 123 high int 124 want *Buffer 125 }{ 126 { 127 &Buffer{Width: 10, Lines: Lines{ 128 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}}, 129 }}, 130 0, 2, 131 &Buffer{Width: 10, Lines: Lines{ 132 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, 133 }}, 134 }, 135 // With dot. 136 { 137 &Buffer{Width: 10, Lines: Lines{ 138 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}}, 139 }, Dot: Pos{1, 1}}, 140 1, 3, 141 &Buffer{Width: 10, Lines: Lines{ 142 Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, 143 }, Dot: Pos{0, 1}}, 144 }, 145 // With dot that is going to be trimmed away. 146 { 147 &Buffer{Width: 10, Lines: Lines{ 148 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}}, 149 }, Dot: Pos{0, 1}}, 150 1, 3, 151 &Buffer{Width: 10, Lines: Lines{ 152 Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, 153 }, Dot: Pos{0, 1}}, 154 }, 155 } 156 157 func TestBufferTrimToLines(t *testing.T) { 158 for _, test := range bufferTrimToLinesTests { 159 b := test.buf 160 b.TrimToLines(test.low, test.high) 161 if !reflect.DeepEqual(b, test.want) { 162 t.Errorf("buf.trimToLines(%v, %v) makes it %v, want %v", 163 test.low, test.high, b, test.want) 164 } 165 } 166 } 167 168 var bufferExtendTests = []struct { 169 buf *Buffer 170 buf2 *Buffer 171 moveDot bool 172 want *Buffer 173 }{ 174 { 175 &Buffer{Width: 10, Lines: Lines{ 176 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}}, 177 &Buffer{Width: 11, Lines: Lines{ 178 Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}}, 179 false, 180 &Buffer{Width: 10, Lines: Lines{ 181 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, 182 Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}}, 183 }, 184 // Moving dot. 185 { 186 &Buffer{Width: 10, Lines: Lines{ 187 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}}, 188 &Buffer{ 189 Width: 11, 190 Lines: Lines{Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}, 191 Dot: Pos{1, 1}, 192 }, 193 true, 194 &Buffer{ 195 Width: 10, 196 Lines: Lines{ 197 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, 198 Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}, 199 Dot: Pos{3, 1}, 200 }, 201 }, 202 } 203 204 func TestBufferExtend(t *testing.T) { 205 for _, test := range bufferExtendTests { 206 buf := test.buf 207 buf.Extend(test.buf2, test.moveDot) 208 if !reflect.DeepEqual(buf, test.want) { 209 t.Errorf("buf.extend(%v, %v) makes it %v, want %v", 210 test.buf2, test.moveDot, buf, test.want) 211 } 212 } 213 } 214 215 var bufferExtendRightTests = []struct { 216 buf *Buffer 217 buf2 *Buffer 218 want *Buffer 219 }{ 220 // No padding, equal height. 221 { 222 &Buffer{Width: 1, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}}, 223 &Buffer{Width: 1, Lines: Lines{Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}}, 224 &Buffer{Width: 2, Lines: Lines{ 225 Line{Cell{"a", ""}, Cell{"c", ""}}, 226 Line{Cell{"b", ""}, Cell{"d", ""}}}}, 227 }, 228 // With padding, equal height. 229 { 230 &Buffer{Width: 2, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}}, 231 &Buffer{Width: 1, Lines: Lines{Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}}, 232 &Buffer{Width: 3, Lines: Lines{ 233 Line{Cell{"a", ""}, Cell{" ", ""}, Cell{"c", ""}}, 234 Line{Cell{"b", ""}, Cell{" ", ""}, Cell{"d", ""}}}}, 235 }, 236 // buf is higher. 237 { 238 &Buffer{Width: 1, Lines: Lines{ 239 Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"x", ""}}}}, 240 &Buffer{Width: 1, Lines: Lines{ 241 Line{Cell{"c", ""}}, Line{Cell{"d", ""}}, 242 }}, 243 &Buffer{Width: 2, Lines: Lines{ 244 Line{Cell{"a", ""}, Cell{"c", ""}}, 245 Line{Cell{"b", ""}, Cell{"d", ""}}, 246 Line{Cell{"x", ""}}}}, 247 }, 248 // buf2 is higher. 249 { 250 &Buffer{Width: 1, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}}, 251 &Buffer{Width: 1, Lines: Lines{ 252 Line{Cell{"c", ""}}, Line{Cell{"d", ""}}, Line{Cell{"e", ""}}, 253 }}, 254 &Buffer{Width: 2, Lines: Lines{ 255 Line{Cell{"a", ""}, Cell{"c", ""}}, 256 Line{Cell{"b", ""}, Cell{"d", ""}}, 257 Line{Cell{" ", ""}, Cell{"e", ""}}}}, 258 }, 259 } 260 261 func TestBufferExtendRight(t *testing.T) { 262 for _, test := range bufferExtendRightTests { 263 buf := test.buf 264 buf.ExtendRight(test.buf2) 265 if !reflect.DeepEqual(buf, test.want) { 266 t.Errorf("buf.extendRight(%v) makes it %v, want %v", 267 test.buf2, buf, test.want) 268 } 269 } 270 } 271 272 func TestBufferBuffer(t *testing.T) { 273 b := NewBufferBuilder(4).Write("text").Buffer() 274 if b.Buffer() != b { 275 t.Errorf("Buffer did not return itself") 276 } 277 } 278 279 var bufferTTYStringTests = []struct { 280 buf *Buffer 281 want string 282 }{ 283 { 284 NewBufferBuilder(4). 285 Write("ABCD"). 286 Newline(). 287 Write("XY"). 288 Buffer(), 289 "Width = 4, Dot = (0, 0)\n" + 290 "┌────┐\n" + 291 "│ABCD│\n" + 292 "│XY$ │\n" + 293 "└────┘\n", 294 }, 295 { 296 NewBufferBuilder(4). 297 Write("AB").SetDotHere(). 298 WriteStringSGR("C", "1"). 299 WriteStringSGR("D", "7"). 300 Newline(). 301 WriteStringSGR("XY", "7"). 302 Buffer(), 303 "Width = 4, Dot = (0, 2)\n" + 304 "┌────┐\n" + 305 "│AB\033[1mC\033[;7mD\033[m│\n" + 306 "│\033[7mXY\033[m$ │\n" + 307 "└────┘\n", 308 }, 309 } 310 311 func TestBufferTTYString(t *testing.T) { 312 for _, test := range bufferTTYStringTests { 313 ttyString := test.buf.TTYString() 314 if ttyString != test.want { 315 t.Errorf("TTYString -> %q, want %q", ttyString, test.want) 316 } 317 } 318 }