1package core
2
3import (
4 "math"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8)
9
10func TestFrameColumnsCannotAddressAdjacentRows(t *testing.T) {
11 for _, x := range []int{-1, 3, 4, math.MaxInt} {
12 f := NewFrame(3, 3)
13 f.Set(0, 1, Cell{Rune: 'x', Style: DefaultStyle()})
14 f.ScrollUp(0, 2, 1)
15 before := f.Clone()
16 for y := range f.Height {
17 require.Panics(t, func() { f.Cell(x, y) })
18 require.Panics(t, func() { f.Set(x, y, Cell{Rune: 'z'}) })
19 }
20 for y := range f.Height {
21 require.Equal(t, before.Row(y), f.Row(y))
22 }
23 require.NoError(t, f.CheckInvariants())
24 }
25}
26
27func TestFrameRejectsInvalidRows(t *testing.T) {
28 for _, width := range []int{0, 3} {
29 f := NewFrame(width, 3)
30 require.Len(t, f.Row(1), width)
31 for _, y := range []int{-1, 3, math.MaxInt} {
32 require.Panics(t, func() { f.Row(y) })
33 require.Panics(t, func() { f.Cell(0, y) })
34 require.Panics(t, func() { f.Set(0, y, BlankCell()) })
35 }
36 require.NoError(t, f.CheckInvariants())
37 }
38}
39
40func TestFillRowClipsColumns(t *testing.T) {
41 f := NewFrame(3, 3)
42 f.ScrollDown(0, 2, 1)
43 f.FillRow(1, math.MinInt, math.MaxInt, Cell{Rune: 'x', Style: DefaultStyle()})
44 for y := range f.Height {
45 for x := range f.Width {
46 want := rune(' ')
47 if y == 1 {
48 want = 'x'
49 }
50 require.Equal(t, want, f.Cell(x, y).Rune)
51 }
52 }
53 require.NoError(t, f.CheckInvariants())
54}
55
56func TestReplaceInvalidSourcePreservesDestination(t *testing.T) {
57 for _, src := range []Frame{{}, {Width: 2, Height: 2}, NewFrame(0, 2), NewFrame(2, 0)} {
58 dst := NewFrame(2, 2)
59 dst.Set(1, 1, Cell{Rune: 'x', Style: DefaultStyle()})
60 before := dst.Clone()
61 dst.Replace(src)
62 require.Equal(t, before.Width, dst.Width)
63 require.Equal(t, before.Height, dst.Height)
64 for y := range dst.Height {
65 require.Equal(t, before.Row(y), dst.Row(y))
66 }
67 require.NoError(t, dst.CheckInvariants())
68 }
69}