Add WordWrap test

This commit is contained in:
Trevor Slocum 2021-08-02 11:01:40 -07:00
parent 6758d2a787
commit b06b07dadb
1 changed files with 60 additions and 0 deletions

View File

@ -1,7 +1,11 @@
package cview
import (
"strings"
"testing"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
// newTestApp returns a new application connected to a simulation screen.
@ -17,3 +21,59 @@ func newTestApp(root Primitive) (*Application, error) {
return app, nil
}
func TestWordWrap(t *testing.T) {
t.Parallel()
cases := []struct {
text string
width int
expectedLines int
}{
{
"first line\n\nthird line",
80,
3,
},
{
"first line\n\nthird line",
6,
5,
},
{
"string without any newlines",
80,
1,
},
{
"string with one newline, string with one newline, string with one>\n<newline, string with one newline",
32,
5,
},
{
"[red:-:-]Do you[-:-:-] want to\n\nquit the application?",
28,
4,
},
{
Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."),
100,
3,
},
}
for i, c := range cases {
lines := WordWrap(c.text, c.width)
for j, line := range lines {
if strings.Contains(line, "\n") {
t.Errorf("case %d, line %d: wrapped line contains newline", i, j)
}
strippedWidth := runewidth.StringWidth(string(StripTags([]byte(line), true, true)))
if strippedWidth > c.width {
t.Errorf("case %d, line %d: wrapped line exceeds width %d with length %d", i, j, c.width, runewidth.StringWidth(line))
}
}
if len(lines) != c.expectedLines {
t.Errorf("case %d: expected %d lines, got %d", i, c.expectedLines, len(lines))
}
}
}