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 (
"context"
"errors"
"fmt"
"os"
"testing"
"time"
@ -17,7 +18,7 @@ func ExampleCalendar() {
// Output:
}
func TestCalendarTimeout(t *testing.T) {
func TestCalendar_timeout(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
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)
ctx, cancel := context.WithCancel(context.Background())
cancel()
@ -44,3 +45,36 @@ func TestCalendarCancel(t *testing.T) {
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() {
zenity.Info("All updates are complete.",
zenity.Title("Information"),
zenity.CustomIcon("./testdata/icon.png"))
zenity.CustomIcon("testdata/icon.png"))
// Output:
}

View File

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