Skip to content

Commit

Permalink
Add tests for GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 23, 2020
1 parent f65bffe commit 5b4c5dd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import (

const rootID = "root"

type runner func(ctx context.Context, t terminalapi.Terminal, c *container.Container, opts ...termdash.Option) error

func Run() error {
return run(termdash.Run)
}

func run(r runner) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -55,7 +61,7 @@ func Run() error {

k := keybinds(ctx, cancel, d)

return termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(k), termdash.RedrawInterval(redrawInterval))
return r(ctx, t, c, termdash.KeyboardSubscriber(k), termdash.RedrawInterval(redrawInterval))
}

func gridLayout(w *widgets) ([]container.Option, error) {
Expand Down
46 changes: 46 additions & 0 deletions gui/gui_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gui

import (
"context"
"fmt"
"testing"

"github.com/mum4k/termdash"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/stretchr/testify/assert"
)

// TODO: Ensure to kill all goroutine when running unit tests.
/*func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}*/

func TestRun(t *testing.T) {
tests := []struct {
name string
r runner
wantErr bool
}{
{
name: "successful running",
r: func(context.Context, terminalapi.Terminal, *container.Container, ...termdash.Option) error {
return nil
},
wantErr: false,
},
{
name: "failed running",
r: func(context.Context, terminalapi.Terminal, *container.Container, ...termdash.Option) error {
return fmt.Errorf("error")
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := run(tt.r)
assert.Equal(t, tt.wantErr, err != nil)
})
}
}

0 comments on commit 5b4c5dd

Please sign in to comment.