1package vt
2
3import (
4 "reflect"
5 "testing"
6
7 renderer "github.com/bnema/vev-vt/core"
8)
9
10func damageKinds(d []renderer.Damage) []renderer.DamageKind {
11 ks := make([]renderer.DamageKind, len(d))
12 for i, dd := range d {
13 ks[i] = dd.Kind
14 }
15 return ks
16}
17
18func hasDamageKind(d []renderer.Damage, kind renderer.DamageKind) bool {
19 for _, dd := range d {
20 if dd.Kind == kind {
21 return true
22 }
23 }
24 return false
25}
26
27func cellAt(s *Screen, x, y int) renderer.Cell {
28 return s.Frame.At(x, y)
29}
30
31func assertFramesEqual(t *testing.T, got, want renderer.Frame) {
32 t.Helper()
33 if !reflect.DeepEqual(got, want) {
34 t.Fatal("frame changed unexpectedly")
35 }
36}
37
38func assertCell(t *testing.T, s *Screen, x, y int, expected rune) {
39 t.Helper()
40 c := cellAt(s, x, y)
41 if c.Rune != expected {
42 t.Errorf("cell(%d,%d) rune = %q, want %q", x, y, c.Rune, expected)
43 }
44 if c.Continuation {
45 t.Errorf("cell(%d,%d) unexpectedly marked as continuation", x, y)
46 }
47}
48
49// assertContinuation asserts the cell at (x,y) is the right half of a
50// wide-character pair (Continuation set, Rune 0).
51func assertContinuation(t *testing.T, s *Screen, x, y int) {
52 t.Helper()
53 c := cellAt(s, x, y)
54 if !c.Continuation {
55 t.Errorf("cell(%d,%d) expected continuation, got rune=%q continuation=%v", x, y, c.Rune, c.Continuation)
56 }
57 if c.Rune != 0 {
58 t.Errorf("cell(%d,%d) continuation rune = %q, want 0", x, y, c.Rune)
59 }
60}
61
62// assertBlank asserts the cell at (x,y) is a blank default cell.
63func assertBlank(t *testing.T, s *Screen, x, y int) {
64 t.Helper()
65 c := cellAt(s, x, y)
66 if c.Rune != ' ' || c.Continuation {
67 t.Errorf("cell(%d,%d) = {rune:%q cont:%v}, want blank space", x, y, c.Rune, c.Continuation)
68 }
69}
70
71func assertNoOrphanWideCells(t *testing.T, s *Screen) {
72 t.Helper()
73 for y := range s.Frame.Height {
74 for x := range s.Frame.Width {
75 c := cellAt(s, x, y)
76 if c.Continuation {
77 if x == 0 || cellAt(s, x-1, y).Continuation || renderer.RuneWidth(cellAt(s, x-1, y).Rune) != 2 {
78 t.Fatalf("orphan continuation at (%d,%d)", x, y)
79 }
80 continue
81 }
82 if renderer.RuneWidth(c.Rune) == 2 && (x+1 >= s.Frame.Width || !cellAt(s, x+1, y).Continuation) {
83 t.Fatalf("orphan wide head at (%d,%d) rune %q", x, y, c.Rune)
84 }
85 }
86 }
87}
88
89func lineText(s *Screen, y int) string {
90 out := make([]rune, s.Frame.Width)
91 for x := range s.Frame.Width {
92 out[x] = s.Frame.At(x, y).Rune
93 }
94 return string(out)
95}
96
97func rowString(row []renderer.Cell) string {
98 out := make([]rune, len(row))
99 for i, cell := range row {
100 out[i] = cell.Rune
101 }
102 return string(out)
103}