From 2c53d1fcab6a05fa6533f2aabb9af9026542fd7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 22 Sep 2023 19:28:10 +0200 Subject: [PATCH] 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. --- cmd/micro/micro_test.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/micro/micro_test.go b/cmd/micro/micro_test.go index 4c5ca684..cd3dda22 100644 --- a/cmd/micro/micro_test.go +++ b/cmd/micro/micro_test.go @@ -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)