zenity/date_test.go

87 lines
1.9 KiB
Go
Raw Normal View History

2021-04-11 22:02:21 -04:00
package zenity_test
import (
"context"
"errors"
2022-05-10 05:30:52 -04:00
"fmt"
2021-04-11 22:02:21 -04:00
"os"
"testing"
"time"
"github.com/ncruces/zenity"
2022-03-29 09:09:01 -04:00
"go.uber.org/goleak"
2021-04-11 22:02:21 -04:00
)
func ExampleCalendar() {
zenity.Calendar("Select a date from below:",
zenity.DefaultDate(2006, time.January, 1))
}
2022-05-10 05:30:52 -04:00
func TestCalendar_timeout(t *testing.T) {
2022-05-18 09:48:09 -04:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2022-03-29 09:09:01 -04:00
defer goleak.VerifyNone(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
defer cancel()
2021-04-11 22:02:21 -04:00
_, err := zenity.Calendar("", zenity.Context(ctx))
2022-03-29 09:09:01 -04:00
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
2021-04-11 22:02:21 -04:00
if !os.IsTimeout(err) {
t.Error("did not timeout:", err)
}
}
2022-05-10 05:30:52 -04:00
func TestCalendar_cancel(t *testing.T) {
2022-03-29 09:09:01 -04:00
defer goleak.VerifyNone(t)
2021-04-11 22:02:21 -04:00
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := zenity.Calendar("", zenity.Context(ctx))
2022-03-29 09:09:01 -04:00
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
2021-04-11 22:02:21 -04:00
if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err)
}
}
2022-05-10 05:30:52 -04:00
func TestCalendar_script(t *testing.T) {
2022-05-18 09:48:09 -04:00
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2022-05-10 05:30:52 -04:00
tests := []struct {
name string
call string
opts []zenity.Option
want time.Time
err error
}{
{name: "Cancel", call: "cancel", err: zenity.ErrCanceled},
2022-05-17 10:36:00 -04:00
{name: "Today", call: "choose today", want: time.Now()},
{name: "Default", call: "press OK", want: time.Date(2006, 1, 1, 0, 0, 0, 0, time.Local),
opts: []zenity.Option{zenity.DefaultDate(2006, 1, 1)}},
2022-05-10 05:30:52 -04:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := zenity.Calendar(fmt.Sprintf("Please, %s.", tt.call), tt.opts...)
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if !DateEquals(got, tt.want) || err != tt.err {
t.Errorf("Calendar() = %v, %v; want %v, %v", got, err, tt.want, tt.err)
}
})
}
}
func DateEquals(t1, t2 time.Time) bool {
d1, m1, y1 := t1.Date()
d2, m2, y2 := t2.Date()
return d1 == d2 && m1 == m2 && y1 == y2
}