| 1 | + | // Copyright 2018 Google Inc. |
| 2 | + | // |
| 3 | + | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + | // you may not use this file except in compliance with the License. |
| 5 | + | // You may obtain a copy of the License at |
| 6 | + | // |
| 7 | + | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + | // |
| 9 | + | // Unless required by applicable law or agreed to in writing, software |
| 10 | + | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + | // See the License for the specific language governing permissions and |
| 13 | + | // limitations under the License. |
| 14 | + | |
| 15 | + | package canvas |
| 16 | + | |
| 17 | + | import ( |
| 18 | + | "image" |
| 19 | + | "testing" |
| 20 | + | |
| 21 | + | "github.com/kylelemons/godebug/pretty" |
| 22 | + | "github.com/mum4k/termdash/internal/area" |
| 23 | + | "github.com/mum4k/termdash/internal/cell" |
| 24 | + | "github.com/mum4k/termdash/internal/terminal/faketerm" |
| 25 | + | ) |
| 26 | + | |
| 27 | + | func TestNew(t *testing.T) { |
| 28 | + | tests := []struct { |
| 29 | + | desc string |
| 30 | + | area image.Rectangle |
| 31 | + | wantSize image.Point |
| 32 | + | wantArea image.Rectangle |
| 33 | + | wantErr bool |
| 34 | + | }{ |
| 35 | + | { |
| 36 | + | desc: "area min has negative X", |
| 37 | + | area: image.Rect(-1, 0, 0, 0), |
| 38 | + | wantErr: true, |
| 39 | + | }, |
| 40 | + | { |
| 41 | + | desc: "area min has negative Y", |
| 42 | + | area: image.Rect(0, -1, 0, 0), |
| 43 | + | wantErr: true, |
| 44 | + | }, |
| 45 | + | { |
| 46 | + | desc: "area max has negative X", |
| 47 | + | area: image.Rect(0, 0, -1, 0), |
| 48 | + | wantErr: true, |
| 49 | + | }, |
| 50 | + | { |
| 51 | + | desc: "area max has negative Y", |
| 52 | + | area: image.Rect(0, 0, 0, -1), |
| 53 | + | wantErr: true, |
| 54 | + | }, |
| 55 | + | { |
| 56 | + | desc: "zero area is invalid", |
| 57 | + | area: image.Rect(0, 0, 0, 0), |
| 58 | + | wantErr: true, |
| 59 | + | }, |
| 60 | + | { |
| 61 | + | desc: "smallest valid size", |
| 62 | + | area: image.Rect(0, 0, 1, 1), |
| 63 | + | wantSize: image.Point{1, 1}, |
| 64 | + | wantArea: image.Rect(0, 0, 1, 1), |
| 65 | + | }, |
| 66 | + | { |
| 67 | + | desc: "rectangular canvas 3 by 4", |
| 68 | + | area: image.Rect(0, 0, 3, 4), |
| 69 | + | wantSize: image.Point{3, 4}, |
| 70 | + | wantArea: image.Rect(0, 0, 3, 4), |
| 71 | + | }, |
| 72 | + | { |
| 73 | + | desc: "non-zero based area", |
| 74 | + | area: image.Rect(1, 1, 2, 3), |
| 75 | + | wantSize: image.Point{1, 2}, |
| 76 | + | wantArea: image.Rect(0, 0, 1, 2), |
| 77 | + | }, |
| 78 | + | } |
| 79 | + | |
| 80 | + | for _, tc := range tests { |
| 81 | + | t.Run(tc.desc, func(t *testing.T) { |
| 82 | + | c, err := New(tc.area) |
| 83 | + | if (err != nil) != tc.wantErr { |
| 84 | + | t.Errorf("New => unexpected error: %v, wantErr: %v", err, tc.wantErr) |
| 85 | + | } |
| 86 | + | if err != nil { |
| 87 | + | return |
| 88 | + | } |
| 89 | + | |
| 90 | + | gotSize := c.Size() |
| 91 | + | if diff := pretty.Compare(tc.wantSize, gotSize); diff != "" { |
| 92 | + | t.Errorf("Size => unexpected diff (-want, +got):\n%s", diff) |
| 93 | + | } |
| 94 | + | |
| 95 | + | gotArea := c.Area() |
| 96 | + | if diff := pretty.Compare(tc.wantArea, gotArea); diff != "" { |
| 97 | + | t.Errorf("Area => unexpected diff (-want, +got):\n%s", diff) |
| 98 | + | } |
| 99 | + | }) |
| 100 | + | } |
| 101 | + | } |
| 102 | + | |
| 103 | + | func TestCanvas(t *testing.T) { |
| 104 | + | tests := []struct { |
| 105 | + | desc string |
| 106 | + | canvas image.Rectangle |
| 107 | + | ops func(*Canvas) error |
| 108 | + | want func(size image.Point) (*faketerm.Terminal, error) |
| 109 | + | wantErr bool |
| 110 | + | }{ |
| 111 | + | { |
| 112 | + | desc: "SetCellOpts fails on a point outside of the canvas", |
| 113 | + | canvas: image.Rect(0, 0, 1, 1), |
| 114 | + | ops: func(cvs *Canvas) error { |
| 115 | + | return cvs.SetCellOpts(image.Point{1, 1}) |
| 116 | + | }, |
| 117 | + | wantErr: true, |
| 118 | + | }, |
| 119 | + | { |
| 120 | + | desc: "SetCellOpts sets options on a cell with no options", |
| 121 | + | canvas: image.Rect(0, 0, 2, 2), |
| 122 | + | ops: func(cvs *Canvas) error { |
| 123 | + | return cvs.SetCellOpts(image.Point{0, 1}, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 124 | + | }, |
| 125 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 126 | + | ft := faketerm.MustNew(size) |
| 127 | + | cvs, err := New(ft.Area()) |
| 128 | + | if err != nil { |
| 129 | + | return nil, err |
| 130 | + | } |
| 131 | + | |
| 132 | + | c, err := cvs.Cell(image.Point{0, 1}) |
| 133 | + | if err != nil { |
| 134 | + | return nil, err |
| 135 | + | } |
| 136 | + | if _, err := cvs.SetCell(image.Point{0, 1}, c.Rune, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 137 | + | return nil, err |
| 138 | + | } |
| 139 | + | |
| 140 | + | if err := cvs.Apply(ft); err != nil { |
| 141 | + | return nil, err |
| 142 | + | } |
| 143 | + | return ft, nil |
| 144 | + | }, |
| 145 | + | }, |
| 146 | + | { |
| 147 | + | desc: "SetCellOpts preserves cell rune", |
| 148 | + | canvas: image.Rect(0, 0, 2, 2), |
| 149 | + | ops: func(cvs *Canvas) error { |
| 150 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X'); err != nil { |
| 151 | + | return err |
| 152 | + | } |
| 153 | + | return cvs.SetCellOpts(image.Point{0, 1}, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 154 | + | }, |
| 155 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 156 | + | ft := faketerm.MustNew(size) |
| 157 | + | cvs, err := New(ft.Area()) |
| 158 | + | if err != nil { |
| 159 | + | return nil, err |
| 160 | + | } |
| 161 | + | |
| 162 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 163 | + | return nil, err |
| 164 | + | } |
| 165 | + | |
| 166 | + | if err := cvs.Apply(ft); err != nil { |
| 167 | + | return nil, err |
| 168 | + | } |
| 169 | + | return ft, nil |
| 170 | + | }, |
| 171 | + | }, |
| 172 | + | { |
| 173 | + | desc: "SetCellOpts overwrites options set previously", |
| 174 | + | canvas: image.Rect(0, 0, 2, 2), |
| 175 | + | ops: func(cvs *Canvas) error { |
| 176 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 177 | + | return err |
| 178 | + | } |
| 179 | + | return cvs.SetCellOpts(image.Point{0, 1}, cell.FgColor(cell.ColorGreen), cell.BgColor(cell.ColorYellow)) |
| 180 | + | }, |
| 181 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 182 | + | ft := faketerm.MustNew(size) |
| 183 | + | cvs, err := New(ft.Area()) |
| 184 | + | if err != nil { |
| 185 | + | return nil, err |
| 186 | + | } |
| 187 | + | |
| 188 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X', cell.FgColor(cell.ColorGreen), cell.BgColor(cell.ColorYellow)); err != nil { |
| 189 | + | return nil, err |
| 190 | + | } |
| 191 | + | |
| 192 | + | if err := cvs.Apply(ft); err != nil { |
| 193 | + | return nil, err |
| 194 | + | } |
| 195 | + | return ft, nil |
| 196 | + | }, |
| 197 | + | }, |
| 198 | + | { |
| 199 | + | desc: "SetCellOpts sets default options when no options provided", |
| 200 | + | canvas: image.Rect(0, 0, 2, 2), |
| 201 | + | ops: func(cvs *Canvas) error { |
| 202 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 203 | + | return err |
| 204 | + | } |
| 205 | + | return cvs.SetCellOpts(image.Point{0, 1}) |
| 206 | + | }, |
| 207 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 208 | + | ft := faketerm.MustNew(size) |
| 209 | + | cvs, err := New(ft.Area()) |
| 210 | + | if err != nil { |
| 211 | + | return nil, err |
| 212 | + | } |
| 213 | + | |
| 214 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X'); err != nil { |
| 215 | + | return nil, err |
| 216 | + | } |
| 217 | + | |
| 218 | + | if err := cvs.Apply(ft); err != nil { |
| 219 | + | return nil, err |
| 220 | + | } |
| 221 | + | return ft, nil |
| 222 | + | }, |
| 223 | + | }, |
| 224 | + | { |
| 225 | + | desc: "SetCellOpts is idempotent", |
| 226 | + | canvas: image.Rect(0, 0, 2, 2), |
| 227 | + | ops: func(cvs *Canvas) error { |
| 228 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X'); err != nil { |
| 229 | + | return err |
| 230 | + | } |
| 231 | + | if err := cvs.SetCellOpts(image.Point{0, 1}, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 232 | + | return err |
| 233 | + | } |
| 234 | + | return cvs.SetCellOpts(image.Point{0, 1}, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 235 | + | }, |
| 236 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 237 | + | ft := faketerm.MustNew(size) |
| 238 | + | cvs, err := New(ft.Area()) |
| 239 | + | if err != nil { |
| 240 | + | return nil, err |
| 241 | + | } |
| 242 | + | |
| 243 | + | if _, err := cvs.SetCell(image.Point{0, 1}, 'X', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 244 | + | return nil, err |
| 245 | + | } |
| 246 | + | |
| 247 | + | if err := cvs.Apply(ft); err != nil { |
| 248 | + | return nil, err |
| 249 | + | } |
| 250 | + | return ft, nil |
| 251 | + | }, |
| 252 | + | }, |
| 253 | + | { |
| 254 | + | desc: "SetAreaCellOpts fails on area too large", |
| 255 | + | canvas: image.Rect(0, 0, 1, 1), |
| 256 | + | ops: func(cvs *Canvas) error { |
| 257 | + | return cvs.SetAreaCellOpts(image.Rect(0, 0, 2, 2), cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 258 | + | }, |
| 259 | + | wantErr: true, |
| 260 | + | }, |
| 261 | + | { |
| 262 | + | desc: "SetAreaCellOpts sets options in the full canvas", |
| 263 | + | canvas: image.Rect(0, 0, 1, 1), |
| 264 | + | ops: func(cvs *Canvas) error { |
| 265 | + | return cvs.SetAreaCellOpts(image.Rect(0, 0, 1, 1), cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 266 | + | }, |
| 267 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 268 | + | ft := faketerm.MustNew(size) |
| 269 | + | cvs, err := New(ft.Area()) |
| 270 | + | if err != nil { |
| 271 | + | return nil, err |
| 272 | + | } |
| 273 | + | |
| 274 | + | for _, p := range []image.Point{ |
| 275 | + | {0, 0}, |
| 276 | + | } { |
| 277 | + | c, err := cvs.Cell(p) |
| 278 | + | if err != nil { |
| 279 | + | return nil, err |
| 280 | + | } |
| 281 | + | if _, err := cvs.SetCell(p, c.Rune, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 282 | + | return nil, err |
| 283 | + | } |
| 284 | + | } |
| 285 | + | |
| 286 | + | if err := cvs.Apply(ft); err != nil { |
| 287 | + | return nil, err |
| 288 | + | } |
| 289 | + | return ft, nil |
| 290 | + | }, |
| 291 | + | }, |
| 292 | + | { |
| 293 | + | desc: "SetAreaCellOpts sets options in a sub-area", |
| 294 | + | canvas: image.Rect(0, 0, 3, 3), |
| 295 | + | ops: func(cvs *Canvas) error { |
| 296 | + | return cvs.SetAreaCellOpts(image.Rect(0, 0, 2, 2), cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 297 | + | }, |
| 298 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 299 | + | ft := faketerm.MustNew(size) |
| 300 | + | cvs, err := New(ft.Area()) |
| 301 | + | if err != nil { |
| 302 | + | return nil, err |
| 303 | + | } |
| 304 | + | |
| 305 | + | for _, p := range []image.Point{ |
| 306 | + | {0, 0}, |
| 307 | + | {0, 1}, |
| 308 | + | {1, 0}, |
| 309 | + | {1, 1}, |
| 310 | + | } { |
| 311 | + | c, err := cvs.Cell(p) |
| 312 | + | if err != nil { |
| 313 | + | return nil, err |
| 314 | + | } |
| 315 | + | if _, err := cvs.SetCell(p, c.Rune, cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 316 | + | return nil, err |
| 317 | + | } |
| 318 | + | } |
| 319 | + | |
| 320 | + | if err := cvs.Apply(ft); err != nil { |
| 321 | + | return nil, err |
| 322 | + | } |
| 323 | + | return ft, nil |
| 324 | + | }, |
| 325 | + | }, |
| 326 | + | { |
| 327 | + | desc: "SetAreaCells sets cells in the full canvas", |
| 328 | + | canvas: image.Rect(0, 0, 1, 1), |
| 329 | + | ops: func(cvs *Canvas) error { |
| 330 | + | return cvs.SetAreaCells(image.Rect(0, 0, 1, 1), 'r') |
| 331 | + | }, |
| 332 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 333 | + | ft := faketerm.MustNew(size) |
| 334 | + | cvs, err := New(ft.Area()) |
| 335 | + | if err != nil { |
| 336 | + | return nil, err |
| 337 | + | } |
| 338 | + | |
| 339 | + | if _, err := cvs.SetCell(image.Point{0, 0}, 'r'); err != nil { |
| 340 | + | return nil, err |
| 341 | + | } |
| 342 | + | |
| 343 | + | if err := cvs.Apply(ft); err != nil { |
| 344 | + | return nil, err |
| 345 | + | } |
| 346 | + | return ft, nil |
| 347 | + | }, |
| 348 | + | }, |
| 349 | + | { |
| 350 | + | desc: "SetAreaCells is idempotent", |
| 351 | + | canvas: image.Rect(0, 0, 1, 1), |
| 352 | + | ops: func(cvs *Canvas) error { |
| 353 | + | if err := cvs.SetAreaCells(image.Rect(0, 0, 1, 1), 'r'); err != nil { |
| 354 | + | return err |
| 355 | + | } |
| 356 | + | return cvs.SetAreaCells(image.Rect(0, 0, 1, 1), 'r') |
| 357 | + | }, |
| 358 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 359 | + | ft := faketerm.MustNew(size) |
| 360 | + | cvs, err := New(ft.Area()) |
| 361 | + | if err != nil { |
| 362 | + | return nil, err |
| 363 | + | } |
| 364 | + | |
| 365 | + | if _, err := cvs.SetCell(image.Point{0, 0}, 'r'); err != nil { |
| 366 | + | return nil, err |
| 367 | + | } |
| 368 | + | |
| 369 | + | if err := cvs.Apply(ft); err != nil { |
| 370 | + | return nil, err |
| 371 | + | } |
| 372 | + | return ft, nil |
| 373 | + | }, |
| 374 | + | }, |
| 375 | + | { |
| 376 | + | desc: "SetAreaCells fails on area too large", |
| 377 | + | canvas: image.Rect(0, 0, 1, 1), |
| 378 | + | ops: func(cvs *Canvas) error { |
| 379 | + | return cvs.SetAreaCells(image.Rect(0, 0, 2, 2), 'r', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 380 | + | }, |
| 381 | + | wantErr: true, |
| 382 | + | }, |
| 383 | + | { |
| 384 | + | desc: "SetAreaCells sets cell options", |
| 385 | + | canvas: image.Rect(0, 0, 1, 1), |
| 386 | + | ops: func(cvs *Canvas) error { |
| 387 | + | return cvs.SetAreaCells(image.Rect(0, 0, 1, 1), 'r', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)) |
| 388 | + | }, |
| 389 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 390 | + | ft := faketerm.MustNew(size) |
| 391 | + | cvs, err := New(ft.Area()) |
| 392 | + | if err != nil { |
| 393 | + | return nil, err |
| 394 | + | } |
| 395 | + | |
| 396 | + | if _, err := cvs.SetCell(image.Point{0, 0}, 'r', cell.FgColor(cell.ColorRed), cell.BgColor(cell.ColorBlue)); err != nil { |
| 397 | + | return nil, err |
| 398 | + | } |
| 399 | + | |
| 400 | + | if err := cvs.Apply(ft); err != nil { |
| 401 | + | return nil, err |
| 402 | + | } |
| 403 | + | return ft, nil |
| 404 | + | }, |
| 405 | + | }, |
| 406 | + | { |
| 407 | + | desc: "SetAreaCells sets cell in a sub-area", |
| 408 | + | canvas: image.Rect(0, 0, 3, 3), |
| 409 | + | ops: func(cvs *Canvas) error { |
| 410 | + | return cvs.SetAreaCells(image.Rect(0, 0, 2, 2), 'p') |
| 411 | + | }, |
| 412 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 413 | + | ft := faketerm.MustNew(size) |
| 414 | + | cvs, err := New(ft.Area()) |
| 415 | + | if err != nil { |
| 416 | + | return nil, err |
| 417 | + | } |
| 418 | + | |
| 419 | + | for _, p := range []image.Point{ |
| 420 | + | {0, 0}, |
| 421 | + | {0, 1}, |
| 422 | + | {1, 0}, |
| 423 | + | {1, 1}, |
| 424 | + | } { |
| 425 | + | if _, err := cvs.SetCell(p, 'p'); err != nil { |
| 426 | + | return nil, err |
| 427 | + | } |
| 428 | + | } |
| 429 | + | |
| 430 | + | if err := cvs.Apply(ft); err != nil { |
| 431 | + | return nil, err |
| 432 | + | } |
| 433 | + | return ft, nil |
| 434 | + | }, |
| 435 | + | }, |
| 436 | + | { |
| 437 | + | desc: "SetAreaCells sets full-width runes that fit", |
| 438 | + | canvas: image.Rect(0, 0, 3, 3), |
| 439 | + | ops: func(cvs *Canvas) error { |
| 440 | + | return cvs.SetAreaCells(image.Rect(0, 0, 2, 2), '世') |
| 441 | + | }, |
| 442 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 443 | + | ft := faketerm.MustNew(size) |
| 444 | + | cvs, err := New(ft.Area()) |
| 445 | + | if err != nil { |
| 446 | + | return nil, err |
| 447 | + | } |
| 448 | + | |
| 449 | + | for _, p := range []image.Point{ |
| 450 | + | {0, 0}, |
| 451 | + | {0, 1}, |
| 452 | + | } { |
| 453 | + | if _, err := cvs.SetCell(p, '世'); err != nil { |
| 454 | + | return nil, err |
| 455 | + | } |
| 456 | + | } |
| 457 | + | |
| 458 | + | if err := cvs.Apply(ft); err != nil { |
| 459 | + | return nil, err |
| 460 | + | } |
| 461 | + | return ft, nil |
| 462 | + | }, |
| 463 | + | }, |
| 464 | + | { |
| 465 | + | desc: "SetAreaCells sets full-width runes that will leave a gap at the end of each row", |
| 466 | + | canvas: image.Rect(0, 0, 3, 3), |
| 467 | + | ops: func(cvs *Canvas) error { |
| 468 | + | return cvs.SetAreaCells(image.Rect(0, 0, 3, 3), '世') |
| 469 | + | }, |
| 470 | + | want: func(size image.Point) (*faketerm.Terminal, error) { |
| 471 | + | ft := faketerm.MustNew(size) |
| 472 | + | cvs, err := New(ft.Area()) |
| 473 | + | if err != nil { |
| 474 | + | return nil, err |
| 475 | + | } |
| 476 | + | |
| 477 | + | for _, p := range []image.Point{ |
| 478 | + | {0, 0}, |
| 479 | + | {0, 1}, |
| 480 | + | {0, 2}, |
| 481 | + | } { |
| 482 | + | if _, err := cvs.SetCell(p, '世'); err != nil { |
| 483 | + | return nil, err |
| 484 | + | } |
| 485 | + | } |
| 486 | + | |
| 487 | + | if err := cvs.Apply(ft); err != nil { |
| 488 | + | return nil, err |
| 489 | + | } |
| 490 | + | return ft, nil |
| 491 | + | }, |
| 492 | + | }, |
| 493 | + | } |
| 494 | + | |
| 495 | + | for _, tc := range tests { |
| 496 | + | t.Run(tc.desc, func(t *testing.T) { |
| 497 | + | cvs, err := New(tc.canvas) |
| 498 | + | if err != nil { |
| 499 | + | t.Fatalf("New => unexpected error: %v", err) |
| 500 | + | } |
| 501 | + | |
| 502 | + | if tc.ops != nil { |
| 503 | + | err := tc.ops(cvs) |
| 504 | + | if (err != nil) != tc.wantErr { |
| 505 | + | t.Errorf("tc.ops => unexpected error: %v, wantErr: %v", err, tc.wantErr) |
| 506 | + | } |
| 507 | + | if err != nil { |
| 508 | + | return |
| 509 | + | } |
| 510 | + | } |
| 511 | + | |
| 512 | + | size := cvs.Size() |
| 513 | + | got, err := faketerm.New(size) |
| 514 | + | if err != nil { |
| 515 | + | t.Fatalf("faketerm.New => unexpected error: %v", err) |
| 516 | + | } |
| 517 | + | if err := cvs.Apply(got); err != nil { |
| 518 | + | t.Fatalf("cvs.Apply => %v", err) |
| 519 | + | } |
| 520 | + | |
| 521 | + | var want *faketerm.Terminal |
| 522 | + | if tc.want != nil { |
| 523 | + | want, err = tc.want(size) |
| 524 | + | if err != nil { |
| 525 | + | t.Fatalf("tc.want => unexpected error: %v", err) |
| 526 | + | } |
| 527 | + | } else { |
| 528 | + | w, err := faketerm.New(size) |
| 529 | + | if err != nil { |
| 530 | + | t.Fatalf("faketerm.New => unexpected error: %v", err) |
| 531 | + | } |
| 532 | + | want = w |
| 533 | + | } |
| 534 | + | |
| 535 | + | if diff := faketerm.Diff(want, got); diff != "" { |
| 536 | + | t.Errorf("cvs.SetCellOpts => %v", diff) |
| 537 | + | } |
| 538 | + | }) |
| 539 | + | } |
| 540 | + | } |
| 541 | + | |
| 542 | + | func TestSetCellAndApply(t *testing.T) { |
| 543 | + | tests := []struct { |
| 544 | + | desc string |
| 545 | + | termSize image.Point |
| 546 | + | canvasArea image.Rectangle |
| 547 | + | point image.Point |
| 548 | + | r rune |
| 549 | + | opts []cell.Option |
| 550 | + | want cell.Buffer // Expected back buffer in the fake terminal. |
| 551 | + | wantCells int |
| 552 | + | wantSetCellErr bool |
| 553 | + | wantApplyErr bool |
| 554 | + | }{ |
| 555 | + | { |
| 556 | + | desc: "setting cell outside the designated area", |
| 557 | + | termSize: image.Point{2, 2}, |
| 558 | + | canvasArea: image.Rect(0, 0, 1, 1), |
| 559 | + | point: image.Point{0, 2}, |
| 560 | + | wantSetCellErr: true, |
| 561 | + | }, |
| 562 | + | { |
| 563 | + | desc: "sets a top-left corner cell", |
| 564 | + | termSize: image.Point{3, 3}, |
| 565 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 566 | + | point: image.Point{0, 0}, |
| 567 | + | r: 'X', |
| 568 | + | wantCells: 1, |
| 569 | + | want: cell.Buffer{ |
| 570 | + | { |
| 571 | + | cell.New(0), |
| 572 | + | cell.New(0), |
| 573 | + | cell.New(0), |
| 574 | + | }, |
| 575 | + | { |
| 576 | + | cell.New(0), |
| 577 | + | cell.New('X'), |
| 578 | + | cell.New(0), |
| 579 | + | }, |
| 580 | + | { |
| 581 | + | cell.New(0), |
| 582 | + | cell.New(0), |
| 583 | + | cell.New(0), |
| 584 | + | }, |
| 585 | + | }, |
| 586 | + | }, |
| 587 | + | { |
| 588 | + | desc: "sets a full-width rune in the top-left corner cell", |
| 589 | + | termSize: image.Point{3, 3}, |
| 590 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 591 | + | point: image.Point{0, 0}, |
| 592 | + | r: '界', |
| 593 | + | wantCells: 2, |
| 594 | + | want: cell.Buffer{ |
| 595 | + | { |
| 596 | + | cell.New(0), |
| 597 | + | cell.New(0), |
| 598 | + | cell.New(0), |
| 599 | + | }, |
| 600 | + | { |
| 601 | + | cell.New(0), |
| 602 | + | cell.New('界'), |
| 603 | + | cell.New(0), |
| 604 | + | }, |
| 605 | + | { |
| 606 | + | cell.New(0), |
| 607 | + | cell.New(0), |
| 608 | + | cell.New(0), |
| 609 | + | }, |
| 610 | + | }, |
| 611 | + | }, |
| 612 | + | { |
| 613 | + | desc: "not enough space for a full-width rune", |
| 614 | + | termSize: image.Point{3, 3}, |
| 615 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 616 | + | point: image.Point{1, 0}, |
| 617 | + | r: '界', |
| 618 | + | wantSetCellErr: true, |
| 619 | + | }, |
| 620 | + | { |
| 621 | + | desc: "sets a top-right corner cell", |
| 622 | + | termSize: image.Point{3, 3}, |
| 623 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 624 | + | point: image.Point{1, 0}, |
| 625 | + | r: 'X', |
| 626 | + | wantCells: 1, |
| 627 | + | want: cell.Buffer{ |
| 628 | + | { |
| 629 | + | cell.New(0), |
| 630 | + | cell.New(0), |
| 631 | + | cell.New(0), |
| 632 | + | }, |
| 633 | + | { |
| 634 | + | cell.New(0), |
| 635 | + | cell.New(0), |
| 636 | + | cell.New(0), |
| 637 | + | }, |
| 638 | + | { |
| 639 | + | cell.New(0), |
| 640 | + | cell.New('X'), |
| 641 | + | cell.New(0), |
| 642 | + | }, |
| 643 | + | }, |
| 644 | + | }, |
| 645 | + | { |
| 646 | + | desc: "sets a bottom-left corner cell", |
| 647 | + | termSize: image.Point{3, 3}, |
| 648 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 649 | + | point: image.Point{0, 1}, |
| 650 | + | r: 'X', |
| 651 | + | wantCells: 1, |
| 652 | + | want: cell.Buffer{ |
| 653 | + | { |
| 654 | + | cell.New(0), |
| 655 | + | cell.New(0), |
| 656 | + | cell.New(0), |
| 657 | + | }, |
| 658 | + | { |
| 659 | + | cell.New(0), |
| 660 | + | cell.New(0), |
| 661 | + | cell.New('X'), |
| 662 | + | }, |
| 663 | + | { |
| 664 | + | cell.New(0), |
| 665 | + | cell.New(0), |
| 666 | + | cell.New(0), |
| 667 | + | }, |
| 668 | + | }, |
| 669 | + | }, |
| 670 | + | { |
| 671 | + | desc: "sets a bottom-right corner cell", |
| 672 | + | termSize: image.Point{3, 3}, |
| 673 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 674 | + | point: image.Point{1, 1}, |
| 675 | + | r: 'Z', |
| 676 | + | wantCells: 1, |
| 677 | + | want: cell.Buffer{ |
| 678 | + | { |
| 679 | + | cell.New(0), |
| 680 | + | cell.New(0), |
| 681 | + | cell.New(0), |
| 682 | + | }, |
| 683 | + | { |
| 684 | + | cell.New(0), |
| 685 | + | cell.New(0), |
| 686 | + | cell.New(0), |
| 687 | + | }, |
| 688 | + | { |
| 689 | + | cell.New(0), |
| 690 | + | cell.New(0), |
| 691 | + | cell.New('Z'), |
| 692 | + | }, |
| 693 | + | }, |
| 694 | + | }, |
| 695 | + | { |
| 696 | + | desc: "sets cell options", |
| 697 | + | termSize: image.Point{3, 3}, |
| 698 | + | canvasArea: image.Rect(1, 1, 3, 3), |
| 699 | + | point: image.Point{1, 1}, |
| 700 | + | r: 'A', |
| 701 | + | opts: []cell.Option{ |
| 702 | + | cell.BgColor(cell.ColorRed), |
| 703 | + | }, |
| 704 | + | wantCells: 1, |
| 705 | + | want: cell.Buffer{ |
| 706 | + | { |
| 707 | + | cell.New(0), |
| 708 | + | cell.New(0), |
| 709 | + | cell.New(0), |
| 710 | + | }, |
| 711 | + | { |
| 712 | + | cell.New(0), |
| 713 | + | cell.New(0), |
| 714 | + | cell.New(0), |
| 715 | + | }, |
| 716 | + | { |
| 717 | + | cell.New(0), |
| 718 | + | cell.New(0), |
| 719 | + | cell.New('A', cell.BgColor(cell.ColorRed)), |
| 720 | + | }, |
| 721 | + | }, |
| 722 | + | }, |
| 723 | + | { |
| 724 | + | desc: "canvas size equals terminal size", |
| 725 | + | termSize: image.Point{1, 1}, |
| 726 | + | canvasArea: image.Rect(0, 0, 1, 1), |
| 727 | + | point: image.Point{0, 0}, |
| 728 | + | r: 'A', |
| 729 | + | wantCells: 1, |
| 730 | + | want: cell.Buffer{ |
| 731 | + | { |
| 732 | + | cell.New('A'), |
| 733 | + | }, |
| 734 | + | }, |
| 735 | + | }, |
| 736 | + | { |
| 737 | + | desc: "terminal too small for the area", |
| 738 | + | termSize: image.Point{1, 1}, |
| 739 | + | canvasArea: image.Rect(0, 0, 2, 2), |
| 740 | + | point: image.Point{0, 0}, |
| 741 | + | r: 'A', |
| 742 | + | wantCells: 1, |
| 743 | + | wantApplyErr: true, |
| 744 | + | }, |
| 745 | + | } |
| 746 | + | |
| 747 | + | for _, tc := range tests { |
| 748 | + | t.Run(tc.desc, func(t *testing.T) { |
| 749 | + | c, err := New(tc.canvasArea) |
| 750 | + | if err != nil { |
| 751 | + | t.Fatalf("New => unexpected error: %v", err) |
| 752 | + | } |
| 753 | + | |
| 754 | + | gotCells, err := c.SetCell(tc.point, tc.r, tc.opts...) |
| 755 | + | if (err != nil) != tc.wantSetCellErr { |
| 756 | + | t.Errorf("SetCell => unexpected error: %v, wantSetCellErr: %v", err, tc.wantSetCellErr) |
| 757 | + | } |
| 758 | + | if err != nil { |
| 759 | + | return |
| 760 | + | } |
| 761 | + | |
| 762 | + | if gotCells != tc.wantCells { |
| 763 | + | t.Errorf("SetCell => unexpected number of cells %d, want %d", gotCells, tc.wantCells) |
| 764 | + | } |
| 765 | + | |
| 766 | + | ft, err := faketerm.New(tc.termSize) |
| 767 | + | if err != nil { |
| 768 | + | t.Fatalf("faketerm.New => unexpected error: %v", err) |
| 769 | + | } |
| 770 | + | err = c.Apply(ft) |
| 771 | + | if (err != nil) != tc.wantApplyErr { |
| 772 | + | t.Errorf("Apply => unexpected error: %v, wantApplyErr: %v", err, tc.wantApplyErr) |
| 773 | + | } |
| 774 | + | if err != nil { |
| 775 | + | return |
| 776 | + | } |
| 777 | + | |
| 778 | + | got := ft.BackBuffer() |
| 779 | + | if diff := pretty.Compare(tc.want, got); diff != "" { |
| 780 | + | t.Errorf("faketerm.BackBuffer => unexpected diff (-want, +got):\n%s", diff) |
| 781 | + | } |
| 782 | + | }) |
| 783 | + | } |
| 784 | + | } |
| 785 | + | |
| 786 | + | func TestClear(t *testing.T) { |
| 787 | + | c, err := New(image.Rect(1, 1, 3, 3)) |
| 788 | + | if err != nil { |
| 789 | + | t.Fatalf("New => unexpected error: %v", err) |
| 790 | + | } |
| 791 | + | |
| 792 | + | if _, err := c.SetCell(image.Point{0, 0}, 'X'); err != nil { |
| 793 | + | t.Fatalf("SetCell => unexpected error: %v", err) |
| 794 | + | } |
| 795 | + | |
| 796 | + | ft, err := faketerm.New(image.Point{3, 3}) |
| 797 | + | if err != nil { |
| 798 | + | t.Fatalf("faketerm.New => unexpected error: %v", err) |
| 799 | + | } |
| 800 | + | // Set one cell outside of the canvas on the terminal. |
| 801 | + | if err := ft.SetCell(image.Point{0, 0}, 'A'); err != nil { |
| 802 | + | t.Fatalf("faketerm.SetCell => unexpected error: %v", err) |
| 803 | + | } |
| 804 | + | |
| 805 | + | if err := c.Apply(ft); err != nil { |
| 806 | + | t.Fatalf("Apply => unexpected error: %v", err) |
| 807 | + | } |
| 808 | + | |
| 809 | + | want := cell.Buffer{ |
| 810 | + | { |
| 811 | + | cell.New('A'), |
| 812 | + | cell.New(0), |
| 813 | + | cell.New(0), |
| 814 | + | }, |
| 815 | + | { |
| 816 | + | cell.New(0), |
| 817 | + | cell.New('X'), |
| 818 | + | cell.New(0), |
| 819 | + | }, |
| 820 | + | { |
| 821 | + | cell.New(0), |
| 822 | + | cell.New(0), |
| 823 | + | cell.New(0), |
| 824 | + | }, |
| 825 | + | } |
| 826 | + | got := ft.BackBuffer() |
| 827 | + | if diff := pretty.Compare(want, got); diff != "" { |
| 828 | + | t.Errorf("faketerm.BackBuffer before Clear => unexpected diff (-want, +got):\n%s", diff) |
| 829 | + | } |
| 830 | + | |
| 831 | + | // Call Clear(), Apply() and verify that only the area belonging to the |
| 832 | + | // canvas was cleared. |
| 833 | + | if err := c.Clear(); err != nil { |
| 834 | + | t.Fatalf("Clear => unexpected error: %v", err) |
| 835 | + | } |
| 836 | + | if err := c.Apply(ft); err != nil { |
| 837 | + | t.Fatalf("Apply => unexpected error: %v", err) |
| 838 | + | } |
| 839 | + | |
| 840 | + | want = cell.Buffer{ |
| 841 | + | { |
| 842 | + | cell.New('A'), |
| 843 | + | cell.New(0), |
| 844 | + | cell.New(0), |
| 845 | + | }, |
| 846 | + | { |
| 847 | + | cell.New(0), |
| 848 | + | cell.New(0), |
| 849 | + | cell.New(0), |
| 850 | + | }, |
| 851 | + | { |
| 852 | + | cell.New(0), |
| 853 | + | cell.New(0), |
| 854 | + | cell.New(0), |
| 855 | + | }, |
| 856 | + | } |
| 857 | + | |
| 858 | + | got = ft.BackBuffer() |
| 859 | + | if diff := pretty.Compare(want, got); diff != "" { |
| 860 | + | t.Errorf("faketerm.BackBuffer after Clear => unexpected diff (-want, +got):\n%s", diff) |
| 861 | + | } |
| 862 | + | } |
| 863 | + | |
| 864 | + | // TestApplyFullWidthRunes verifies that when applying a full-width rune to the |
| 865 | + | // terminal, canvas doesn't touch the neighbor cell that holds the remaining |
| 866 | + | // part of the full-width rune. |
| 867 | + | func TestApplyFullWidthRunes(t *testing.T) { |
| 868 | + | ar := image.Rect(0, 0, 3, 3) |
| 869 | + | c, err := New(ar) |
| 870 | + | if err != nil { |
| 871 | + | t.Fatalf("New => unexpected error: %v", err) |
| 872 | + | } |
| 873 | + | |
| 874 | + | fullP := image.Point{0, 0} |
| 875 | + | if _, err := c.SetCell(fullP, '界'); err != nil { |
| 876 | + | t.Fatalf("SetCell => unexpected error: %v", err) |
| 877 | + | } |
| 878 | + | |
| 879 | + | ft, err := faketerm.New(area.Size(ar)) |
| 880 | + | if err != nil { |
| 881 | + | t.Fatalf("faketerm.New => unexpected error: %v", err) |
| 882 | + | } |
| 883 | + | partP := image.Point{1, 0} |
| 884 | + | if err := ft.SetCell(partP, 'A'); err != nil { |
| 885 | + | t.Fatalf("faketerm.SetCell => unexpected error: %v", err) |
| 886 | + | } |
| 887 | + | |
| 888 | + | if err := c.Apply(ft); err != nil { |
| 889 | + | t.Fatalf("Apply => unexpected error: %v", err) |
| 890 | + | } |
| 891 | + | |
| 892 | + | want, err := cell.NewBuffer(area.Size(ar)) |
| 893 | + | if err != nil { |
| 894 | + | t.Fatalf("NewBuffer => unexpected error: %v", err) |
| 895 | + | } |
| 896 | + | want[fullP.X][fullP.Y].Rune = '界' |
| 897 | + | want[partP.X][partP.Y].Rune = 'A' |
| 898 | + | |
| 899 | + | got := ft.BackBuffer() |
| 900 | + | if diff := pretty.Compare(want, got); diff != "" { |
| 901 | + | t.Errorf("faketerm.BackBuffer => unexpected diff (-want, +got):\n%s", diff) |
| 902 | + | } |
| 903 | + | } |
| 904 | + | |
| 905 | + | func TestCell(t *testing.T) { |
| 906 | + | tests := []struct { |
| 907 | + | desc string |
| 908 | + | cvs func() (*Canvas, error) |
| 909 | + | point image.Point |
| 910 | + | want *cell.Cell |
| 911 | + | wantErr bool |
| 912 | + | }{ |
| 913 | + | { |
| 914 | + | desc: "requested point falls outside of the canvas", |
| 915 | + | cvs: func() (*Canvas, error) { |
| 916 | + | cvs, err := New(image.Rect(0, 0, 1, 1)) |
| 917 | + | if err != nil { |
| 918 | + | return nil, err |
| 919 | + | } |
| 920 | + | return cvs, nil |
| 921 | + | }, |
| 922 | + | point: image.Point{1, 1}, |
| 923 | + | wantErr: true, |
| 924 | + | }, |
| 925 | + | { |
| 926 | + | desc: "returns the cell", |
| 927 | + | cvs: func() (*Canvas, error) { |
| 928 | + | cvs, err := New(image.Rect(0, 0, 2, 2)) |
| 929 | + | if err != nil { |
| 930 | + | return nil, err |
| 931 | + | } |
| 932 | + | if _, err := cvs.SetCell( |
| 933 | + | image.Point{1, 1}, 'A', |
| 934 | + | cell.FgColor(cell.ColorRed), |
| 935 | + | cell.BgColor(cell.ColorBlue), |
| 936 | + | ); err != nil { |
| 937 | + | return nil, err |
| 938 | + | } |
| 939 | + | return cvs, nil |
| 940 | + | }, |
| 941 | + | point: image.Point{1, 1}, |
| 942 | + | want: &cell.Cell{ |
| 943 | + | Rune: 'A', |
| 944 | + | Opts: cell.NewOptions( |
| 945 | + | cell.FgColor(cell.ColorRed), |
| 946 | + | cell.BgColor(cell.ColorBlue), |
| 947 | + | ), |
| 948 | + | }, |
| 949 | + | }, |
| 950 | + | } |
| 951 | + | |
| 952 | + | for _, tc := range tests { |
| 953 | + | t.Run(tc.desc, func(t *testing.T) { |
| 954 | + | cvs, err := tc.cvs() |
| 955 | + | if err != nil { |
| 956 | + | t.Fatalf("tc.cvs => unexpected error: %v", err) |
| 957 | + | } |
| 958 | + | |
| 959 | + | got, err := cvs.Cell(tc.point) |
| 960 | + | if (err != nil) != tc.wantErr { |
| 961 | + | t.Errorf("Cell => unexpected error: %v, wantErr: %v", err, tc.wantErr) |
| 962 | + | } |
| 963 | + | if err != nil { |
| 964 | + | return |
| 965 | + | } |
| 966 | + | |
| 967 | + | if diff := pretty.Compare(tc.want, got); diff != "" { |
| 968 | + | t.Errorf("Cell => unexpected diff (-want, +got):\n%s", diff) |
| 969 | + | } |
| 970 | + | }) |
| 971 | + | } |
| 972 | + | } |
| 973 | + | |
| 974 | + | // mustNew creates a new Canvas or panics. |
| 975 | + | func mustNew(ar image.Rectangle) *Canvas { |
| 976 | + | c, err := New(ar) |
| 977 | + | if err != nil { |
| 978 | + | panic(err) |
| 979 | + | } |
| 980 | + | return c |
| 981 | + | } |
| 982 | + | |
| 983 | + | // mustFill fills the canvas with the specified runes or panics. |
| 984 | + | func mustFill(c *Canvas, r rune) { |
| 985 | + | ar := c.Area() |
| 986 | + | for col := 0; col < ar.Max.X; col++ { |
| 987 | + | for row := 0; row < ar.Max.Y; row++ { |
| 988 | + | if _, err := c.SetCell(image.Point{col, row}, r); err != nil { |
| 989 | + | panic(err) |
| 990 | + | } |
| 991 | + | } |
| 992 | + | } |
| 993 | + | } |
| 994 | + | |
| 995 | + | // mustSetCell sets cell at the specified point of the canvas or panics. |
| 996 | + | func mustSetCell(c *Canvas, p image.Point, r rune, opts ...cell.Option) { |
| 997 | + | if _, err := c.SetCell(p, r, opts...); err != nil { |
| 998 | + | panic(err) |
| 999 | + | } |
| 1000 | + | } |
| 1001 | + | |
| 1002 | + | func TestCopyTo(t *testing.T) { |
| 1003 | + | tests := []struct { |
| 1004 | + | desc string |
| 1005 | + | src *Canvas |
| 1006 | + | dst *Canvas |
| 1007 | + | want *Canvas |
| 1008 | + | wantErr bool |
| 1009 | + | }{ |
| 1010 | + | { |
| 1011 | + | desc: "fails when the canvas doesn't fit", |
| 1012 | + | src: func() *Canvas { |
| 1013 | + | c := mustNew(image.Rect(0, 0, 3, 3)) |
| 1014 | + | mustFill(c, 'X') |
| 1015 | + | return c |
| 1016 | + | }(), |
| 1017 | + | dst: mustNew(image.Rect(0, 0, 2, 2)), |
| 1018 | + | want: mustNew(image.Rect(0, 0, 3, 3)), |
| 1019 | + | wantErr: true, |
| 1020 | + | }, |
| 1021 | + | { |
| 1022 | + | desc: "fails when the area lies outside of the destination canvas", |
| 1023 | + | src: func() *Canvas { |
| 1024 | + | c := mustNew(image.Rect(3, 3, 4, 4)) |
| 1025 | + | mustFill(c, 'X') |
| 1026 | + | return c |
| 1027 | + | }(), |
| 1028 | + | dst: mustNew(image.Rect(0, 0, 3, 3)), |
| 1029 | + | want: mustNew(image.Rect(0, 0, 3, 3)), |
| 1030 | + | wantErr: true, |
| 1031 | + | }, |
| 1032 | + | { |
| 1033 | + | desc: "copies zero based same size canvases", |
| 1034 | + | src: func() *Canvas { |
| 1035 | + | c := mustNew(image.Rect(0, 0, 3, 3)) |
| 1036 | + | mustFill(c, 'X') |
| 1037 | + | return c |
| 1038 | + | }(), |
| 1039 | + | dst: mustNew(image.Rect(0, 0, 3, 3)), |
| 1040 | + | want: func() *Canvas { |
| 1041 | + | c := mustNew(image.Rect(0, 0, 3, 3)) |
| 1042 | + | mustSetCell(c, image.Point{0, 0}, 'X') |
| 1043 | + | mustSetCell(c, image.Point{1, 0}, 'X') |
| 1044 | + | mustSetCell(c, image.Point{2, 0}, 'X') |
| 1045 | + | |
| 1046 | + | mustSetCell(c, image.Point{0, 1}, 'X') |
| 1047 | + | mustSetCell(c, image.Point{1, 1}, 'X') |
| 1048 | + | mustSetCell(c, image.Point{2, 1}, 'X') |
| 1049 | + | |
| 1050 | + | mustSetCell(c, image.Point{0, 2}, 'X') |
| 1051 | + | mustSetCell(c, image.Point{1, 2}, 'X') |
| 1052 | + | mustSetCell(c, image.Point{2, 2}, 'X') |
| 1053 | + | return c |
| 1054 | + | }(), |
| 1055 | + | }, |
| 1056 | + | { |
| 1057 | + | desc: "copies smaller canvas with an offset", |
| 1058 | + | src: func() *Canvas { |
| 1059 | + | c := mustNew(image.Rect(1, 1, 2, 2)) |
| 1060 | + | mustFill(c, 'X') |
| 1061 | + | return c |
| 1062 | + | }(), |
| 1063 | + | dst: mustNew(image.Rect(0, 0, 3, 3)), |
| 1064 | + | want: func() *Canvas { |
| 1065 | + | c := mustNew(image.Rect(0, 0, 3, 3)) |
| 1066 | + | mustSetCell(c, image.Point{1, 1}, 'X') |
| 1067 | + | return c |
| 1068 | + | }(), |
| 1069 | + | }, |
| 1070 | + | { |
| 1071 | + | desc: "copies smaller canvas with an offset into a canvas with offset from terminal", |
| 1072 | + | src: func() *Canvas { |
| 1073 | + | c := mustNew(image.Rect(1, 1, 2, 2)) |
| 1074 | + | mustFill(c, 'X') |
| 1075 | + | return c |
| 1076 | + | }(), |
| 1077 | + | dst: mustNew(image.Rect(3, 3, 6, 6)), |
| 1078 | + | want: func() *Canvas { |
| 1079 | + | c := mustNew(image.Rect(3, 3, 6, 6)) |
| 1080 | + | mustSetCell(c, image.Point{1, 1}, 'X') |
| 1081 | + | return c |
| 1082 | + | }(), |
| 1083 | + | }, |
| 1084 | + | { |
| 1085 | + | desc: "copies cell options", |
| 1086 | + | src: func() *Canvas { |
| 1087 | + | c := mustNew(image.Rect(0, 0, 1, 1)) |
| 1088 | + | mustSetCell(c, image.Point{0, 0}, 'X', |
| 1089 | + | cell.FgColor(cell.ColorRed), |
| 1090 | + | cell.BgColor(cell.ColorBlue), |
| 1091 | + | ) |
| 1092 | + | return c |
| 1093 | + | }(), |
| 1094 | + | dst: mustNew(image.Rect(0, 0, 3, 1)), |
| 1095 | + | want: func() *Canvas { |
| 1096 | + | c := mustNew(image.Rect(0, 0, 3, 1)) |
| 1097 | + | mustSetCell(c, image.Point{0, 0}, 'X', |
| 1098 | + | cell.FgColor(cell.ColorRed), |
| 1099 | + | cell.BgColor(cell.ColorBlue), |
| 1100 | + | ) |
| 1101 | + | return c |
| 1102 | + | }(), |
| 1103 | + | }, |
| 1104 | + | { |
| 1105 | + | desc: "copies cells with full-width runes", |
| 1106 | + | src: func() *Canvas { |
| 1107 | + | c := mustNew(image.Rect(0, 0, 3, 3)) |
| 1108 | + | mustSetCell(c, image.Point{0, 0}, '界') |
| 1109 | + | mustSetCell(c, image.Point{1, 1}, '界') |
| 1110 | + | return c |
| 1111 | + | }(), |
| 1112 | + | dst: mustNew(image.Rect(0, 0, 3, 3)), |
| 1113 | + | want: func() *Canvas { |
| 1114 | + | c := mustNew(image.Rect(0, 0, 3, 3)) |
| 1115 | + | mustSetCell(c, image.Point{0, 0}, '界') |
| 1116 | + | mustSetCell(c, image.Point{1, 1}, '界') |
| 1117 | + | return c |
| 1118 | + | }(), |
| 1119 | + | }, |
| 1120 | + | } |
| 1121 | + | |
| 1122 | + | for _, tc := range tests { |
| 1123 | + | t.Run(tc.desc, func(t *testing.T) { |
| 1124 | + | err := tc.src.CopyTo(tc.dst) |
| 1125 | + | if (err != nil) != tc.wantErr { |
| 1126 | + | t.Errorf("CopyTo => unexpected error: %v, wantErr: %v", err, tc.wantErr) |
| 1127 | + | } |
| 1128 | + | if err != nil { |
| 1129 | + | return |
| 1130 | + | } |
| 1131 | + | |
| 1132 | + | ftSize := image.Point{10, 10} |
| 1133 | + | got, err := faketerm.New(ftSize) |
| 1134 | + | if err != nil { |
| 1135 | + | t.Fatalf("faketerm.New(tc.dst.Size()) => unexpected error: %v", err) |
| 1136 | + | } |
| 1137 | + | if err := tc.dst.Apply(got); err != nil { |
| 1138 | + | t.Fatalf("tc.dst.Apply => unexpected error: %v", err) |
| 1139 | + | } |
| 1140 | + | |
| 1141 | + | want, err := faketerm.New(ftSize) |
| 1142 | + | if err != nil { |
| 1143 | + | t.Fatalf("faketerm.New(tc.want.Size()) => unexpected error: %v", err) |
| 1144 | + | } |
| 1145 | + | |
| 1146 | + | if err := tc.want.Apply(want); err != nil { |
| 1147 | + | t.Fatalf("tc.want.Apply => unexpected error: %v", err) |
| 1148 | + | } |
| 1149 | + | |
| 1150 | + | if diff := faketerm.Diff(want, got); diff != "" { |
| 1151 | + | t.Errorf("CopyTo => %v", diff) |
| 1152 | + | } |
| 1153 | + | }) |
| 1154 | + | } |
| 1155 | + | } |
| 1156 | + | |