This commit is contained in:
Nuno Cruces 2022-05-10 10:30:52 +01:00
parent 396f3f0a64
commit 8277866bb0
3 changed files with 39 additions and 3 deletions

View file

@ -3,6 +3,7 @@ package zenity_test
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"os" "os"
"testing" "testing"
"time" "time"
@ -17,7 +18,7 @@ func ExampleCalendar() {
// Output: // Output:
} }
func TestCalendarTimeout(t *testing.T) { func TestCalendar_timeout(t *testing.T) {
defer goleak.VerifyNone(t) defer goleak.VerifyNone(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5) ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
defer cancel() defer cancel()
@ -31,7 +32,7 @@ func TestCalendarTimeout(t *testing.T) {
} }
} }
func TestCalendarCancel(t *testing.T) { func TestCalendar_cancel(t *testing.T) {
defer goleak.VerifyNone(t) defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
@ -44,3 +45,36 @@ func TestCalendarCancel(t *testing.T) {
t.Error("was not canceled:", err) t.Error("was not canceled:", err)
} }
} }
func TestCalendar_script(t *testing.T) {
tests := []struct {
name string
call string
opts []zenity.Option
want time.Time
err error
}{
{name: "Cancel", call: "cancel", err: zenity.ErrCanceled},
{name: "Black", call: "choose today", want: time.Now()},
{name: "Rebecca", call: "press OK", want: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local),
opts: []zenity.Option{zenity.DefaultDate(2000, 1, 1)}},
}
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)
}
got.Date()
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
}

View file

@ -43,7 +43,7 @@ func ExampleQuestion() {
func ExampleCustomIcon() { func ExampleCustomIcon() {
zenity.Info("All updates are complete.", zenity.Info("All updates are complete.",
zenity.Title("Information"), zenity.Title("Information"),
zenity.CustomIcon("./testdata/icon.png")) zenity.CustomIcon("testdata/icon.png"))
// Output: // Output:
} }

View file

@ -248,6 +248,8 @@ func saveBackRef(id uintptr, ptr unsafe.Pointer) {
defer backRefs.Unlock() defer backRefs.Unlock()
if backRefs.m == nil { if backRefs.m == nil {
backRefs.m = map[uintptr]unsafe.Pointer{} backRefs.m = map[uintptr]unsafe.Pointer{}
} else if _, ok := backRefs.m[id]; ok {
panic("saveBackRef")
} }
backRefs.m[id] = ptr backRefs.m[id] = ptr
} }