Improve test coverage.

This commit is contained in:
Nuno Cruces 2021-06-18 16:16:04 +01:00
parent b83ac53cce
commit 93936c933a
9 changed files with 211 additions and 14 deletions

View file

@ -2,6 +2,7 @@
[![PkgGoDev](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/ncruces/zenity) [![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](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)
This repo includes both a cross-platform Go package providing This repo includes both a cross-platform Go package providing
[Zenity](https://help.gnome.org/users/zenity/stable/)-like dialogs [Zenity](https://help.gnome.org/users/zenity/stable/)-like dialogs

View file

@ -270,7 +270,6 @@ func isUniformTypeIdentifier(pattern string) bool {
func splitDirAndName(path string) (dir, name string) { func splitDirAndName(path string) (dir, name string) {
if path != "" { if path != "" {
path = filepath.Clean(path)
fi, err := os.Stat(path) fi, err := os.Stat(path)
if err == nil && fi.IsDir() { if err == nil && fi.IsDir() {
return path, "" return path, ""

View file

@ -5,6 +5,23 @@ import (
"testing" "testing"
) )
func TestFileFilters_name(t *testing.T) {
tests := []struct {
data FileFilters
want string
}{
{FileFilters{{"", []string{`*.png`}}}, "*.png"},
{FileFilters{{"", []string{`*.png`, `*.jpg`}}}, "*.png *.jpg"},
{FileFilters{{"Image files", []string{`*.png`, `*.jpg`}}}, "Image files"},
}
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)
}
}
}
func TestFileFilters_simplify(t *testing.T) { func TestFileFilters_simplify(t *testing.T) {
tests := []struct { tests := []struct {
data FileFilters data FileFilters
@ -54,10 +71,11 @@ func TestFileFilters_types(t *testing.T) {
{FileFilters{{"", []string{`*.[\[]PNG`}}}, []string{"", "[PNG"}}, {FileFilters{{"", []string{`*.[\[]PNG`}}}, []string{"", "[PNG"}},
{FileFilters{{"", []string{`*.[\]]PNG`}}}, []string{"", "]PNG"}}, {FileFilters{{"", []string{`*.[\]]PNG`}}}, []string{"", "]PNG"}},
{FileFilters{{"", []string{`public.png`}}}, []string{"", "public.png"}}, {FileFilters{{"", []string{`public.png`}}}, []string{"", "public.png"}},
{FileFilters{{"", []string{`-public-.png`}}}, []string{"", "png"}},
} }
for i, tt := range tests { for i, tt := range tests {
if got := tt.data.types(); !reflect.DeepEqual(got, tt.want) { if got := tt.data.types(); !reflect.DeepEqual(got, tt.want) {
t.Fatalf("FileFilters.types[%d] = %v, want %v", i, got, tt.want) t.Errorf("FileFilters.types[%d] = %v, want %v", i, got, tt.want)
} }
} }
} }

37
file_util_test.go Normal file
View file

@ -0,0 +1,37 @@
package zenity
import (
"os"
"path/filepath"
"testing"
)
func Test_splitDirAndName(t *testing.T) {
tempDir := os.TempDir()
tests := []struct {
path string
wantDir string
wantName string
}{
// filepath.Split test cases
{"a/b", "a/", "b"},
{"a/b/", "a/b/", ""},
{"a/", "a/", ""},
{"a", "", "a"},
{"/", "/", ""},
// we split differently if we know it's a directory
{tempDir, tempDir, ""},
{filepath.Clean(tempDir), filepath.Clean(tempDir), ""},
{filepath.Join(tempDir, "a"), filepath.Clean(tempDir) + string(filepath.Separator), "a"},
}
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)
}
if gotName != tt.wantName {
t.Errorf("splitDirAndName[%d].name = %q, want %q", i, gotName, tt.wantName)
}
}
}

View file

@ -47,9 +47,12 @@ func ParseColor(s string) color.Color {
} }
} }
c, _ := colornames.Map[strings.ToLower(s)] c, ok := colornames.Map[strings.ToLower(s)]
if ok {
return c return c
} }
return nil
}
// UnparseColor is internal. // UnparseColor is internal.
func UnparseColor(c color.Color) string { func UnparseColor(c color.Color) string {

View file

@ -7,7 +7,7 @@ import (
"golang.org/x/image/colornames" "golang.org/x/image/colornames"
) )
func TestColor(t *testing.T) { func TestColor_names(t *testing.T) {
tests := []string{ tests := []string{
"chocolate", "chocolate",
"lime", "lime",
@ -17,20 +17,73 @@ func TestColor(t *testing.T) {
"salmon", "salmon",
"tomato", "tomato",
} }
eq := func(c1, c2 color.Color) bool { for _, test := range tests {
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 !colorEq(c1, c3) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v, want %v", c1, c3, c1)
}
}
}
func TestColor_colors(t *testing.T) {
tests := []color.Color{
color.Black,
color.White,
color.Opaque,
color.Transparent,
}
for _, test := range tests {
c := ParseColor(UnparseColor(test))
if !colorEq(c, test) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v, want %v", test, c, test)
}
}
}
func TestColor_strings(t *testing.T) {
tests := []struct {
data string
want color.Color
}{
{"#000", color.Black},
{"#000f", color.Black},
{"#000000", color.Black},
{"#000000ff", color.Black},
{"#fff", color.White},
{"#ffff", color.White},
{"#ffffff", color.White},
{"#ffffffff", color.White},
{"#FFF", color.Opaque},
{"#FFFF", color.Opaque},
{"#FFFFFF", color.Opaque},
{"#FFFFFFFF", color.Opaque},
{"#0000", color.Transparent},
{"#00000000", color.Transparent},
{"#8888", color.NRGBA{0x88, 0x88, 0x88, 0x88}},
{"#80808080", color.NRGBA{0x80, 0x80, 0x80, 0x80}},
{"rgb(128,128,128)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"rgba(128,128,128,0.5)", color.NRGBA{0x80, 0x80, 0x80, 0x80}},
{"rgba(128,128,128,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"not a color", nil},
}
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)
}
}
}
func colorEq(c1, c2 color.Color) bool {
if c1 == nil || c2 == nil {
return c1 == c2
}
r1, g1, b1, a1 := c1.RGBA() r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA() r2, g2, b2, a2 := c2.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2 return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
} }
for _, test := range tests {
c1 := colornames.Map[test]
c2 := ParseColor(test)
c3 := ParseColor(UnparseColor(c2))
if !eq(c1, c2) {
t.Errorf("ParseColor(%s) = %v, want %v", test, c2, c1)
}
if !eq(c1, c3) {
t.Errorf("ParseColor(UnparseColor(%s)) = %v, want %v", test, c3, c1)
}
}
}

