zenity/msg_test.go

72 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"
)
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"),
zenity.Icon(zenity.ErrorIcon))
// 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"),
zenity.Icon(zenity.InfoIcon))
// 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"),
zenity.Icon(zenity.WarningIcon))
// 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"),
zenity.Icon(zenity.QuestionIcon))
// Output:
2020-01-04 22:21:39 -05:00
}
2020-01-29 06:09:06 -05:00
var msgFuncs = []func(string, ...zenity.Option) (bool, error){
zenity.Error,
zenity.Info,
zenity.Warning,
zenity.Question,
}
func TestMessageTimeout(t *testing.T) {
for _, f := range msgFuncs {
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10)
_, err := f("text", zenity.Context(ctx))
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()
}
}
func TestMessageCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
for _, f := range msgFuncs {
_, err := f("text", zenity.Context(ctx))
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
}
}
}