Debian Codegraph @

screen/screen_test.go

raw symbols SCIP
1 package screen
2
3 import (
4 "fmt"
5 "testing"
6
7 uv "github.com/charmbracelet/ultraviolet"
8 "github.com/charmbracelet/x/ansi"
9 )
10
11 // mockScreen is a basic screen implementation for testing
12 type mockScreen struct {
13 *uv.Buffer
14 method ansi.Method
15 }
16
17 func newMockScreen(width, height int) *mockScreen {
18 return &mockScreen{
19 Buffer: uv.NewBuffer(width, height),
20 method: ansi.WcWidth,
21 }
22 }
23
24 func (m *mockScreen) Bounds() uv.Rectangle {
25 return m.Buffer.Bounds()
26 }
27
28 func (m *mockScreen) CellAt(x, y int) *uv.Cell {
29 return m.Buffer.CellAt(x, y)
30 }
31
32 func (m *mockScreen) SetCell(x, y int, c *uv.Cell) {
33 m.Buffer.SetCell(x, y, c)
34 }
35
36 func (m *mockScreen) WidthMethod() uv.WidthMethod {
37 return m.method
38 }
39
40 // minimalMockScreen is a minimal screen that only implements the required Screen interface
41 // and doesn't have any of the optional methods (Clear, Fill, Clone, etc.)
42 type minimalMockScreen struct {
43 cells [][]uv.Cell
44 width int
45 height int
46 method ansi.Method
47 }
48
49 func newMinimalMockScreen(width, height int) *minimalMockScreen {
50 cells := make([][]uv.Cell, height)
51 for i := range cells {
52 cells[i] = make([]uv.Cell, width)
53 for j := range cells[i] {
54 cells[i][j] = uv.EmptyCell
55 }
56 }
57 return &minimalMockScreen{
58 cells: cells,
59 width: width,
60 height: height,
61 method: ansi.WcWidth,
62 }
63 }
64
65 func (m *minimalMockScreen) Bounds() uv.Rectangle {
66 return uv.Rect(0, 0, m.width, m.height)
67 }
68
69 func (m *minimalMockScreen) CellAt(x, y int) *uv.Cell {
70 if x < 0 || x >= m.width || y < 0 || y >= m.height {
71 return nil
72 }
73 return &m.cells[y][x]
74 }
75
76 func (m *minimalMockScreen) SetCell(x, y int, c *uv.Cell) {
77 if x < 0 || x >= m.width || y < 0 || y >= m.height {
78 return
79 }
80 if c == nil {
81 m.cells[y][x] = uv.EmptyCell
82 } else {
83 m.cells[y][x] = *c
84 }
85 }
86
87 func (m *minimalMockScreen) WidthMethod() uv.WidthMethod {
88 return m.method
89 }
90
91 // nilCellMockScreen is a screen that can return nil for certain cells
92 type nilCellMockScreen struct {
93 cells map[string]*uv.Cell
94 width int
95 height int
96 method ansi.Method
97 nilPositions map[string]bool
98 }
99
100 func newNilCellMockScreen(width, height int) *nilCellMockScreen {
101 return &nilCellMockScreen{
102 cells: make(map[string]*uv.Cell),
103 width: width,
104 height: height,
105 method: ansi.WcWidth,
106 nilPositions: make(map[string]bool),
107 }
108 }
109
110 func (m *nilCellMockScreen) Bounds() uv.Rectangle {
111 return uv.Rect(0, 0, m.width, m.height)
112 }
113
114 func (m *nilCellMockScreen) CellAt(x, y int) *uv.Cell {
115 if x < 0 || x >= m.width || y < 0 || y >= m.height {
116 return nil
117 }
118 key := fmt.Sprintf("%d,%d", x, y)
119 if m.nilPositions[key] {
120 return nil
121 }
122 if cell, ok := m.cells[key]; ok {
123 return cell
124 }
125 // Return empty cell by default
126 empty := uv.EmptyCell
127 return &empty
128 }
129
130 func (m *nilCellMockScreen) SetCell(x, y int, c *uv.Cell) {
131 if x < 0 || x >= m.width || y < 0 || y >= m.height {
132 return
133 }
134 key := fmt.Sprintf("%d,%d", x, y)
135 if c == nil {
136 m.cells[key] = &uv.EmptyCell
137 } else {
138 cellCopy := *c
139 m.cells[key] = &cellCopy
140 }
141 }
142
143 func (m *nilCellMockScreen) WidthMethod() uv.WidthMethod {
144 return m.method
145 }
146
147 func (m *nilCellMockScreen) SetNilAt(x, y int) {
148 key := fmt.Sprintf("%d,%d", x, y)
149 m.nilPositions[key] = true
150 }
151
152 // mockScreenWithClear implements Clear method
153 type mockScreenWithClear struct {
154 *mockScreen
155 clearCalled bool
156 }
157
158 func (m *mockScreenWithClear) Clear() {
159 m.clearCalled = true
160 m.Buffer.Clear()
161 }
162
163 // mockScreenWithClearArea implements ClearArea method
164 type mockScreenWithClearArea struct {
165 *mockScreen
166 clearAreaCalled bool
167 lastArea uv.Rectangle
168 }
169
170 func (m *mockScreenWithClearArea) ClearArea(area uv.Rectangle) {
171 m.clearAreaCalled = true
172 m.lastArea = area
173 m.Buffer.ClearArea(area)
174 }
175
176 // mockScreenWithFill implements Fill method
177 type mockScreenWithFill struct {
178 *mockScreen
179 fillCalled bool
180 lastCell *uv.Cell
181 }
182
183 func (m *mockScreenWithFill) Fill(cell *uv.Cell) {
184 m.fillCalled = true
185 m.lastCell = cell
186 m.Buffer.Fill(cell)
187 }
188
189 // mockScreenWithFillArea implements FillArea method
190 type mockScreenWithFillArea struct {
191 *mockScreen
192 fillAreaCalled bool
193 lastCell *uv.Cell
194 lastArea uv.Rectangle
195 }
196
197 func (m *mockScreenWithFillArea) FillArea(cell *uv.Cell, area uv.Rectangle) {
198 m.fillAreaCalled = true
199 m.lastCell = cell
200 m.lastArea = area
201 m.Buffer.FillArea(cell, area)
202 }
203
204 // mockScreenWithClone implements Clone method
205 type mockScreenWithClone struct {
206 *mockScreen
207 cloneCalled bool
208 }
209
210 func (m *mockScreenWithClone) Clone() *uv.Buffer {
211 m.cloneCalled = true
212 return m.Buffer.Clone()
213 }
214
215 // mockScreenWithCloneArea implements CloneArea method
216 type mockScreenWithCloneArea struct {
217 *mockScreen
218 cloneAreaCalled bool
219 lastArea uv.Rectangle
220 }
221
222 func (m *mockScreenWithCloneArea) CloneArea(area uv.Rectangle) *uv.Buffer {
223 m.cloneAreaCalled = true
224 m.lastArea = area
225 return m.Buffer.CloneArea(area)
226 }
227
228 func TestClear(t *testing.T) {
229 t.Run("with Clear method", func(t *testing.T) {
230 scr := &mockScreenWithClear{
231 mockScreen: newMockScreen(10, 5),
232 }
233
234 // Set some cells
235 testCell := &uv.Cell{Content: "X", Width: 1}
236 scr.SetCell(0, 0, testCell)
237 scr.SetCell(5, 2, testCell)
238
239 Clear(scr)
240
241 if !scr.clearCalled {
242 t.Error("Clear method was not called")
243 }
244
245 // Verify cells are cleared
246 if cell := scr.CellAt(0, 0); cell != nil && cell.Content != " " {
247 t.Errorf("Cell at (0,0) not cleared, got %v", cell)
248 }
249 if cell := scr.CellAt(5, 2); cell != nil && cell.Content != " " {
250 t.Errorf("Cell at (5,2) not cleared, got %v", cell)
251 }
252 })
253
254 t.Run("without Clear method", func(t *testing.T) {
255 scr := newMockScreen(10, 5)
256
257 // Set some cells
258 testCell := &uv.Cell{Content: "X", Width: 1}
259 scr.SetCell(0, 0, testCell)
260 scr.SetCell(5, 2, testCell)
261
262 Clear(scr)
263
264 // Verify cells are cleared (filled with nil/empty)
265 if cell := scr.CellAt(0, 0); cell != nil && cell.Content != " " {
266 t.Errorf("Cell at (0,0) not cleared, got %v", cell)
267 }
268 if cell := scr.CellAt(5, 2); cell != nil && cell.Content != " " {
269 t.Errorf("Cell at (5,2) not cleared, got %v", cell)
270 }
271 })
272 }
273
274 func TestClearArea(t *testing.T) {
275 t.Run("with ClearArea method", func(t *testing.T) {
276 scr := &mockScreenWithClearArea{
277 mockScreen: newMockScreen(10, 5),
278 }
279
280 // Set cells across the screen
281 testCell := &uv.Cell{Content: "X", Width: 1}
282 for y := 0; y < 5; y++ {
283 for x := 0; x < 10; x++ {
284 scr.SetCell(x, y, testCell)
285 }
286 }
287
288 area := uv.Rect(2, 1, 4, 2)
289 ClearArea(scr, area)
290
291 if !scr.clearAreaCalled {
292 t.Error("ClearArea method was not called")
293 }
294
295 if scr.lastArea != area {
296 t.Errorf("ClearArea called with wrong area, expected %v, got %v", area, scr.lastArea)
297 }
298
299 // Verify only the area is cleared
300 for y := 0; y < 5; y++ {
301 for x := 0; x < 10; x++ {
302 cell := scr.CellAt(x, y)
303 if x >= 2 && x < 6 && y >= 1 && y < 3 {
304 // Inside cleared area
305 if cell != nil && cell.Content != " " {
306 t.Errorf("Cell at (%d,%d) should be cleared, got %v", x, y, cell)
307 }
308 } else {
309 // Outside cleared area
310 if cell == nil || cell.Content != "X" {
311 t.Errorf("Cell at (%d,%d) should not be cleared, got %v", x, y, cell)
312 }
313 }
314 }
315 }
316 })
317
318 t.Run("without ClearArea method", func(t *testing.T) {
319 scr := newMockScreen(10, 5)
320
321 // Set cells across the screen
322 testCell := &uv.Cell{Content: "X", Width: 1}
323 for y := 0; y < 5; y++ {
324 for x := 0; x < 10; x++ {
325 scr.SetCell(x, y, testCell)
326 }
327 }
328
329 area := uv.Rect(2, 1, 4, 2)
330 ClearArea(scr, area)
331
332 // Verify only the area is cleared
333 for y := 0; y < 5; y++ {
334 for x := 0; x < 10; x++ {
335 cell := scr.CellAt(x, y)
336 if x >= 2 && x < 6 && y >= 1 && y < 3 {
337 // Inside cleared area
338 if cell != nil && cell.Content != " " {
339 t.Errorf("Cell at (%d,%d) should be cleared, got %v", x, y, cell)
340 }
341 } else {
342 // Outside cleared area
343 if cell == nil || cell.Content != "X" {
344 t.Errorf("Cell at (%d,%d) should not be cleared, got %v", x, y, cell)
345 }
346 }
347 }
348 }
349 })
350 }
351
352 func TestFill(t *testing.T) {
353 t.Run("with Fill method", func(t *testing.T) {
354 scr := &mockScreenWithFill{
355 mockScreen: newMockScreen(10, 5),
356 }
357
358 fillCell := &uv.Cell{Content: "F", Width: 1}
359 Fill(scr, fillCell)
360
361 if !scr.fillCalled {
362 t.Error("Fill method was not called")
363 }
364
365 if scr.lastCell != fillCell {
366 t.Errorf("Fill called with wrong cell, expected %v, got %v", fillCell, scr.lastCell)
367 }
368
369 // Verify all cells are filled
370 for y := 0; y < 5; y++ {
371 for x := 0; x < 10; x++ {
372 cell := scr.CellAt(x, y)
373 if cell == nil || cell.Content != "F" {
374 t.Errorf("Cell at (%d,%d) not filled correctly, got %v", x, y, cell)
375 }
376 }
377 }
378 })
379
380 t.Run("without Fill method", func(t *testing.T) {
381 scr := newMockScreen(10, 5)
382
383 fillCell := &uv.Cell{Content: "F", Width: 1}
384 Fill(scr, fillCell)
385
386 // Verify all cells are filled
387 for y := 0; y < 5; y++ {
388 for x := 0; x < 10; x++ {
389 cell := scr.CellAt(x, y)
390 if cell == nil || cell.Content != "F" {
391 t.Errorf("Cell at (%d,%d) not filled correctly, got %v", x, y, cell)
392 }
393 }
394 }
395 })
396
397 t.Run("with nil cell", func(t *testing.T) {
398 scr := newMockScreen(10, 5)
399
400 // Set some cells first
401 testCell := &uv.Cell{Content: "X", Width: 1}
402 scr.SetCell(0, 0, testCell)
403 scr.SetCell(5, 2, testCell)
404
405 Fill(scr, nil)
406
407 // Verify all cells are cleared (filled with empty)
408 for y := 0; y < 5; y++ {
409 for x := 0; x < 10; x++ {
410 cell := scr.CellAt(x, y)
411 if cell != nil && cell.Content != " " {
412 t.Errorf("Cell at (%d,%d) not cleared, got %v", x, y, cell)
413 }
414 }
415 }
416 })
417 }
418
419 func TestFillArea(t *testing.T) {
420 t.Run("with FillArea method", func(t *testing.T) {
421 scr := &mockScreenWithFillArea{
422 mockScreen: newMockScreen(10, 5),
423 }
424
425 fillCell := &uv.Cell{Content: "A", Width: 1}
426 area := uv.Rect(2, 1, 4, 2)
427 FillArea(scr, fillCell, area)
428
429 if !scr.fillAreaCalled {
430 t.Error("FillArea method was not called")
431 }
432
433 if scr.lastCell != fillCell {
434 t.Errorf("FillArea called with wrong cell, expected %v, got %v", fillCell, scr.lastCell)
435 }
436
437 if scr.lastArea != area {
438 t.Errorf("FillArea called with wrong area, expected %v, got %v", area, scr.lastArea)
439 }
440
441 // Verify only the area is filled
442 for y := 0; y < 5; y++ {
443 for x := 0; x < 10; x++ {
444 cell := scr.CellAt(x, y)
445 if x >= 2 && x < 6 && y >= 1 && y < 3 {
446 // Inside filled area
447 if cell == nil || cell.Content != "A" {
448 t.Errorf("Cell at (%d,%d) should be filled with 'A', got %v", x, y, cell)
449 }
450 }
451 }
452 }
453 })
454
455 t.Run("without FillArea method", func(t *testing.T) {
456 scr := newMockScreen(10, 5)
457
458 fillCell := &uv.Cell{Content: "B", Width: 1}
459 area := uv.Rect(2, 1, 4, 2)
460 FillArea(scr, fillCell, area)
461
462 // Verify only the area is filled
463 for y := 0; y < 5; y++ {
464 for x := 0; x < 10; x++ {
465 cell := scr.CellAt(x, y)
466 if x >= 2 && x < 6 && y >= 1 && y < 3 {
467 // Inside filled area
468 if cell == nil || cell.Content != "B" {
469 t.Errorf("Cell at (%d,%d) should be filled with 'B', got %v", x, y, cell)
470 }
471 }
472 }
473 }
474 })
475
476 t.Run("with nil cell", func(t *testing.T) {
477 scr := newMockScreen(10, 5)
478
479 // Set cells across the screen
480 testCell := &uv.Cell{Content: "X", Width: 1}
481 for y := 0; y < 5; y++ {
482 for x := 0; x < 10; x++ {
483 scr.SetCell(x, y, testCell)
484 }
485 }
486
487 area := uv.Rect(2, 1, 4, 2)
488 FillArea(scr, nil, area)
489
490 // Verify only the area is cleared
491 for y := 0; y < 5; y++ {
492 for x := 0; x < 10; x++ {
493 cell := scr.CellAt(x, y)
494 if x >= 2 && x < 6 && y >= 1 && y < 3 {
495 // Inside filled area (should be empty)
496 if cell != nil && cell.Content != " " {
497 t.Errorf("Cell at (%d,%d) should be cleared, got %v", x, y, cell)
498 }
499 } else {
500 // Outside filled area
501 if cell == nil || cell.Content != "X" {
502 t.Errorf("Cell at (%d,%d) should not be changed, got %v", x, y, cell)
503 }
504 }
505 }
506 }
507 })
508 }
509
510 func TestClone(t *testing.T) {
511 t.Run("with Clone method", func(t *testing.T) {
512 scr := &mockScreenWithClone{
513 mockScreen: newMockScreen(10, 5),
514 }
515
516 // Set some cells
517 scr.SetCell(0, 0, &uv.Cell{Content: "A", Width: 1})
518 scr.SetCell(5, 2, &uv.Cell{Content: "B", Width: 1})
519 scr.SetCell(9, 4, &uv.Cell{Content: "C", Width: 1})
520
521 cloned := Clone(scr)
522
523 if !scr.cloneCalled {
524 t.Error("Clone method was not called")
525 }
526
527 if cloned == nil {
528 t.Fatal("Clone returned nil")
529 }
530
531 // Verify cloned buffer has same content
532 if cloned.Width() != 10 || cloned.Height() != 5 {
533 t.Errorf("Cloned buffer has wrong dimensions: %dx%d", cloned.Width(), cloned.Height())
534 }
535
536 // Check specific cells
537 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "A" {
538 t.Errorf("Cloned cell at (0,0) incorrect, got %v", cell)
539 }
540 if cell := cloned.CellAt(5, 2); cell == nil || cell.Content != "B" {
541 t.Errorf("Cloned cell at (5,2) incorrect, got %v", cell)
542 }
543 if cell := cloned.CellAt(9, 4); cell == nil || cell.Content != "C" {
544 t.Errorf("Cloned cell at (9,4) incorrect, got %v", cell)
545 }
546 })
547
548 t.Run("without Clone method", func(t *testing.T) {
549 scr := newMockScreen(10, 5)
550
551 // Set some cells
552 scr.SetCell(0, 0, &uv.Cell{Content: "A", Width: 1})
553 scr.SetCell(5, 2, &uv.Cell{Content: "B", Width: 1})
554 scr.SetCell(9, 4, &uv.Cell{Content: "C", Width: 1})
555
556 cloned := Clone(scr)
557
558 if cloned == nil {
559 t.Fatal("Clone returned nil")
560 }
561
562 // Verify cloned buffer has same content
563 if cloned.Width() != 10 || cloned.Height() != 5 {
564 t.Errorf("Cloned buffer has wrong dimensions: %dx%d", cloned.Width(), cloned.Height())
565 }
566
567 // Check specific cells
568 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "A" {
569 t.Errorf("Cloned cell at (0,0) incorrect, got %v", cell)
570 }
571 if cell := cloned.CellAt(5, 2); cell == nil || cell.Content != "B" {
572 t.Errorf("Cloned cell at (5,2) incorrect, got %v", cell)
573 }
574 if cell := cloned.CellAt(9, 4); cell == nil || cell.Content != "C" {
575 t.Errorf("Cloned cell at (9,4) incorrect, got %v", cell)
576 }
577 })
578 }
579
580 func TestCloneArea(t *testing.T) {
581 t.Run("with CloneArea method", func(t *testing.T) {
582 scr := &mockScreenWithCloneArea{
583 mockScreen: newMockScreen(10, 5),
584 }
585
586 // Set cells across the screen
587 for y := 0; y < 5; y++ {
588 for x := 0; x < 10; x++ {
589 content := string(rune('A' + y*10 + x))
590 scr.SetCell(x, y, &uv.Cell{Content: content, Width: 1})
591 }
592 }
593
594 area := uv.Rect(2, 1, 4, 2)
595 cloned := CloneArea(scr, area)
596
597 if !scr.cloneAreaCalled {
598 t.Error("CloneArea method was not called")
599 }
600
601 if scr.lastArea != area {
602 t.Errorf("CloneArea called with wrong area, expected %v, got %v", area, scr.lastArea)
603 }
604
605 if cloned == nil {
606 t.Fatal("CloneArea returned nil")
607 }
608
609 // Verify cloned buffer has correct dimensions
610 if cloned.Width() != 4 || cloned.Height() != 2 {
611 t.Errorf("Cloned buffer has wrong dimensions: %dx%d, expected 4x2", cloned.Width(), cloned.Height())
612 }
613
614 // Verify content matches the cloned area
615 for y := 0; y < 2; y++ {
616 for x := 0; x < 4; x++ {
617 expectedContent := string(rune('A' + (y+1)*10 + (x + 2)))
618 cell := cloned.CellAt(x, y)
619 if cell == nil || cell.Content != expectedContent {
620 t.Errorf("Cloned cell at (%d,%d) incorrect, expected %s, got %v", x, y, expectedContent, cell)
621 }
622 }
623 }
624 })
625
626 t.Run("without CloneArea method", func(t *testing.T) {
627 scr := newMockScreen(10, 5)
628
629 // Set cells across the screen
630 for y := 0; y < 5; y++ {
631 for x := 0; x < 10; x++ {
632 content := string(rune('A' + y*10 + x))
633 scr.SetCell(x, y, &uv.Cell{Content: content, Width: 1})
634 }
635 }
636
637 area := uv.Rect(2, 1, 4, 2)
638 cloned := CloneArea(scr, area)
639
640 if cloned == nil {
641 t.Fatal("CloneArea returned nil")
642 }
643
644 // Verify cloned buffer has correct dimensions
645 if cloned.Width() != 4 || cloned.Height() != 2 {
646 t.Errorf("Cloned buffer has wrong dimensions: %dx%d, expected 4x2", cloned.Width(), cloned.Height())
647 }
648
649 // Verify content matches the cloned area
650 for y := 0; y < 2; y++ {
651 for x := 0; x < 4; x++ {
652 expectedContent := string(rune('A' + (y+1)*10 + (x + 2)))
653 cell := cloned.CellAt(x, y)
654 if cell == nil || cell.Content != expectedContent {
655 t.Errorf("Cloned cell at (%d,%d) incorrect, expected %s, got %v", x, y, expectedContent, cell)
656 }
657 }
658 }
659 })
660
661 t.Run("with zero cells", func(t *testing.T) {
662 scr := newMockScreen(10, 5)
663
664 // Set some cells but leave some as zero
665 scr.SetCell(2, 1, &uv.Cell{Content: "A", Width: 1})
666 scr.SetCell(4, 2, &uv.Cell{Content: "B", Width: 1})
667 // Leave (3,1) and (5,2) as zero cells
668
669 area := uv.Rect(2, 1, 4, 2)
670 cloned := CloneArea(scr, area)
671
672 if cloned == nil {
673 t.Fatal("CloneArea returned nil")
674 }
675
676 // Check that non-zero cells are cloned
677 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "A" {
678 t.Errorf("Cell at (0,0) should be 'A', got %v", cell)
679 }
680 if cell := cloned.CellAt(2, 1); cell == nil || cell.Content != "B" {
681 t.Errorf("Cell at (2,1) should be 'B', got %v", cell)
682 }
683
684 // Check that zero cells are not cloned (should be empty)
685 if cell := cloned.CellAt(1, 0); cell != nil && !cell.IsZero() && cell.Content != " " {
686 t.Errorf("Cell at (1,0) should be zero or empty, got %v", cell)
687 }
688 if cell := cloned.CellAt(3, 1); cell != nil && !cell.IsZero() && cell.Content != " " {
689 t.Errorf("Cell at (3,1) should be zero or empty, got %v", cell)
690 }
691 })
692 }
693
694 func TestScreenBufferIntegration(t *testing.T) {
695 t.Run("using ScreenBuffer", func(t *testing.T) {
696 scr := uv.NewScreenBuffer(10, 5)
697
698 // Test Clear
699 scr.SetCell(0, 0, &uv.Cell{Content: "X", Width: 1})
700 Clear(scr)
701 if cell := scr.CellAt(0, 0); cell != nil && cell.Content != " " {
702 t.Errorf("Clear failed, cell at (0,0) is %v", cell)
703 }
704
705 // Test Fill
706 fillCell := &uv.Cell{Content: "F", Width: 1}
707 Fill(scr, fillCell)
708 for y := 0; y < 5; y++ {
709 for x := 0; x < 10; x++ {
710 if cell := scr.CellAt(x, y); cell == nil || cell.Content != "F" {
711 t.Errorf("Fill failed at (%d,%d), got %v", x, y, cell)
712 }
713 }
714 }
715
716 // Test ClearArea
717 area := uv.Rect(2, 1, 3, 2)
718 ClearArea(scr, area)
719 for y := 1; y < 3; y++ {
720 for x := 2; x < 5; x++ {
721 if cell := scr.CellAt(x, y); cell != nil && cell.Content != " " {
722 t.Errorf("ClearArea failed at (%d,%d), got %v", x, y, cell)
723 }
724 }
725 }
726
727 // Test FillArea
728 fillCell2 := &uv.Cell{Content: "A", Width: 1}
729 area2 := uv.Rect(1, 1, 2, 2)
730 FillArea(scr, fillCell2, area2)
731 for y := 1; y < 3; y++ {
732 for x := 1; x < 3; x++ {
733 if cell := scr.CellAt(x, y); cell == nil || cell.Content != "A" {
734 t.Errorf("FillArea failed at (%d,%d), got %v", x, y, cell)
735 }
736 }
737 }
738
739 // Test Clone
740 cloned := Clone(scr)
741 if cloned.Width() != 10 || cloned.Height() != 5 {
742 t.Errorf("Clone dimensions wrong: %dx%d", cloned.Width(), cloned.Height())
743 }
744
745 // Test CloneArea
746 area3 := uv.Rect(0, 0, 3, 3)
747 clonedArea := CloneArea(scr, area3)
748 if clonedArea.Width() != 3 || clonedArea.Height() != 3 {
749 t.Errorf("CloneArea dimensions wrong: %dx%d", clonedArea.Width(), clonedArea.Height())
750 }
751 })
752 }
753
754 func TestEdgeCases(t *testing.T) {
755 t.Run("empty screen", func(t *testing.T) {
756 scr := newMockScreen(0, 0)
757
758 // These should not panic
759 Clear(scr)
760 Fill(scr, &uv.Cell{Content: "X", Width: 1})
761 ClearArea(scr, uv.Rect(0, 0, 1, 1))
762 FillArea(scr, &uv.Cell{Content: "X", Width: 1}, uv.Rect(0, 0, 1, 1))
763 Clone(scr)
764 CloneArea(scr, uv.Rect(0, 0, 1, 1))
765 })
766
767 t.Run("wide cells", func(t *testing.T) {
768 scr := uv.NewScreenBuffer(10, 5)
769
770 // Test with wide cell (e.g., emoji or CJK character)
771 wideCell := &uv.Cell{Content: "😀", Width: 2}
772 scr.SetCell(0, 0, wideCell)
773
774 t.Logf("Set wide cell at (0,0): %+v", scr.CellAt(0, 0))
775
776 cloned := Clone(scr)
777 t.Logf("Cloned cell at (0,0): %+v", cloned.CellAt(0, 0))
778
779 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "😀" || cell.Width != 2 {
780 t.Errorf("Wide cell not cloned correctly, got %#v", cell)
781 }
782
783 // Test FillArea with wide cell
784 FillArea(scr, wideCell, uv.Rect(0, 1, 4, 1))
785 for x := 0; x < 4; x += 2 {
786 if cell := scr.CellAt(x, 1); cell == nil || cell.Content != "😀" || cell.Width != 2 {
787 t.Errorf("Wide cell at (%d,1) not filled correctly, got %#v", x, cell)
788 }
789 }
790 })
791
792 t.Run("styled cells", func(t *testing.T) {
793 scr := uv.NewScreenBuffer(10, 5)
794
795 // Test with styled cell
796 styledCell := &uv.Cell{
797 Content: "S",
798 Width: 1,
799 Style: uv.NewStyle().Bold(true).Italic(true),
800 }
801 scr.SetCell(0, 0, styledCell)
802
803 cloned := Clone(scr)
804 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "S" || !cell.Style.Attrs.Contains(uv.BoldAttr) || !cell.Style.Attrs.Contains(uv.ItalicAttr) {
805 t.Errorf("Styled cell not cloned correctly, got %v", cell)
806 }
807 })
808
809 t.Run("cells with links", func(t *testing.T) {
810 scr := uv.NewScreenBuffer(10, 5)
811
812 // Test with cell containing hyperlink
813 linkedCell := &uv.Cell{
814 Content: "L",
815 Width: 1,
816 Link: uv.NewLink("https://example.com", "id=test"),
817 }
818 scr.SetCell(0, 0, linkedCell)
819
820 cloned := Clone(scr)
821 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "L" || cell.Link.URL != "https://example.com" {
822 t.Errorf("Cell with link not cloned correctly, got %v", cell)
823 }
824 })
825 }
826
827 func TestMinimalScreenFallbacks(t *testing.T) {
828 t.Run("Clear fallback", func(t *testing.T) {
829 scr := newMinimalMockScreen(5, 3)
830
831 // Set some cells
832 testCell := &uv.Cell{Content: "X", Width: 1}
833 scr.SetCell(0, 0, testCell)
834 scr.SetCell(2, 1, testCell)
835 scr.SetCell(4, 2, testCell)
836
837 // Clear should use Fill fallback
838 Clear(scr)
839
840 // Verify all cells are cleared
841 for y := 0; y < 3; y++ {
842 for x := 0; x < 5; x++ {
843 cell := scr.CellAt(x, y)
844 if cell != nil && cell.Content != " " {
845 t.Errorf("Cell at (%d,%d) not cleared, got %v", x, y, cell)
846 }
847 }
848 }
849 })
850
851 t.Run("ClearArea fallback", func(t *testing.T) {
852 scr := newMinimalMockScreen(5, 3)
853
854 // Set cells across the screen
855 testCell := &uv.Cell{Content: "X", Width: 1}
856 for y := 0; y < 3; y++ {
857 for x := 0; x < 5; x++ {
858 scr.SetCell(x, y, testCell)
859 }
860 }
861
862 // ClearArea should use FillArea fallback
863 area := uv.Rect(1, 0, 3, 2)
864 ClearArea(scr, area)
865
866 // Verify only the area is cleared
867 for y := 0; y < 3; y++ {
868 for x := 0; x < 5; x++ {
869 cell := scr.CellAt(x, y)
870 if x >= 1 && x < 4 && y >= 0 && y < 2 {
871 // Inside cleared area
872 if cell != nil && cell.Content != " " {
873 t.Errorf("Cell at (%d,%d) should be cleared, got %v", x, y, cell)
874 }
875 } else {
876 // Outside cleared area
877 if cell == nil || cell.Content != "X" {
878 t.Errorf("Cell at (%d,%d) should not be cleared, got %v", x, y, cell)
879 }
880 }
881 }
882 }
883 })
884
885 t.Run("Fill fallback", func(t *testing.T) {
886 scr := newMinimalMockScreen(5, 3)
887
888 fillCell := &uv.Cell{Content: "F", Width: 1}
889 // Fill should use FillArea fallback
890 Fill(scr, fillCell)
891
892 // Verify all cells are filled
893 for y := 0; y < 3; y++ {
894 for x := 0; x < 5; x++ {
895 cell := scr.CellAt(x, y)
896 if cell == nil || cell.Content != "F" {
897 t.Errorf("Cell at (%d,%d) not filled correctly, got %v", x, y, cell)
898 }
899 }
900 }
901 })
902
903 t.Run("FillArea fallback loop", func(t *testing.T) {
904 scr := newMinimalMockScreen(5, 3)
905
906 fillCell := &uv.Cell{Content: "A", Width: 1}
907 area := uv.Rect(1, 1, 2, 1)
908 // FillArea should use the loop fallback
909 FillArea(scr, fillCell, area)
910
911 // Verify only the area is filled
912 for y := 0; y < 3; y++ {
913 for x := 0; x < 5; x++ {
914 cell := scr.CellAt(x, y)
915 if x >= 1 && x < 3 && y >= 1 && y < 2 {
916 // Inside filled area
917 if cell == nil || cell.Content != "A" {
918 t.Errorf("Cell at (%d,%d) should be filled with 'A', got %v", x, y, cell)
919 }
920 } else {
921 // Outside filled area (should be empty)
922 if cell != nil && cell.Content != " " {
923 t.Errorf("Cell at (%d,%d) should be empty, got %v", x, y, cell)
924 }
925 }
926 }
927 }
928 })
929
930 t.Run("Clone fallback", func(t *testing.T) {
931 scr := newMinimalMockScreen(5, 3)
932
933 // Set some cells
934 scr.SetCell(0, 0, &uv.Cell{Content: "A", Width: 1})
935 scr.SetCell(2, 1, &uv.Cell{Content: "B", Width: 1})
936 scr.SetCell(4, 2, &uv.Cell{Content: "C", Width: 1})
937
938 // Clone should use CloneArea fallback
939 cloned := Clone(scr)
940
941 if cloned == nil {
942 t.Fatal("Clone returned nil")
943 }
944
945 // Verify cloned buffer has same dimensions
946 if cloned.Width() != 5 || cloned.Height() != 3 {
947 t.Errorf("Cloned buffer has wrong dimensions: %dx%d", cloned.Width(), cloned.Height())
948 }
949
950 // Check specific cells
951 if cell := cloned.CellAt(0, 0); cell == nil || cell.Content != "A" {
952 t.Errorf("Cloned cell at (0,0) incorrect, got %v", cell)
953 }
954 if cell := cloned.CellAt(2, 1); cell == nil || cell.Content != "B" {
955 t.Errorf("Cloned cell at (2,1) incorrect, got %v", cell)
956 }
957 if cell := cloned.CellAt(4, 2); cell == nil || cell.Content != "C" {
958 t.Errorf("Cloned cell at (4,2) incorrect, got %v", cell)
959 }
960 })
961
962 t.Run("CloneArea fallback loop", func(t *testing.T) {
963 scr := newMinimalMockScreen(5, 3)
964
965 // Set cells across the screen
966 for y := 0; y < 3; y++ {
967 for x := 0; x < 5; x++ {
968 content := string(rune('A' + y*5 + x))
969 scr.SetCell(x, y, &uv.Cell{Content: content, Width: 1})
970 }
971 }
972
973 // CloneArea should use the loop fallback
974 area := uv.Rect(1, 0, 3, 2)
975 cloned := CloneArea(scr, area)
976
977 if cloned == nil {
978 t.Fatal("CloneArea returned nil")
979 }
980
981 // Verify cloned buffer has correct dimensions
982 if cloned.Width() != 3 || cloned.Height() != 2 {
983 t.Errorf("Cloned buffer has wrong dimensions: %dx%d, expected 3x2", cloned.Width(), cloned.Height())
984 }
985
986 // Verify content matches the cloned area
987 for y := 0; y < 2; y++ {
988 for x := 0; x < 3; x++ {
989 expectedContent := string(rune('A' + y*5 + (x+1)))
990 cell := cloned.CellAt(x, y)
991 if cell == nil || cell.Content != expectedContent {
992 t.Errorf("Cloned cell at (%d,%d) incorrect, expected %s, got %v", x, y, expectedContent, cell)
993 }
994 }
995 }
996 })
997
998 t.Run("CloneArea with nil and zero cells", func(t *testing.T) {
999 scr := newMinimalMockScreen(5, 3)
1000
1001 // Set some cells but leave some as zero/empty
1002 scr.SetCell(1, 0, &uv.Cell{Content: "A", Width: 1})
1003 scr.SetCell(3, 1, &uv.Cell{Content: "B", Width: 1})
1004 // Set a zero cell explicitly
1005 scr.SetCell(2, 0, &uv.Cell{})
1006 // Leave other cells as empty
1007
1008 area := uv.Rect(0, 0, 5, 2)
1009 cloned := CloneArea(scr, area)
1010
1011 if cloned == nil {
1012 t.Fatal("CloneArea returned nil")
1013 }
1014
1015 // Check that non-zero cells are cloned
1016 if cell := cloned.CellAt(1, 0); cell == nil || cell.Content != "A" {
1017 t.Errorf("Cell at (1,0) should be 'A', got %v", cell)
1018 }
1019 if cell := cloned.CellAt(3, 1); cell == nil || cell.Content != "B" {
1020 t.Errorf("Cell at (3,1) should be 'B', got %v", cell)
1021 }
1022
1023 // Check that empty cells remain empty
1024 if cell := cloned.CellAt(0, 0); cell != nil && !cell.IsZero() && cell.Content != " " {
1025 t.Errorf("Cell at (0,0) should be empty, got %v", cell)
1026 }
1027 if cell := cloned.CellAt(2, 0); cell != nil && !cell.IsZero() && cell.Content != " " {
1028 t.Errorf("Cell at (2,0) should be empty, got %v", cell)
1029 }
1030 if cell := cloned.CellAt(2, 1); cell != nil && !cell.IsZero() && cell.Content != " " {
1031 t.Errorf("Cell at (2,1) should be empty, got %v", cell)
1032 }
1033 })
1034
1035 t.Run("CloneArea with nil cells", func(t *testing.T) {
1036 scr := newNilCellMockScreen(5, 3)
1037
1038 // Set some cells
1039 scr.SetCell(1, 0, &uv.Cell{Content: "A", Width: 1})
1040 scr.SetCell(3, 1, &uv.Cell{Content: "B", Width: 1})
1041
1042 // Make position (2, 1) return nil
1043 scr.SetNilAt(2, 1)
1044
1045 area := uv.Rect(0, 0, 5, 2)
1046 cloned := CloneArea(scr, area)
1047
1048 if cloned == nil {
1049 t.Fatal("CloneArea returned nil")
1050 }
1051
1052 // Check that non-nil cells are cloned
1053 if cell := cloned.CellAt(1, 0); cell == nil || cell.Content != "A" {
1054 t.Errorf("Cell at (1,0) should be 'A', got %v", cell)
1055 }
1056 if cell := cloned.CellAt(3, 1); cell == nil || cell.Content != "B" {
1057 t.Errorf("Cell at (3,1) should be 'B', got %v", cell)
1058 }
1059
1060 // Check that nil cell position remains empty in cloned buffer
1061 if cell := cloned.CellAt(2, 1); cell != nil && !cell.IsZero() && cell.Content != " " {
1062 t.Errorf("Cell at (2,1) should be empty (was nil), got %v", cell)
1063 }
1064 })
1065 }
1066
1067 func BenchmarkClear(b *testing.B) {
1068 scr := uv.NewScreenBuffer(80, 24)
1069 b.ResetTimer()
1070 for i := 0; i < b.N; i++ {
1071 Clear(scr)
1072 }
1073 }
1074
1075 func BenchmarkFill(b *testing.B) {
1076 scr := uv.NewScreenBuffer(80, 24)
1077 cell := &uv.Cell{Content: "X", Width: 1}
1078 b.ResetTimer()
1079 for i := 0; i < b.N; i++ {
1080 Fill(scr, cell)
1081 }
1082 }
1083
1084 func BenchmarkClone(b *testing.B) {
1085 scr := uv.NewScreenBuffer(80, 24)
1086 // Fill with some content
1087 for y := 0; y < 24; y++ {
1088 for x := 0; x < 80; x++ {
1089 scr.SetCell(x, y, &uv.Cell{Content: "X", Width: 1})
1090 }
1091 }
1092 b.ResetTimer()
1093 for i := 0; i < b.N; i++ {
1094 Clone(scr)
1095 }
1096 }
1097
1098 func BenchmarkCloneArea(b *testing.B) {
1099 scr := uv.NewScreenBuffer(80, 24)
1100 // Fill with some content
1101 for y := 0; y < 24; y++ {
1102 for x := 0; x < 80; x++ {
1103 scr.SetCell(x, y, &uv.Cell{Content: "X", Width: 1})
1104 }
1105 }
1106 area := uv.Rect(10, 5, 20, 10)
1107 b.ResetTimer()
1108 for i := 0; i < b.N; i++ {
1109 CloneArea(scr, area)
1110 }
1111 }