mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
126 lines
2.6 KiB
Go
126 lines
2.6 KiB
Go
package peco
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/mattn/go-runewidth"
|
|
"github.com/nsf/termbox-go"
|
|
)
|
|
|
|
type dummyScreen struct {
|
|
*interceptor
|
|
width int
|
|
height int
|
|
}
|
|
|
|
func setDummyScreen() (*interceptor, func()) {
|
|
i := newInterceptor()
|
|
old := screen
|
|
guard := func() { screen = old }
|
|
screen = dummyScreen{
|
|
i,
|
|
100,
|
|
100,
|
|
}
|
|
return i, guard
|
|
}
|
|
|
|
func (d dummyScreen) SetCell(x, y int, ch rune, fg, bg termbox.Attribute) {
|
|
d.record("SetCell", interceptorArgs{x, y, ch, fg, bg})
|
|
}
|
|
func (d dummyScreen) Clear(fg, bg termbox.Attribute) error { return nil }
|
|
func (d dummyScreen) Flush() error {
|
|
d.record("Flush", interceptorArgs{})
|
|
return nil
|
|
}
|
|
func (d dummyScreen) PollEvent() chan termbox.Event { return nil }
|
|
func (d dummyScreen) Size() (int, int) {
|
|
return d.width, d.height
|
|
}
|
|
|
|
func TestLayoutType(t *testing.T) {
|
|
layouts := []struct {
|
|
value LayoutType
|
|
expectOK bool
|
|
}{
|
|
{LayoutTypeTopDown, true},
|
|
{LayoutTypeBottomUp, true},
|
|
{"foobar", false},
|
|
}
|
|
for _, l := range layouts {
|
|
valid := IsValidLayoutType(l.value)
|
|
if valid != l.expectOK {
|
|
t.Errorf("LayoutType %s, expected IsValidLayoutType to return %s, but got %s",
|
|
l.value,
|
|
l.expectOK,
|
|
valid,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPrintScreen(t *testing.T) {
|
|
i, guard := setDummyScreen()
|
|
defer guard()
|
|
|
|
makeVerifier := func(initX, initY int, fill bool) func(string) {
|
|
return func(msg string) {
|
|
i.reset()
|
|
t.Logf("Checking printScreen(%d, %d, %s, %s)", initX, initY, msg, fill)
|
|
width := utf8.RuneCountInString(msg)
|
|
printScreen(initX, initY, termbox.ColorDefault, termbox.ColorDefault, msg, fill)
|
|
events := i.events["SetCell"]
|
|
if !fill {
|
|
if len(events) != width {
|
|
t.Errorf("Expected %d SetCell events, got %d",
|
|
width,
|
|
len(events),
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
// fill == true
|
|
w, _ := screen.Size()
|
|
if rw := runewidth.StringWidth(msg); rw != width {
|
|
w -= rw - width
|
|
}
|
|
if len(events) != w {
|
|
t.Errorf("Expected %d SetCell events, got %d",
|
|
w,
|
|
len(events),
|
|
)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
verify := makeVerifier(0, 0, false)
|
|
verify("Hello, World!")
|
|
verify("日本語")
|
|
|
|
verify = makeVerifier(0, 0, true)
|
|
verify("Hello, World!")
|
|
verify("日本語")
|
|
}
|
|
|
|
func TestStatusBar(t *testing.T) {
|
|
i, guard := setDummyScreen()
|
|
defer guard()
|
|
|
|
st := NewStatusBar(NewCtx(nil), AnchorBottom, 0)
|
|
st.PrintStatus("Hello, World!")
|
|
|
|
events := i.events
|
|
if l := len(events["Flush"]); l != 1 {
|
|
t.Errorf("Expected 1 Flush event, got %d", l)
|
|
return
|
|
}
|
|
|
|
// XXX This is not a test... :/
|
|
// need to think of a way to verify that we're actually doing something
|
|
st.ClearStatus(500 * time.Millisecond)
|
|
<-time.After(time.Second)
|
|
} |