zenity/msg_test.go

75 lines
1.3 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"
"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-04-29 11:05:28 -04:00
var msgFuncs = []func(string, ...zenity.Option) error{
2020-01-29 06:09:06 -05:00
zenity.Error,
zenity.Info,
zenity.Warning,
zenity.Question,
}
2021-04-30 14:19:14 -04:00
func TestMessage_timeout(t *testing.T) {
2020-01-29 06:09:06 -05:00
for _, f := range msgFuncs {
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10)
2021-04-29 11:05:28 -04:00
err := f("text", zenity.Context(ctx))
2020-01-29 06:09:06 -05:00
if !os.IsTimeout(err) {
2020-01-29 09:15:21 -05:00
t.Error("did not timeout:", err)
2020-01-29 06:09:06 -05:00
}
cancel()
2021-04-30 14:19:14 -04:00
goleak.VerifyNone(t)
2020-01-29 06:09:06 -05:00
}
}
2021-04-30 14:19:14 -04:00
func TestMessage_cancel(t *testing.T) {
defer goleak.VerifyNone(t)
2020-01-29 06:09:06 -05:00
ctx, cancel := context.WithCancel(context.Background())
cancel()
for _, f := range msgFuncs {
2021-04-29 11:05:28 -04:00
err := f("text", zenity.Context(ctx))
2020-01-29 06:09:06 -05:00
if !errors.Is(err, context.Canceled) {
2020-01-29 09:15:21 -05:00
t.Error("was not canceled:", err)
2020-01-29 06:09:06 -05:00
}
}
}