test: Perform DoEvent() as long as normal or draw events are present

This is necessary since DoEvent() isn't called in a loop like in the main
application, but as one-shot only and a async draw event can lead to ignore
the explicit injected events.
Additional checks have been added to check the presence of the expected buffers.
This commit is contained in:
Jöran Karl
2023-09-22 19:28:10 +02:00
parent f265179def
commit 2c53d1fcab

View File

@@ -109,7 +109,10 @@ func handleEvent() {
if e != nil {
screen.Events <- e
}
DoEvent()
for len(screen.DrawChan()) > 0 || len(screen.Events) > 0 {
DoEvent()
}
}
func injectKey(key tcell.Key, r rune, mod tcell.ModMask) {
@@ -151,6 +154,16 @@ func openFile(file string) {
injectKey(tcell.KeyEnter, rune(tcell.KeyEnter), tcell.ModNone)
}
func findBuffer(file string) *buffer.Buffer {
var buf *buffer.Buffer
for _, b := range buffer.OpenBuffers {
if b.Path == file {
buf = b
}
}
return buf
}
func createTestFile(name string, content string) (string, error) {
testf, err := ioutil.TempFile("", name)
if err != nil {
@@ -190,14 +203,7 @@ func TestSimpleEdit(t *testing.T) {
openFile(file)
var buf *buffer.Buffer
for _, b := range buffer.OpenBuffers {
if b.Path == file {
buf = b
}
}
if buf == nil {
if findBuffer(file) == nil {
t.Errorf("Could not find buffer %s", file)
return
}
@@ -236,6 +242,11 @@ func TestMouse(t *testing.T) {
openFile(file)
if findBuffer(file) == nil {
t.Errorf("Could not find buffer %s", file)
return
}
// buffer:
// base content
// the selections need to happen at different locations to avoid a double click
@@ -299,6 +310,11 @@ func TestSearchAndReplace(t *testing.T) {
openFile(file)
if findBuffer(file) == nil {
t.Errorf("Could not find buffer %s", file)
return
}
injectKey(tcell.KeyCtrlE, rune(tcell.KeyCtrlE), tcell.ModCtrl)
injectString(fmt.Sprintf("replaceall %s %s", "foo", "test_string"))
injectKey(tcell.KeyEnter, rune(tcell.KeyEnter), tcell.ModNone)