View file

@ -0,0 +1,53 @@
// +build !windows,!darwin
package zenutil
import (
"context"
"errors"
"os"
"os/exec"
"testing"
)
func TestRun(t *testing.T) {
_, err := Run(nil, []string{"--version"})
if err, skip := skip(err); skip {
t.Skip("skipping:", err)
}
if err != nil {
t.Fatal(err)
}
}
func TestRun_context(t *testing.T) {
_, err := Run(context.TODO(), []string{"--version"})
if err, skip := skip(err); skip {
t.Skip("skipping:", err)
}
if err != nil {
t.Fatal(err)
}
}
func TestRunProgress(t *testing.T) {
_, err := RunProgress(nil, 100, nil, []string{"--version"})
if err, skip := skip(err); skip {
t.Skip("skipping:", err)
}
if err != nil {
t.Fatal(err)
}
}
func skip(err error) (error, bool) {
if _, ok := err.(*exec.Error); ok {
// zenity/osascript/etc were not found in path
return err, true
}
if err != nil && os.Getenv("DISPLAY") == "" {
// no display
return errors.New("no display"), true
}
return nil, false
}

View file

@ -10,6 +10,11 @@ func TestUnescape(t *testing.T) {
{`abc`, "abc"}, {`abc`, "abc"},
{`ab\c`, "abc"}, {`ab\c`, "abc"},
{`a\bc`, "a\bc"}, {`a\bc`, "a\bc"},
{`abc\f`, "abc\f"},
{`abc\n`, "abc\n"},
{`abc\r`, "abc\r"},
{`abc\t`, "abc\t"},
{`abc\v`, "abc\v"},
{`a\1c`, "a\001c"}, {`a\1c`, "a\001c"},
{`a\12c`, "a\012c"}, {`a\12c`, "a\012c"},
{`a\123c`, "a\123c"}, {`a\123c`, "a\123c"},

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"os" "os"
"runtime"
"testing" "testing"
"time" "time"
@ -16,6 +17,13 @@ func ExamplePassword() {
// Output: // Output:
} }
func ExamplePassword_username() {
zenity.Password(
zenity.Title("Type your username and password"),
zenity.Username())
// Output:
}
func TestPassword_timeout(t *testing.T) { func TestPassword_timeout(t *testing.T) {
defer goleak.VerifyNone(t) defer goleak.VerifyNone(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) ctx, cancel := context.WithTimeout(context.Background(), time.Second/10)
@ -43,3 +51,23 @@ func TestPassword_cancel(t *testing.T) {
t.Error("was not canceled:", err) t.Error("was not canceled:", err)
} }
} }
func TestPassword_username(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, _, err := zenity.Password(zenity.Context(ctx), zenity.Username())
if err, skip := skip(err); skip {
t.Skip("skipping:", err)
}
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
if !errors.Is(err, zenity.ErrUnsupported) {
t.Error("was not unsupported:", err)
}
} else {
if !errors.Is(err, context.Canceled) {
t.Error("was not canceled:", err)
}
}
}