Scripted user tests.

This commit is contained in:
Nuno Cruces 2021-07-07 13:24:46 +01:00
parent 619a5e9d30
commit e54d4e9414
10 changed files with 146 additions and 35 deletions

View File

@ -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)
}
})
}
}

View File

@ -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)
}
})
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
}
}

View File

@ -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)
}
})
}
}

View File

@ -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)
}
}

View File

@ -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)
}
})
}