2021-07-06 20:54:32 -04:00
|
|
|
package zenity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
|
|
|
"reflect"
|
2022-12-07 09:35:37 -05:00
|
|
|
"runtime"
|
2021-07-06 20:54:32 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ncruces/zenity/internal/zenutil"
|
|
|
|
)
|
|
|
|
|
2022-12-07 09:35:37 -05:00
|
|
|
func Test_quoteAccelerators(t *testing.T) {
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2022-12-07 09:35:37 -05:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
text string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{name: "None", text: "abc", want: "abc"},
|
|
|
|
{name: "One", text: "&abc", want: "&&abc"},
|
|
|
|
{name: "Two", text: "&a&bc", want: "&&a&&bc"},
|
|
|
|
{name: "Three", text: "ab&&c", want: "ab&&&&c"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := quoteAccelerators(tt.text); got != tt.want {
|
|
|
|
t.Errorf("quoteAccelerators(%q) = %q; want %q", tt.text, got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 07:24:24 -04:00
|
|
|
func Test_appendGeneral(t *testing.T) {
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2022-06-02 07:24:24 -04:00
|
|
|
got := appendGeneral(nil, options{
|
2022-12-14 19:26:34 -05:00
|
|
|
title: ptr("Title"),
|
2022-06-02 07:24:24 -04:00
|
|
|
attach: 12345,
|
|
|
|
modal: true,
|
|
|
|
})
|
|
|
|
want := []string{"--title", "Title", "--attach", "12345", "--modal"}
|
2021-07-06 20:54:32 -04:00
|
|
|
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) {
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2021-07-06 20:54:32 -04:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
opts options
|
|
|
|
want []string
|
|
|
|
}{
|
2022-12-14 19:26:34 -05:00
|
|
|
{name: "OK", opts: options{okLabel: ptr("OK")}, want: []string{"--ok-label", "OK"}},
|
|
|
|
{name: "Cancel", opts: options{cancelLabel: ptr("Cancel")}, want: []string{"--cancel-label", "Cancel"}},
|
|
|
|
{name: "Extra", opts: options{extraButton: ptr("Extra")}, want: []string{"--extra-button", "Extra"}},
|
2021-07-06 20:54:32 -04:00
|
|
|
}
|
|
|
|
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) {
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2021-07-06 20:54:32 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:48:09 -04:00
|
|
|
func Test_appendWindowIcon(t *testing.T) {
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2021-07-06 20:54:32 -04:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
opts options
|
|
|
|
want []string
|
|
|
|
}{
|
2022-05-18 09:48:09 -04:00
|
|
|
{name: "NoIcon", opts: options{windowIcon: NoIcon}, want: nil},
|
|
|
|
{name: "Info", opts: options{windowIcon: InfoIcon}, want: []string{"--window-icon=info"}},
|
|
|
|
{name: "Error", opts: options{windowIcon: ErrorIcon}, want: []string{"--window-icon=error"}},
|
|
|
|
{name: "Warning", opts: options{windowIcon: WarningIcon}, want: []string{"--window-icon=warning"}},
|
|
|
|
{name: "Question", opts: options{windowIcon: QuestionIcon}, want: []string{"--window-icon=question"}},
|
|
|
|
{name: "Password", opts: options{windowIcon: PasswordIcon}, want: nil},
|
|
|
|
{name: "File", opts: options{windowIcon: "png"}, want: []string{"--window-icon", "png"}},
|
2021-07-06 20:54:32 -04:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-05-18 09:48:09 -04:00
|
|
|
if got := appendWindowIcon(nil, tt.opts); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("appendWindowIcon() = %v; want %v", got, tt.want)
|
2021-07-06 20:54:32 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_strResult(t *testing.T) {
|
|
|
|
sentinel := errors.New("sentinel")
|
2022-12-07 09:35:37 -05:00
|
|
|
cancel := exit1Cmd().Run()
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2021-07-06 20:54:32 -04:00
|
|
|
|
|
|
|
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) {
|
|
|
|
sentinel := errors.New("sentinel")
|
2022-12-07 09:35:37 -05:00
|
|
|
cancel := exit1Cmd().Run()
|
2022-12-15 09:29:56 -05:00
|
|
|
zenutil.Separator = "|"
|
|
|
|
t.Parallel()
|
2021-07-06 20:54:32 -04:00
|
|
|
|
2021-07-08 13:56:28 -04:00
|
|
|
if out, err := lstResult(options{}, []byte(""), nil); !reflect.DeepEqual(out, []string{}) || err != nil {
|
2022-12-07 09:35:37 -05:00
|
|
|
t.Errorf(`lstResult("", nil) = %v, %v`, out, err)
|
2021-07-08 13:56:28 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2022-12-07 09:35:37 -05:00
|
|
|
|
|
|
|
func Test_pwdResult(t *testing.T) {
|
|
|
|
username := options{username: true}
|
|
|
|
sentinel := errors.New("sentinel")
|
|
|
|
cancel := exit1Cmd().Run()
|
2022-12-15 09:29:56 -05:00
|
|
|
t.Parallel()
|
2022-12-07 09:35:37 -05:00
|
|
|
|
|
|
|
if usr, pwd, err := pwdResult("|", options{}, []byte(""), nil); usr != "" || pwd != "" || err != nil {
|
|
|
|
t.Errorf(`pwdResult("", nil) = %v, %q, %q`, usr, pwd, err)
|
|
|
|
}
|
|
|
|
if usr, pwd, err := pwdResult("|", options{}, []byte("out"), nil); usr != "" || pwd != "out" || err != nil {
|
|
|
|
t.Errorf(`pwdResult("out", nil) = %v, %q, %q`, usr, pwd, err)
|
|
|
|
}
|
|
|
|
if usr, pwd, err := pwdResult("|", username, []byte("one|two"), nil); usr != "one" || pwd != "two" || err != nil {
|
|
|
|
t.Errorf(`pwdResult("one|two", nil) = %v, %q, %q`, usr, pwd, err)
|
|
|
|
}
|
|
|
|
if usr, pwd, err := pwdResult("|", options{}, []byte("one|two"), nil); usr != "" || pwd != "one|two" || err != nil {
|
|
|
|
t.Errorf(`pwdResult("one|two", nil) = %v, %q, %q`, usr, pwd, err)
|
|
|
|
}
|
|
|
|
if usr, pwd, err := pwdResult("|", options{}, []byte("out"), sentinel); usr != "" || pwd != "" || err != sentinel {
|
|
|
|
t.Errorf(`pwdResult("out", error) = %v, %q, %q`, usr, pwd, err)
|
|
|
|
}
|
|
|
|
if usr, pwd, err := pwdResult("|", options{}, []byte("out"), cancel); usr != "" || pwd != "" || err != ErrCanceled {
|
|
|
|
t.Errorf(`pwdResult("out", cancel) = %v, %q, %q`, usr, pwd, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func exit1Cmd() *exec.Cmd {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return exec.Command("cmd", "/k", "exit", "1")
|
|
|
|
}
|
|
|
|
return exec.Command("false")
|
|
|
|
}
|