Skip to content

Commit

Permalink
test: Perform DoEvent() as long as normal or draw events are present
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JoeKar committed Nov 1, 2023
1 parent 8ed303a commit 8f53bed
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions cmd/micro/micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,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) {
Expand Down Expand Up @@ -149,6 +152,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 {
Expand Down Expand Up @@ -188,14 +201,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
}
Expand Down Expand Up @@ -234,6 +240,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
Expand Down Expand Up @@ -297,6 +308,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)
Expand Down

0 comments on commit 8f53bed

Please sign in to comment.