zenity/util_unix_test.go

117 lines
3.8 KiB
Go
Raw Permalink Normal View History

2022-03-24 10:37:37 -04:00
//go:build !windows
2021-07-06 20:54:32 -04:00
package zenity
import (
"errors"
"os/exec"
"reflect"
"testing"
"github.com/ncruces/zenity/internal/zenutil"
)
func Test_appendTitle(t *testing.T) {
got := appendTitle(nil, options{title: stringPtr("Title")})
want := []string{"--title", "Title"}
if !reflect.DeepEqual(got, want) {
2021-07-07 08:24:46 -04:00
t.Errorf("appendTitle() = %v; want %v", got, want)
2021-07-06 20:54:32 -04:00
}
}
func Test_appendButtons(t *testing.T) {
tests := []struct {
name string
opts options
want []string
}{
{name: "OK", opts: options{okLabel: stringPtr("OK")}, want: []string{"--ok-label", "OK"}},
{name: "Cancel", opts: options{cancelLabel: stringPtr("Cancel")}, want: []string{"--cancel-label", "Cancel"}},
{name: "Extra", opts: options{extraButton: stringPtr("Extra")}, want: []string{"--extra-button", "Extra"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := appendButtons(nil, tt.opts); !reflect.DeepEqual(got, tt.want) {
2021-07-07 08:24:46 -04:00
t.Errorf("appendButtons() = %v; want %v", got, tt.want)
2021-07-06 20:54:32 -04:00
}
})
}
}
func Test_appendWidthHeight(t *testing.T) {
tests := []struct {
name string
opts options
want []string
}{
{name: "Width", opts: options{width: 100}, want: []string{"--width", "100"}},
{name: "Height", opts: options{height: 100}, want: []string{"--height", "100"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := appendWidthHeight(nil, tt.opts); !reflect.DeepEqual(got, tt.want) {
2021-07-07 08:24:46 -04:00
t.Errorf("appendWidthHeight() = %v; want %v", got, tt.want)
2021-07-06 20:54:32 -04:00
}
})
}
}
func Test_appendIcon(t *testing.T) {
tests := []struct {
name string
opts options
want []string
}{
{name: "NoIcon", opts: options{icon: NoIcon}, want: nil},
{name: "Info", opts: options{icon: InfoIcon}, want: []string{"--window-icon=info"}},
{name: "Error", opts: options{icon: ErrorIcon}, want: []string{"--window-icon=error"}},
{name: "Warning", opts: options{icon: WarningIcon}, want: []string{"--window-icon=warning"}},
{name: "Question", opts: options{icon: QuestionIcon}, want: []string{"--window-icon=question"}},
{name: "Password", opts: options{icon: PasswordIcon}, want: nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := appendIcon(nil, tt.opts); !reflect.DeepEqual(got, tt.want) {
2021-07-07 08:24:46 -04:00
t.Errorf("appendIcon() = %v; want %v", got, tt.want)
2021-07-06 20:54:32 -04:00
}
})
}
}
func Test_strResult(t *testing.T) {
sentinel := errors.New("sentinel")
cancel := exec.Command("false").Run()
if out, err := strResult(options{}, []byte("out"), nil); out != "out" || err != nil {
2021-07-07 08:24:46 -04:00
t.Errorf(`strResult("out", nil) = %q, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
if out, err := strResult(options{}, []byte("out"), sentinel); out != "" || err != sentinel {
2021-07-07 08:24:46 -04:00
t.Errorf(`strResult("out", error) = %q, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
if out, err := strResult(options{}, []byte("out"), cancel); out != "" || err != ErrCanceled {
2021-07-07 08:24:46 -04:00
t.Errorf(`strResult("out", cancel) = %q, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
}
func Test_lstResult(t *testing.T) {
zenutil.Separator = "|"
sentinel := errors.New("sentinel")
cancel := exec.Command("false").Run()
2021-07-08 13:56:28 -04:00
if out, err := lstResult(options{}, []byte(""), nil); !reflect.DeepEqual(out, []string{}) || err != nil {
t.Errorf(`lstResult("out", nil) = %v, %v`, out, err)
}
2021-07-06 20:54:32 -04:00
if out, err := lstResult(options{}, []byte("out"), nil); !reflect.DeepEqual(out, []string{"out"}) || err != nil {
2021-07-07 08:24:46 -04:00
t.Errorf(`lstResult("out", nil) = %v, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
if out, err := lstResult(options{}, []byte("one|two"), nil); !reflect.DeepEqual(out, []string{"one", "two"}) || err != nil {
2021-07-07 08:24:46 -04:00
t.Errorf(`lstResult("one|two", nil) = %v, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
if out, err := lstResult(options{}, []byte("out"), sentinel); out != nil || err != sentinel {
2021-07-07 08:24:46 -04:00
t.Errorf(`lstResult("out", error) = %v, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
if out, err := lstResult(options{}, []byte("out"), cancel); out != nil || err != ErrCanceled {
2021-07-07 08:24:46 -04:00
t.Errorf(`lstResult("out", cancel) = %v, %v`, out, err)
2021-07-06 20:54:32 -04:00
}
}