From 8277866bb0cab35879c5e623be48ed1adf85be8b Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 10 May 2022 10:30:52 +0100 Subject: [PATCH] Tests. --- date_test.go | 38 ++++++++++++++++++++++++++++++++++++-- msg_test.go | 2 +- util_windows.go | 2 ++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/date_test.go b/date_test.go index e775dfc..bad7c0b 100644 --- a/date_test.go +++ b/date_test.go @@ -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 +} diff --git a/msg_test.go b/msg_test.go index fca4554..4983438 100644 --- a/msg_test.go +++ b/msg_test.go @@ -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: } diff --git a/util_windows.go b/util_windows.go index 6752f94..43e8046 100644 --- a/util_windows.go +++ b/util_windows.go @@ -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 }