From e54d4e9414c5c10f7c46c8fb96d9e75a45233f1a Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 7 Jul 2021 13:24:46 +0100 Subject: [PATCH] Scripted user tests. --- color_test.go | 27 +++++++++++++++ entry_test.go | 28 ++++++++++++++++ file_filter_test.go | 6 ++-- file_util_test.go | 4 +-- internal/zenutil/color.go | 10 ++++++ internal/zenutil/color_test.go | 25 +++++--------- internal/zenutil/unescape_test.go | 2 +- list_test.go | 55 +++++++++++++++++++++++++++++++ util_unix_test.go | 22 ++++++------- zenity_test.go | 2 +- 10 files changed, 146 insertions(+), 35 deletions(-) diff --git a/color_test.go b/color_test.go index bae1075..5aed6a1 100644 --- a/color_test.go +++ b/color_test.go @@ -3,12 +3,14 @@ package zenity_test import ( "context" "errors" + "fmt" "image/color" "os" "testing" "time" "github.com/ncruces/zenity" + "github.com/ncruces/zenity/internal/zenutil" "go.uber.org/goleak" ) @@ -52,3 +54,28 @@ func TestSelectColor_cancel(t *testing.T) { t.Error("was not canceled:", err) } } + +func TestSelectColor_script(t *testing.T) { + tests := []struct { + name string + call string + want color.Color + err error + }{ + {name: "Cancel", call: "cancel", want: nil, err: zenity.ErrCanceled}, + {name: "Black", call: "choose black", want: color.Black, err: nil}, + {name: "White", call: "choose white", want: color.White, err: nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + zenity.Info(fmt.Sprintf("In the color selection dialog, %s.", tt.call)) + color, err := zenity.SelectColor() + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if !zenutil.ColorEquals(color, tt.want) || err != tt.err { + t.Errorf("SelectColor() = %v, %v; want %v, %v", color, err, tt.want, tt.err) + } + }) + } +} diff --git a/entry_test.go b/entry_test.go index 8022a06..e87547c 100644 --- a/entry_test.go +++ b/entry_test.go @@ -3,6 +3,7 @@ package zenity_test import ( "context" "errors" + "fmt" "os" "testing" "time" @@ -44,3 +45,30 @@ func TestEntry_cancel(t *testing.T) { t.Error("was not canceled:", err) } } + +func TestEntry_script(t *testing.T) { + tests := []struct { + name string + call string + opts []zenity.Option + want string + err error + }{ + {name: "Cancel", call: "cancel", want: "", err: zenity.ErrCanceled}, + {name: "123", call: "enter 123", want: "123", err: nil}, + {name: "abc", call: "enter abc", want: "abc", err: nil}, + {name: "Password", call: "press OK", want: "xpto", err: nil, + opts: []zenity.Option{zenity.HideText(), zenity.EntryText("xpto")}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + text, err := zenity.Entry(fmt.Sprintf("Please, %s.", tt.call), tt.opts...) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if text != tt.want || err != tt.err { + t.Errorf("Entry() = %q, %v; want %q, %v", text, err, tt.want, tt.err) + } + }) + } +} diff --git a/file_filter_test.go b/file_filter_test.go index 4b7e214..e95b7fd 100644 --- a/file_filter_test.go +++ b/file_filter_test.go @@ -17,7 +17,7 @@ func TestFileFilters_name(t *testing.T) { for i, tt := range tests { tt.data.name() if got := tt.data[0].Name; got != tt.want { - t.Errorf("FileFilters.name[%d] = %q, want %q", i, got, tt.want) + t.Errorf("FileFilters.name[%d] = %q; want %q", i, got, tt.want) } } } @@ -46,7 +46,7 @@ func TestFileFilters_simplify(t *testing.T) { for i, tt := range tests { tt.data.simplify() if got := tt.data[0].Patterns; !reflect.DeepEqual(got, tt.want) { - t.Errorf("FileFilters.simplify[%d] = %q, want %q", i, got, tt.want) + t.Errorf("FileFilters.simplify[%d] = %q; want %q", i, got, tt.want) } } } @@ -75,7 +75,7 @@ func TestFileFilters_types(t *testing.T) { } for i, tt := range tests { if got := tt.data.types(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("FileFilters.types[%d] = %v, want %v", i, got, tt.want) + t.Errorf("FileFilters.types[%d] = %v; want %v", i, got, tt.want) } } } diff --git a/file_util_test.go b/file_util_test.go index ecdd451..f45909b 100644 --- a/file_util_test.go +++ b/file_util_test.go @@ -28,10 +28,10 @@ func Test_splitDirAndName(t *testing.T) { for i, tt := range tests { gotDir, gotName := splitDirAndName(tt.path) if gotDir != tt.wantDir { - t.Errorf("splitDirAndName[%d].dir = %q, want %q", i, gotDir, tt.wantDir) + t.Errorf("splitDirAndName[%d].dir = %q; want %q", i, gotDir, tt.wantDir) } if gotName != tt.wantName { - t.Errorf("splitDirAndName[%d].name = %q, want %q", i, gotName, tt.wantName) + t.Errorf("splitDirAndName[%d].name = %q; want %q", i, gotName, tt.wantName) } } } diff --git a/internal/zenutil/color.go b/internal/zenutil/color.go index 8388270..d67562b 100644 --- a/internal/zenutil/color.go +++ b/internal/zenutil/color.go @@ -63,3 +63,13 @@ func UnparseColor(c color.Color) string { return fmt.Sprintf("rgba(%d,%d,%d,%f)", n.R, n.G, n.B, float32(n.A)/255) } } + +// ColorEquals is internal. +func ColorEquals(c1, c2 color.Color) bool { + if c1 == nil || c2 == nil { + return c1 == c2 + } + r1, g1, b1, a1 := c1.RGBA() + r2, g2, b2, a2 := c2.RGBA() + return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2 +} diff --git a/internal/zenutil/color_test.go b/internal/zenutil/color_test.go index 6993630..b252a02 100644 --- a/internal/zenutil/color_test.go +++ b/internal/zenutil/color_test.go @@ -21,11 +21,11 @@ func TestColor_names(t *testing.T) { c1 := colornames.Map[test] c2 := ParseColor(test) c3 := ParseColor(UnparseColor(c1)) - if !colorEq(c1, c2) { - t.Errorf("ParseColor(%s) = %v, want %v", test, c2, c1) + if !ColorEquals(c1, c2) { + t.Errorf("ParseColor(%s) = %v; want %v", test, c2, c1) } - if !colorEq(c1, c3) { - t.Errorf("ParseColor(UnparseColor(%v)) = %v, want %v", c1, c3, c1) + if !ColorEquals(c1, c3) { + t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", c1, c3, c1) } } } @@ -39,8 +39,8 @@ func TestColor_colors(t *testing.T) { } for _, test := range tests { c := ParseColor(UnparseColor(test)) - if !colorEq(c, test) { - t.Errorf("ParseColor(UnparseColor(%v)) = %v, want %v", test, c, test) + if !ColorEquals(c, test) { + t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", test, c, test) } } } @@ -73,17 +73,8 @@ func TestColor_strings(t *testing.T) { } for _, test := range tests { c := ParseColor(test.data) - if !colorEq(c, test.want) { - t.Errorf("ParseColor(%s) = %v, want %v", test.data, c, test.want) + if !ColorEquals(c, test.want) { + t.Errorf("ParseColor(%s) = %v; want %v", test.data, c, test.want) } } } - -func colorEq(c1, c2 color.Color) bool { - if c1 == nil || c2 == nil { - return c1 == c2 - } - r1, g1, b1, a1 := c1.RGBA() - r2, g2, b2, a2 := c2.RGBA() - return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2 -} diff --git a/internal/zenutil/unescape_test.go b/internal/zenutil/unescape_test.go index 3b32290..e5ebc93 100644 --- a/internal/zenutil/unescape_test.go +++ b/internal/zenutil/unescape_test.go @@ -27,7 +27,7 @@ func TestUnescape(t *testing.T) { } for _, tt := range tests { if got := Unescape(tt.data); got != tt.want { - t.Errorf("Unescape(%q) = %q, want %q", tt.data, got, tt.want) + t.Errorf("Unescape(%q) = %q; want %q", tt.data, got, tt.want) } } } diff --git a/list_test.go b/list_test.go index 10eeddb..f8595b1 100644 --- a/list_test.go +++ b/list_test.go @@ -3,7 +3,9 @@ package zenity_test import ( "context" "errors" + "fmt" "os" + "reflect" "testing" "time" @@ -72,3 +74,56 @@ func TestList_cancel(t *testing.T) { t.Error("was not canceled:", err) } } + +func TestList_script(t *testing.T) { + items := []string{"apples", "oranges", "bananas", "strawberries"} + tests := []struct { + name string + call string + opts []zenity.Option + want string + err error + }{ + {name: "Cancel", call: "cancel", want: "", err: zenity.ErrCanceled}, + {name: "Apples", call: "select apples", want: "apples", err: nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + text, err := zenity.List(fmt.Sprintf("Please, %s.", tt.call), items, tt.opts...) + if skip, err := skip(err); skip { + t.Skip("skipping:", err) + } + if text != tt.want || err != tt.err { + t.Errorf("List() = %q, %v; want %q, %v", text, err, tt.want, tt.err) + } + }) + } +} + +func TestListMultiple_script(t *testing.T) { + items := []string{"apples", "oranges", "bananas", "strawberries"} + tests := []struct { + name string + call string + opts []zenity.Option + want []string + err error + }{ + {name: "Cancel", call: "cancel", want: nil, err: zenity.ErrCanceled}, + {name: "Nothing", call: "select nothing", want: []string{}, err: nil}, + {name: "Apples", call: "select apples", want: []string{"apples"}, err: nil}, + {name: "Apples & Oranges", call: "select apples and oranges", + want: []string{"apples", "oranges"}, err: nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := zenity.ListMultiple(fmt.Sprintf("Please, %s.", tt.call), items, tt.opts...) + 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) + } + }) + } +} diff --git a/util_unix_test.go b/util_unix_test.go index 4d7bc5d..bce27b6 100644 --- a/util_unix_test.go +++ b/util_unix_test.go @@ -15,7 +15,7 @@ func Test_appendTitle(t *testing.T) { got := appendTitle(nil, options{title: stringPtr("Title")}) want := []string{"--title", "Title"} if !reflect.DeepEqual(got, want) { - t.Errorf("appendTitle() = %v, want %v", got, want) + t.Errorf("appendTitle() = %v; want %v", got, want) } } @@ -32,7 +32,7 @@ func Test_appendButtons(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := appendButtons(nil, tt.opts); !reflect.DeepEqual(got, tt.want) { - t.Errorf("appendButtons() = %v, want %v", got, tt.want) + t.Errorf("appendButtons() = %v; want %v", got, tt.want) } }) } @@ -50,7 +50,7 @@ func Test_appendWidthHeight(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := appendWidthHeight(nil, tt.opts); !reflect.DeepEqual(got, tt.want) { - t.Errorf("appendWidthHeight() = %v, want %v", got, tt.want) + t.Errorf("appendWidthHeight() = %v; want %v", got, tt.want) } }) } @@ -72,7 +72,7 @@ func Test_appendIcon(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := appendIcon(nil, tt.opts); !reflect.DeepEqual(got, tt.want) { - t.Errorf("appendIcon() = %v, want %v", got, tt.want) + t.Errorf("appendIcon() = %v; want %v", got, tt.want) } }) } @@ -83,13 +83,13 @@ func Test_strResult(t *testing.T) { cancel := exec.Command("false").Run() if out, err := strResult(options{}, []byte("out"), nil); out != "out" || err != nil { - t.Errorf("strResult(out, nil) = %q, %v", out, err) + t.Errorf(`strResult("out", nil) = %q, %v`, out, err) } if out, err := strResult(options{}, []byte("out"), sentinel); out != "" || err != sentinel { - t.Errorf("strResult(out, nil) = %q, %v", out, err) + t.Errorf(`strResult("out", error) = %q, %v`, out, err) } if out, err := strResult(options{}, []byte("out"), cancel); out != "" || err != ErrCanceled { - t.Errorf("strResult(out, nil) = %q, %v", out, err) + t.Errorf(`strResult("out", cancel) = %q, %v`, out, err) } } @@ -99,15 +99,15 @@ func Test_lstResult(t *testing.T) { cancel := exec.Command("false").Run() if out, err := lstResult(options{}, []byte("out"), nil); !reflect.DeepEqual(out, []string{"out"}) || err != nil { - t.Errorf("lstResult(out, nil) = %v, %v", out, err) + t.Errorf(`lstResult("out", nil) = %v, %v`, out, err) } if out, err := lstResult(options{}, []byte("one|two"), nil); !reflect.DeepEqual(out, []string{"one", "two"}) || err != nil { - t.Errorf("lstResult(out, nil) = %v, %v", out, err) + t.Errorf(`lstResult("one|two", nil) = %v, %v`, out, err) } if out, err := lstResult(options{}, []byte("out"), sentinel); out != nil || err != sentinel { - t.Errorf("lstResult(out, nil) = %v, %v", out, err) + t.Errorf(`lstResult("out", error) = %v, %v`, out, err) } if out, err := lstResult(options{}, []byte("out"), cancel); out != nil || err != ErrCanceled { - t.Errorf("lstResult(out, nil) = %q, %v", out, err) + t.Errorf(`lstResult("out", cancel) = %v, %v`, out, err) } } diff --git a/zenity_test.go b/zenity_test.go index 56a11f3..4349cab 100644 --- a/zenity_test.go +++ b/zenity_test.go @@ -61,7 +61,7 @@ func Test_applyOptions(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := applyOptions([]Option{tt.args}); !reflect.DeepEqual(got, tt.want) { - t.Errorf("applyOptions() = %v, want %v", got, tt.want) + t.Errorf("applyOptions() = %v; want %v", got, tt.want) } }) }