From 15dc2768c4a2259f1adc05d431c9b1748a965564 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 12 Jul 2021 14:15:07 +0100 Subject: [PATCH] Scripted user tests. --- README.md | 4 +- msg_test.go | 110 +++++++++++++++++++++++++++++++++++++++------------- pwd_test.go | 2 +- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 112fd77..4c7e024 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Zenity dialogs for Golang, Windows and macOS -[![PkgGoDev](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/ncruces/zenity) +[![Go Reference](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/ncruces/zenity) [![Go Report](https://goreportcard.com/badge/github.com/ncruces/zenity)](https://goreportcard.com/report/github.com/ncruces/zenity) -[![Go Report](https://gocover.io/_badge/github.com/ncruces/zenity)](https://gocover.io/github.com/ncruces/zenity) +[![Go Cover](https://gocover.io/_badge/github.com/ncruces/zenity)](https://gocover.io/github.com/ncruces/zenity) This repo includes both a cross-platform Go package providing [Zenity](https://help.gnome.org/users/zenity/stable/)-like dialogs diff --git a/msg_test.go b/msg_test.go index 5f1df5d..6e2f86e 100644 --- a/msg_test.go +++ b/msg_test.go @@ -3,6 +3,7 @@ package zenity_test import ( "context" "errors" + "fmt" "os" "testing" "time" @@ -39,42 +40,95 @@ func ExampleQuestion() { // Output: } -var msgFuncs = []func(string, ...zenity.Option) error{ - zenity.Error, - zenity.Info, - zenity.Warning, - zenity.Question, +var msgFuncs = []struct { + name string + fn func(string, ...zenity.Option) error +}{ + {"Error", zenity.Error}, + {"Info", zenity.Info}, + {"Warning", zenity.Warning}, + {"Question", zenity.Question}, } func TestMessage_timeout(t *testing.T) { - for _, f := range msgFuncs { - ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + for _, tt := range msgFuncs { + t.Run(tt.name, func(t *testing.T) { + defer goleak.VerifyNone(t) + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + defer cancel() - err := f("text", zenity.Context(ctx)) - if skip, err := skip(err); skip { - t.Skip("skipping:", err) - } - if !os.IsTimeout(err) { - t.Error("did not timeout:", err) - } - - cancel() - goleak.VerifyNone(t) + err := tt.fn("text", zenity.Context(ctx)) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if !os.IsTimeout(err) { + t.Error("did not timeout:", err) + } + }) } } func TestMessage_cancel(t *testing.T) { - defer goleak.VerifyNone(t) - ctx, cancel := context.WithCancel(context.Background()) - cancel() + for _, tt := range msgFuncs { + t.Run(tt.name, func(t *testing.T) { + defer goleak.VerifyNone(t) + ctx, cancel := context.WithCancel(context.Background()) + cancel() - for _, f := range msgFuncs { - err := f("text", zenity.Context(ctx)) - if skip, err := skip(err); skip { - t.Skip("skipping:", err) - } - if !errors.Is(err, context.Canceled) { - t.Error("was not canceled:", err) - } + err := tt.fn("text", zenity.Context(ctx)) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if !errors.Is(err, context.Canceled) { + t.Error("was not canceled:", err) + } + }) + } +} + +func TestMessage_script(t *testing.T) { + for _, tt := range msgFuncs { + t.Run(tt.name+"OK", func(t *testing.T) { + err := tt.fn("Please, press OK.", zenity.OKLabel("OK")) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if err != nil { + t.Errorf("%s() = %v; want nil", tt.name, err) + } + }) + t.Run(tt.name+"Extra", func(t *testing.T) { + err := tt.fn("Please, press Extra.", zenity.ExtraButton("Extra")) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if err != zenity.ErrExtraButton { + t.Errorf("%s() = %v; want %v", tt.name, err, zenity.ErrExtraButton) + } + }) + } + + tests := []struct { + name string + call string + err error + }{ + {name: "QuestionYes", call: "press Yes", err: nil}, + {name: "QuestionNo", call: "press No", err: zenity.ErrExtraButton}, + {name: "QuestionCancel", call: "cancel", err: zenity.ErrCanceled}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := zenity.Question(fmt.Sprintf("Please, %s.", tt.call), + zenity.OKLabel("Yes"), + zenity.ExtraButton("No"), + zenity.CancelLabel("Cancel")) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if err != tt.err { + t.Errorf("Questtion() = %v; want %v", err, tt.err) + } + }) } } diff --git a/pwd_test.go b/pwd_test.go index d47e424..c51583b 100644 --- a/pwd_test.go +++ b/pwd_test.go @@ -62,7 +62,7 @@ func TestPassword_username(t *testing.T) { t.Skip("skipping:", err) } if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { - if !errors.Is(err, zenity.ErrUnsupported) { + if err != zenity.ErrUnsupported { t.Error("was not unsupported:", err) } } else {