diff --git a/README.md b/README.md index ecdf0b9..112fd77 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![PkgGoDev](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/ncruces/zenity) [![Go Report](https://goreportcard.com/badge/github.com/ncruces/zenity)](https://goreportcard.com/report/github.com/ncruces/zenity) -[![Go Report](http://gocover.io/_badge/github.com/ncruces/zenity)](https://gocover.io/github.com/ncruces/zenity) +[![Go Report](https://gocover.io/_badge/github.com/ncruces/zenity)](https://gocover.io/github.com/ncruces/zenity) This repo includes both a cross-platform Go package providing [Zenity](https://help.gnome.org/users/zenity/stable/)-like dialogs diff --git a/util_unix_test.go b/util_unix_test.go new file mode 100644 index 0000000..4d7bc5d --- /dev/null +++ b/util_unix_test.go @@ -0,0 +1,113 @@ +// +build !windows + +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) { + t.Errorf("appendTitle() = %v, want %v", got, want) + } +} + +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) { + t.Errorf("appendButtons() = %v, want %v", got, tt.want) + } + }) + } +} + +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) { + t.Errorf("appendWidthHeight() = %v, want %v", got, tt.want) + } + }) + } +} + +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) { + t.Errorf("appendIcon() = %v, want %v", got, tt.want) + } + }) + } +} + +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 { + 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) + } + if out, err := strResult(options{}, []byte("out"), cancel); out != "" || err != ErrCanceled { + t.Errorf("strResult(out, nil) = %q, %v", out, err) + } +} + +func Test_lstResult(t *testing.T) { + zenutil.Separator = "|" + sentinel := errors.New("sentinel") + 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) + } + 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) + } + if out, err := lstResult(options{}, []byte("out"), sentinel); out != nil || err != sentinel { + t.Errorf("lstResult(out, nil) = %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) + } +} diff --git a/zenity_test.go b/zenity_test.go new file mode 100644 index 0000000..56a11f3 --- /dev/null +++ b/zenity_test.go @@ -0,0 +1,68 @@ +package zenity + +import ( + "context" + "image/color" + "reflect" + "testing" +) + +func Test_applyOptions(t *testing.T) { + tests := []struct { + name string + args Option + want options + }{ + // General options + {name: "Title", args: Title("Title"), want: options{title: stringPtr("Title")}}, + {name: "Width", args: Width(100), want: options{width: 100}}, + {name: "Height", args: Height(100), want: options{height: 100}}, + {name: "OKLabel", args: OKLabel("OK"), want: options{okLabel: stringPtr("OK")}}, + {name: "CancelLabel", args: CancelLabel("Cancel"), want: options{cancelLabel: stringPtr("Cancel")}}, + {name: "ExtraButton", args: ExtraButton("Extra"), want: options{extraButton: stringPtr("Extra")}}, + {name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}}, + {name: "DefaultCancel", args: DefaultCancel(), want: options{defaultCancel: true}}, + + // Message options + {name: "NoWrap", args: NoWrap(), want: options{noWrap: true}}, + {name: "Ellipsize", args: Ellipsize(), want: options{ellipsize: true}}, + + // Entry options + {name: "EntryText", args: EntryText("text"), want: options{entryText: "text"}}, + {name: "HideText", args: HideText(), want: options{hideText: true}}, + {name: "Username", args: Username(), want: options{username: true}}, + + // List options + {name: "DisallowEmpty", args: DisallowEmpty(), want: options{disallowEmpty: true}}, + {name: "DefaultItems", args: DefaultItems("a", "b"), want: options{defaultItems: []string{"a", "b"}}}, + + // File selection options + {name: "Directory", args: Directory(), want: options{directory: true}}, + {name: "ConfirmOverwrite", args: ConfirmOverwrite(), want: options{confirmOverwrite: true}}, + {name: "ConfirmCreate", args: ConfirmCreate(), want: options{confirmCreate: true}}, + {name: "ShowHidden", args: ShowHidden(), want: options{showHidden: true}}, + {name: "Filename", args: Filename("file.go"), want: options{filename: "file.go"}}, + {name: "FileFilters", args: FileFilter{"Go files", []string{"*.go"}}, want: options{ + fileFilters: FileFilters{{"Go files", []string{"*.go"}}}, + }}, + + // Progress indication options + {name: "MaxValue", args: MaxValue(100), want: options{maxValue: 100}}, + {name: "NoCancel", args: NoCancel(), want: options{noCancel: true}}, + {name: "TimeRemaining", args: TimeRemaining(), want: options{timeRemaining: true}}, + + // Color selection options + {name: "Color", args: Color(color.Black), want: options{color: color.Black}}, + {name: "ShowPalette", args: ShowPalette(), want: options{showPalette: true}}, + + // Context for timeout + {name: "Context", args: Context(context.TODO()), want: options{ctx: context.TODO()}}, + } + 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) + } + }) + } +}