| 1 |
package cellbuf |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
func TestNewCell(t *testing.T) { |
| 8 |
tests := []struct { |
| 9 |
name string |
| 10 |
mainRune rune |
| 11 |
combRune []rune |
| 12 |
want *Cell |
| 13 |
}{ |
| 14 |
{ |
| 15 |
name: "simple ascii", |
| 16 |
mainRune: 'a', |
| 17 |
want: &Cell{Rune: 'a', Width: 1}, |
| 18 |
}, |
| 19 |
{ |
| 20 |
name: "wide character", |
| 21 |
mainRune: '世', |
| 22 |
want: &Cell{Rune: '世', Width: 2}, |
| 23 |
}, |
| 24 |
{ |
| 25 |
name: "combining character", |
| 26 |
mainRune: 'e', |
| 27 |
combRune: []rune{'́'}, // accent |
| 28 |
want: &Cell{Rune: 'e', Comb: []rune{'́'}, Width: 1}, |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "zero width", |
| 32 |
mainRune: '\u200B', // zero-width space |
| 33 |
want: &Cell{Rune: '\u200B', Width: 0}, |
| 34 |
}, |
| 35 |
} |
| 36 |
|
| 37 |
for _, tt := range tests { |
| 38 |
t.Run(tt.name, func(t *testing.T) { |
| 39 |
got := NewCell(tt.mainRune, tt.combRune...) |
| 40 |
if got.Rune != tt.want.Rune { |
| 41 |
t.Errorf("NewCell().Rune = %v, want %v", got.Rune, tt.want.Rune) |
| 42 |
} |
| 43 |
if got.Width != tt.want.Width { |
| 44 |
t.Errorf("NewCell().Width = %v, want %v", got.Width, tt.want.Width) |
| 45 |
} |
| 46 |
if len(got.Comb) != len(tt.want.Comb) { |
| 47 |
t.Errorf("NewCell().Comb length = %v, want %v", len(got.Comb), len(tt.want.Comb)) |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestNewCellString(t *testing.T) { |
| 54 |
tests := []struct { |
| 55 |
name string |
| 56 |
str string |
| 57 |
want *Cell |
| 58 |
}{ |
| 59 |
{ |
| 60 |
name: "empty string", |
| 61 |
str: "", |
| 62 |
want: &Cell{Width: 0}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "simple ascii", |
| 66 |
str: "a", |
| 67 |
want: &Cell{Rune: 'a', Width: 1}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "combining character", |
| 71 |
str: "é", // e with acute accent |
| 72 |
want: &Cell{Rune: 'é', Width: 1}, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "wide character", |
| 76 |
str: "世", |
| 77 |
want: &Cell{Rune: '世', Width: 2}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for _, tt := range tests { |
| 82 |
t.Run(tt.name, func(t *testing.T) { |
| 83 |
got := NewCellString(tt.str) |
| 84 |
if got.Rune != tt.want.Rune { |
| 85 |
t.Errorf("NewCellString().Rune = %v, want %v", got.Rune, tt.want.Rune) |
| 86 |
} |
| 87 |
if got.Width != tt.want.Width { |
| 88 |
t.Errorf("NewCellString().Width = %v, want %v", got.Width, tt.want.Width) |
| 89 |
} |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestLine(t *testing.T) { |
| 95 |
tests := []struct { |
| 96 |
name string |
| 97 |
line Line |
| 98 |
wantStr string |
| 99 |
wantLen int |
| 100 |
wantWidth int |
| 101 |
}{ |
| 102 |
{ |
| 103 |
name: "empty line", |
| 104 |
line: Line{}, |
| 105 |
wantStr: "", |
| 106 |
wantLen: 0, |
| 107 |
wantWidth: 0, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "simple line", |
| 111 |
line: Line{NewCell('a'), NewCell('b'), NewCell('c')}, |
| 112 |
wantStr: "abc", |
| 113 |
wantLen: 3, |
| 114 |
wantWidth: 3, |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "line with nil cells", |
| 118 |
line: Line{nil, NewCell('a'), nil}, |
| 119 |
wantStr: " a", |
| 120 |
wantLen: 3, |
| 121 |
wantWidth: 3, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "line with wide chars", |
| 125 |
line: Line{NewCell('世'), NewCell('界')}, |
| 126 |
wantStr: "世界", |
| 127 |
wantLen: 2, |
| 128 |
wantWidth: 2, |
| 129 |
}, |
| 130 |
} |
| 131 |
|
| 132 |
for _, tt := range tests { |
| 133 |
t.Run(tt.name, func(t *testing.T) { |
| 134 |
if got := tt.line.String(); got != tt.wantStr { |
| 135 |
t.Errorf("Line.String() = %q, want %q", got, tt.wantStr) |
| 136 |
} |
| 137 |
if got := tt.line.Len(); got != tt.wantLen { |
| 138 |
t.Errorf("Line.Len() = %v, want %v", got, tt.wantLen) |
| 139 |
} |
| 140 |
if got := tt.line.Width(); got != tt.wantWidth { |
| 141 |
t.Errorf("Line.Width() = %v, want %v", got, tt.wantWidth) |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestBuffer(t *testing.T) { |
| 148 |
t.Run("creation and resizing", func(t *testing.T) { |
| 149 |
b := NewBuffer(3, 2) |
| 150 |
if b.Width() != 3 { |
| 151 |
t.Errorf("Buffer width = %d, want 3", b.Width()) |
| 152 |
} |
| 153 |
if b.Height() != 2 { |
| 154 |
t.Errorf("Buffer height = %d, want 2", b.Height()) |
| 155 |
} |
| 156 |
|
| 157 |
b.Resize(4, 3) |
| 158 |
if b.Width() != 4 { |
| 159 |
t.Errorf("After resize, buffer width = %d, want 4", b.Width()) |
| 160 |
} |
| 161 |
if b.Height() != 3 { |
| 162 |
t.Errorf("After resize, buffer height = %d, want 3", b.Height()) |
| 163 |
} |
| 164 |
}) |
| 165 |
|
| 166 |
t.Run("cell operations", func(t *testing.T) { |
| 167 |
b := NewBuffer(3, 3) |
| 168 |
cell := NewCell('A') |
| 169 |
|
| 170 |
b.SetCell(1, 1, cell) |
| 171 |
got := b.Cell(1, 1) |
| 172 |
if got.Rune != 'A' { |
| 173 |
t.Errorf("After SetCell, got rune %c, want A", got.Rune) |
| 174 |
} |
| 175 |
}) |
| 176 |
|
| 177 |
t.Run("clear operations", func(t *testing.T) { |
| 178 |
b := NewBuffer(2, 2) |
| 179 |
b.SetCell(0, 0, NewCell('A')) |
| 180 |
b.SetCell(1, 0, NewCell('B')) |
| 181 |
b.Clear() |
| 182 |
|
| 183 |
// if b.Cell(0, 0) != nil { |
| 184 |
// TODO: Should we return nil instead of BlankCell? Nil indicates the |
| 185 |
// default cell. |
| 186 |
|
| 187 |
if !b.Cell(0, 0).Equal(&BlankCell) { |
| 188 |
t.Error("After Clear, cell should be nil") |
| 189 |
} |
| 190 |
}) |
| 191 |
|
| 192 |
t.Run("insert line", func(t *testing.T) { |
| 193 |
b := NewBuffer(3, 3) |
| 194 |
b.SetCell(0, 0, NewCell('A')) |
| 195 |
b.SetCell(0, 1, NewCell('B')) |
| 196 |
|
| 197 |
b.InsertLine(1, 1, nil) |
| 198 |
got := b.Cell(0, 1) |
| 199 |
if !got.Equal(&BlankCell) { |
| 200 |
t.Error("After InsertLine, inserted line should be empty") |
| 201 |
} |
| 202 |
}) |
| 203 |
|
| 204 |
t.Run("delete line", func(t *testing.T) { |
| 205 |
b := NewBuffer(3, 3) |
| 206 |
b.SetCell(0, 0, NewCell('A')) |
| 207 |
b.SetCell(0, 1, NewCell('B')) |
| 208 |
|
| 209 |
b.DeleteLine(0, 1, nil) |
| 210 |
got := b.Cell(0, 0) |
| 211 |
if !got.Equal(NewCell('B')) { |
| 212 |
t.Error("After DeleteLine, first line should be empty") |
| 213 |
} |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
func TestBufferBounds(t *testing.T) { |
| 218 |
b := NewBuffer(4, 3) |
| 219 |
bounds := b.Bounds() |
| 220 |
|
| 221 |
if bounds.Min.X != 0 || bounds.Min.Y != 0 { |
| 222 |
t.Errorf("Buffer bounds min = (%d,%d), want (0,0)", bounds.Min.X, bounds.Min.Y) |
| 223 |
} |
| 224 |
if bounds.Max.X != 4 || bounds.Max.Y != 3 { |
| 225 |
t.Errorf("Buffer bounds max = (%d,%d), want (4,3)", bounds.Max.X, bounds.Max.Y) |
| 226 |
} |
| 227 |
} |