zenity/entry_test.go

75 lines
1.7 KiB
Go
Raw Normal View History

2021-04-05 18:19:02 -04:00
package zenity_test
import (
"context"
"errors"
2021-07-07 08:24:46 -04:00
"fmt"
2021-04-05 18:19:02 -04:00
"os"
"testing"
"time"
"github.com/ncruces/zenity"
2021-04-30 14:19:14 -04:00
"go.uber.org/goleak"
2021-04-05 18:19:02 -04:00
)
func ExampleEntry() {
zenity.Entry("Enter new text:",
zenity.Title("Add a new entry"))
// Output:
}
2021-04-30 14:19:14 -04:00
func TestEntry_timeout(t *testing.T) {
defer goleak.VerifyNone(t)
2021-04-05 18:19:02 -04:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10)
2021-04-30 14:19:14 -04:00
defer cancel()
2021-04-05 18:19:02 -04:00
2021-04-29 11:05:28 -04:00
_, err := zenity.Entry("", zenity.Context(ctx))
2021-07-06 20:01:22 -04:00
if skip, err := skip(err); skip {
2021-06-18 10:04:09 -04:00
t.Skip("skipping:", err)
}
2021-04-05 18:19:02 -04:00
if !os.IsTimeout(err) {
t.Error("did not timeout:", err)
}
}
2021-04-30 14:19:14 -04:00
func TestEntry_cancel(t *testing.T) {
defer goleak.VerifyNone(t)
2021-04-05 18:19:02 -04:00
ctx, cancel := context.WithCancel(context.Background())
cancel()
2021-04-29 11:05:28 -04:00
_, err := zenity.Entry("", zenity.Context(ctx))
2021-07-06 20:01:22 -04:00
if skip, err := skip(err); skip {
2021-06-18 10:04:09 -04:00
t.Skip("skipping:", err)
}
2021-04-05 18:19:02 -04:00
if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err)
}
}
2021-07-07 08:24:46 -04:00
func TestEntry_script(t *testing.T) {
tests := []struct {
name string
call string
opts []zenity.Option
want string
err error
}{
{name: "Cancel", call: "cancel", want: "", err: zenity.ErrCanceled},
{name: "123", call: "enter 123", want: "123", err: nil},
{name: "abc", call: "enter abc", want: "abc", err: nil},
{name: "Password", call: "press OK", want: "xpto", err: nil,
opts: []zenity.Option{zenity.HideText(), zenity.EntryText("xpto")}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
text, err := zenity.Entry(fmt.Sprintf("Please, %s.", tt.call), tt.opts...)
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if text != tt.want || err != tt.err {
t.Errorf("Entry() = %q, %v; want %q, %v", text, err, tt.want, tt.err)
}
})
}
}