zenity/list_test.go

124 lines
3.0 KiB
Go
Raw Normal View History

2021-04-07 09:16:35 -04:00
package zenity_test
import (
"context"
"errors"
2021-07-07 08:24:46 -04:00
"fmt"
2021-04-07 09:16:35 -04:00
"os"
2021-07-07 08:24:46 -04:00
"reflect"
2021-04-07 09:16:35 -04:00
"testing"
"time"
"github.com/ncruces/zenity"
2021-04-30 14:19:14 -04:00
"go.uber.org/goleak"
2021-04-07 09:16:35 -04:00
)
func ExampleList() {
zenity.List(
"Select items from the list below:",
[]string{"apples", "oranges", "bananas", "strawberries"},
zenity.Title("Select items from the list"),
zenity.DisallowEmpty(),
)
}
func ExampleListItems() {
zenity.ListItems(
"Select items from the list below:",
"apples", "oranges", "bananas", "strawberries")
}
func ExampleListMultiple() {
zenity.ListMultiple(
"Select items from the list below:",
[]string{"apples", "oranges", "bananas", "strawberries"},
zenity.Title("Select items from the list"),
zenity.DefaultItems("apples", "bananas"),
)
}
func ExampleListMultipleItems() {
zenity.ListMultipleItems(
"Select items from the list below:",
"apples", "oranges", "bananas", "strawberries")
}
2021-04-30 14:19:14 -04:00
func TestList_timeout(t *testing.T) {
defer goleak.VerifyNone(t)
2021-04-07 09:16:35 -04:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10)
2021-04-30 14:19:14 -04:00
defer cancel()
2021-04-07 09:16:35 -04:00
2021-04-29 11:05:28 -04:00
_, err := zenity.List("", nil, 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-07 09:16:35 -04:00
if !os.IsTimeout(err) {
t.Error("did not timeout:", err)
}
}
2021-04-30 14:19:14 -04:00
func TestList_cancel(t *testing.T) {
defer goleak.VerifyNone(t)
2021-04-07 09:16:35 -04:00
ctx, cancel := context.WithCancel(context.Background())
cancel()
2021-04-29 11:05:28 -04:00
_, err := zenity.List("", nil, 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-07 09:16:35 -04:00
if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err)
}
}
2021-07-07 08:24:46 -04:00
func TestList_script(t *testing.T) {
items := []string{"apples", "oranges", "bananas", "strawberries"}
tests := []struct {
name string
call string
want string
err error
}{
2021-07-12 20:43:52 -04:00
{name: "Cancel", call: "cancel", err: zenity.ErrCanceled},
{name: "Apples", call: "select apples", want: "apples"},
2021-07-07 08:24:46 -04:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-07-12 20:43:52 -04:00
got, err := zenity.ListItems(fmt.Sprintf("Please, %s.", tt.call), items...)
2021-07-07 08:24:46 -04:00
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
2021-07-12 20:43:52 -04:00
if got != tt.want || err != tt.err {
t.Errorf("List() = %q, %v; want %q, %v", got, err, tt.want, tt.err)
2021-07-07 08:24:46 -04:00
}
})
}
}
func TestListMultiple_script(t *testing.T) {
items := []string{"apples", "oranges", "bananas", "strawberries"}
tests := []struct {
name string
call string
want []string
err error
}{
2021-07-12 20:43:52 -04:00
{name: "Cancel", call: "cancel", err: zenity.ErrCanceled},
{name: "Nothing", call: "select nothing", want: []string{}},
{name: "Apples", call: "select apples", want: []string{"apples"}},
2021-07-07 08:24:46 -04:00
{name: "Apples & Oranges", call: "select apples and oranges",
2021-07-12 20:43:52 -04:00
want: []string{"apples", "oranges"}},
2021-07-07 08:24:46 -04:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-07-12 20:43:52 -04:00
got, err := zenity.ListMultipleItems(fmt.Sprintf("Please, %s.", tt.call), items...)
2021-07-07 08:24:46 -04:00
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !reflect.DeepEqual(got, tt.want) || err != tt.err {
t.Errorf("ListMultiple() = %q, %v; want %v, %v", got, err, tt.want, tt.err)
}
})
}
}