zenity/file_test.go

107 lines
2.3 KiB
Go
Raw Normal View History

2020-01-10 07:00:38 -05:00
package zenity_test
2019-12-11 05:29:31 -05:00
2020-01-29 06:09:06 -05:00
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/ncruces/zenity"
2021-04-30 14:19:14 -04:00
"go.uber.org/goleak"
2020-01-29 06:09:06 -05:00
)
2019-12-11 05:29:31 -05:00
2021-04-13 08:28:26 -04:00
const defaultPath = ``
const defaultName = ``
2020-01-10 07:00:38 -05:00
func ExampleSelectFile() {
zenity.SelectFile(
zenity.Filename(defaultPath),
zenity.FileFilters{
{"Go files", []string{"*.go"}},
{"Web files", []string{"*.html", "*.js", "*.css"}},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
2020-01-24 07:52:45 -05:00
})
2020-01-10 07:00:38 -05:00
// Output:
2019-12-11 05:29:31 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleSelectFileMutiple() {
zenity.SelectFileMutiple(
zenity.Filename(defaultPath),
zenity.FileFilters{
{"Go files", []string{"*.go"}},
{"Web files", []string{"*.html", "*.js", "*.css"}},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
2020-01-24 07:52:45 -05:00
})
2020-01-10 07:00:38 -05:00
// Output:
2019-12-11 05:29:31 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleSelectFileSave() {
zenity.SelectFileSave(
zenity.ConfirmOverwrite(),
2020-01-10 07:00:38 -05:00
zenity.Filename(defaultName),
zenity.FileFilters{
{"Go files", []string{"*.go"}},
{"Web files", []string{"*.html", "*.js", "*.css"}},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
2020-01-24 07:52:45 -05:00
})
2020-01-10 07:00:38 -05:00
// Output:
2019-12-11 05:29:31 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleSelectFile_directory() {
zenity.SelectFile(
zenity.Filename(defaultPath),
zenity.Directory())
2020-01-10 07:00:38 -05:00
// Output:
2020-01-09 20:46:53 -05:00
}
2020-01-10 07:00:38 -05:00
func ExampleSelectFileMutiple_directory() {
zenity.SelectFileMutiple(
zenity.Filename(defaultPath),
zenity.Directory())
2020-01-10 07:00:38 -05:00
// Output:
2019-12-11 05:29:31 -05:00
}
2020-01-29 06:09:06 -05:00
var fileFuncs = []func(...zenity.Option) (string, error){
zenity.SelectFile,
zenity.SelectFileSave,
func(o ...zenity.Option) (string, error) {
return zenity.SelectFile(append(o, zenity.Directory())...)
},
func(o ...zenity.Option) (string, error) {
_, err := zenity.SelectFileMutiple(append(o, zenity.Directory())...)
return "", err
},
func(o ...zenity.Option) (string, error) {
_, err := zenity.SelectFileMutiple(o...)
return "", err
},
}
2021-04-30 14:19:14 -04:00
func TestFile_timeout(t *testing.T) {
2020-01-29 06:09:06 -05:00
for _, f := range fileFuncs {
ctx, cancel := context.WithTimeout(context.Background(), time.Second/10)
_, err := f(zenity.Context(ctx))
if !os.IsTimeout(err) {
2020-01-29 09:15:21 -05:00
t.Error("did not timeout:", err)
2020-01-29 06:09:06 -05:00
}
cancel()
2021-04-30 14:19:14 -04:00
goleak.VerifyNone(t)
2020-01-29 06:09:06 -05:00
}
}
2021-04-30 14:19:14 -04:00
func TestFile_cancel(t *testing.T) {
defer goleak.VerifyNone(t)
2020-01-29 06:09:06 -05:00
ctx, cancel := context.WithCancel(context.Background())
cancel()
for _, f := range fileFuncs {
_, err := f(zenity.Context(ctx))
if !errors.Is(err, context.Canceled) {
2020-01-29 09:15:21 -05:00
t.Error("was not canceled:", err)
2020-01-29 06:09:06 -05:00
}
}
}