zenity/msg_test.go

144 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-01-10 07:00:38 -05:00
package zenity_test
2020-01-04 22:21:39 -05:00
2020-01-29 06:09:06 -05:00
import (
"context"
"errors"
2021-07-12 09:15:07 -04:00
"fmt"
2020-01-29 06:09:06 -05:00
"os"
"testing"
"time"
"github.com/ncruces/zenity"
2021-04-30 14:19:14 -04:00
"go.uber.org/goleak"
2020-01-29 06:09:06 -05:00
)
2020-01-04 22:21:39 -05:00
2020-01-10 07:00:38 -05:00
func ExampleError() {
2020-01-17 07:28:44 -05:00
zenity.Error("An error has occurred.",
2020-01-10 07:00:38 -05:00
zenity.Title("Error"),
2021-05-11 09:58:04 -04:00
zenity.ErrorIcon)
2020-01-10 07:00:38 -05:00
// Output:
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleInfo() {
zenity.Info("All updates are complete.",
zenity.Title("Information"),
2021-05-11 09:58:04 -04:00
zenity.InfoIcon)
2020-01-10 07:00:38 -05:00
// Output:
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleWarning() {
zenity.Warning("Are you sure you want to proceed?",
zenity.Title("Warning"),
2021-05-11 09:58:04 -04:00
zenity.WarningIcon)
2020-01-10 07:00:38 -05:00
// Output:
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleQuestion() {
zenity.Question("Are you sure you want to proceed?",
zenity.Title("Question"),
2021-05-11 09:58:04 -04:00
zenity.QuestionIcon)
2020-01-10 07:00:38 -05:00
// Output:
2020-01-04 22:21:39 -05:00
}
2020-01-29 06:09:06 -05:00
2021-07-12 09:15:07 -04:00
var msgFuncs = []struct {
name string
fn func(string, ...zenity.Option) error
}{
{"Error", zenity.Error},
{"Info", zenity.Info},
{"Warning", zenity.Warning},
{"Question", zenity.Question},
2020-01-29 06:09:06 -05:00
}
2021-04-30 14:19:14 -04:00
func TestMessage_timeout(t *testing.T) {
2021-07-12 09:15:07 -04:00
for _, tt := range msgFuncs {
t.Run(tt.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
2021-07-29 11:20:50 -04:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
2021-07-12 09:15:07 -04:00
defer cancel()
2020-01-29 06:09:06 -05:00
2021-07-12 09:15:07 -04:00
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)
}
})
2020-01-29 06:09:06 -05:00
}
}
2021-04-30 14:19:14 -04:00
func TestMessage_cancel(t *testing.T) {
2021-07-12 09:15:07 -04:00
for _, tt := range msgFuncs {
t.Run(tt.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
cancel()
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)
}
})
}
}
2020-01-29 06:09:06 -05:00
2021-07-12 09:15:07 -04:00
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
}{
2021-07-12 20:43:52 -04:00
{name: "QuestionYes", call: "press Yes"},
2021-07-12 09:15:07 -04:00
{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)
}
})
2020-01-29 06:09:06 -05:00
}
}
func TestMessage_callbacks(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
for i := 0; i < 2000; i++ {
zenity.Error("text", zenity.Context(ctx))
}
}