Scripted user tests.

This commit is contained in:
Nuno Cruces 2021-07-12 14:15:07 +01:00
parent 317cca7fc8
commit 15dc2768c4
3 changed files with 85 additions and 31 deletions

View file

@ -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

View file

@ -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 {
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))
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)
}
cancel()
goleak.VerifyNone(t)
})
}
}
func TestMessage_cancel(t *testing.T) {
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))
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)
}
})
}
}

View file

@ -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 {