pkg/cli/term: Increase coverage of buffer.go and reader.go.

This commit is contained in:
Qi Xiao 2021-10-03 00:18:29 +01:00
parent 0219b99add
commit 00c26410cc
3 changed files with 47 additions and 9 deletions

View File

@ -132,6 +132,17 @@ var bufferTrimToLinesTests = []struct {
Line{Cell{"a", ""}}, Line{Cell{"b", ""}},
}},
},
// Negative low is treated as 0.
{
&Buffer{Width: 10, Lines: Lines{
Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}},
}},
-1, 2,
&Buffer{Width: 10, Lines: Lines{
Line{Cell{"a", ""}}, Line{Cell{"b", ""}},
}},
},
// With dot.
{
&Buffer{Width: 10, Lines: Lines{
@ -280,6 +291,10 @@ var bufferTTYStringTests = []struct {
buf *Buffer
want string
}{
{
nil,
"nil",
},
{
NewBufferBuilder(4).
Write("ABCD").
@ -294,15 +309,16 @@ var bufferTTYStringTests = []struct {
},
{
NewBufferBuilder(4).
Write("AB").SetDotHere().
WriteStringSGR("C", "1").
WriteStringSGR("D", "7").
Write("A").SetDotHere().
WriteStringSGR("B", "1").
WriteStringSGR("C", "7").
Write("D").
Newline().
WriteStringSGR("XY", "7").
Buffer(),
"Width = 4, Dot = (0, 2)\n" +
"Width = 4, Dot = (0, 1)\n" +
"┌────┐\n" +
"│AB\033[1mC\033[;7mD\033[m│\n" +
"│A\033[1mB\033[;7mC\033[mD│\n" +
"│\033[7mXY\033[m$ │\n" +
"└────┘\n",
},

View File

@ -0,0 +1,20 @@
package term
import (
"errors"
"testing"
"src.elv.sh/pkg/tt"
)
var Args = tt.Args
func TestIsReadErrorRecoverable(t *testing.T) {
tt.Test(t, tt.Fn("IsReadErrorRecoverable", IsReadErrorRecoverable), tt.Table{
Args(seqError{}).Rets(true),
Args(ErrStopped).Rets(true),
Args(errTimeout).Rets(true),
Args(errors.New("other error")).Rets(false),
})
}

View File

@ -5,6 +5,7 @@ package term
import (
"os"
"strings"
"testing"
"src.elv.sh/pkg/testutil"
@ -181,9 +182,10 @@ func TestReader_ReadEvent_BadSeq(t *testing.T) {
if err == nil {
t.Fatalf("got nil err with event %v, want non-nil error", ev)
}
errMsg := err.(seqError).msg
if errMsg != test.wantErrMsg {
t.Errorf("got err with message %v, want %v", errMsg, test.wantErrMsg)
errMsg := err.Error()
if !strings.HasPrefix(errMsg, test.wantErrMsg) {
t.Errorf("got err with message %v, want message starting with %v",
errMsg, test.wantErrMsg)
}
})
}
@ -211,7 +213,7 @@ func TestReader_ReadRawEvent(t *testing.T) {
func setupReader(t *testing.T) (Reader, *os.File) {
pr, pw := testutil.MustPipe()
r := newReader(pr)
r := NewReader(pr)
t.Cleanup(func() {
r.Close()
pr.Close()