aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/flag/flag_test.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2020-12-23 09:57:37 -0800
committerIan Lance Taylor <iant@golang.org>2020-12-30 15:13:24 -0800
commitcfcbb4227fb20191e04eb8d7766ae6202f526afd (patch)
treee2effea96f6f204451779f044415c2385e45042b /libgo/go/flag/flag_test.go
parent0696141107d61483f38482b941549959a0d7f613 (diff)
downloadgcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.zip
gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.gz
gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.bz2
libgo: update to Go1.16beta1 release
This does not yet include support for the //go:embed directive added in this release. * Makefile.am (check-runtime): Don't create check-runtime-dir. (mostlyclean-local): Don't remove check-runtime-dir. (check-go-tool, check-vet): Copy in go.mod and modules.txt. (check-cgo-test, check-carchive-test): Add go.mod file. * Makefile.in: Regenerate. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/280172
Diffstat (limited to 'libgo/go/flag/flag_test.go')
-rw-r--r--libgo/go/flag/flag_test.go55
1 files changed, 50 insertions, 5 deletions
diff --git a/libgo/go/flag/flag_test.go b/libgo/go/flag/flag_test.go
index a01a5e4..06cab79 100644
--- a/libgo/go/flag/flag_test.go
+++ b/libgo/go/flag/flag_test.go
@@ -10,7 +10,6 @@ import (
"fmt"
"internal/testenv"
"io"
- "io/ioutil"
"os"
"os/exec"
"runtime"
@@ -38,6 +37,7 @@ func TestEverything(t *testing.T) {
String("test_string", "0", "string value")
Float64("test_float64", 0, "float64 value")
Duration("test_duration", 0, "time.Duration value")
+ Func("test_func", "func value", func(string) error { return nil })
m := make(map[string]*Flag)
desired := "0"
@@ -52,6 +52,8 @@ func TestEverything(t *testing.T) {
ok = true
case f.Name == "test_duration" && f.Value.String() == desired+"s":
ok = true
+ case f.Name == "test_func" && f.Value.String() == "":
+ ok = true
}
if !ok {
t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
@@ -59,7 +61,7 @@ func TestEverything(t *testing.T) {
}
}
VisitAll(visitor)
- if len(m) != 8 {
+ if len(m) != 9 {
t.Error("VisitAll misses some flags")
for k, v := range m {
t.Log(k, *v)
@@ -82,9 +84,10 @@ func TestEverything(t *testing.T) {
Set("test_string", "1")
Set("test_float64", "1")
Set("test_duration", "1s")
+ Set("test_func", "1")
desired = "1"
Visit(visitor)
- if len(m) != 8 {
+ if len(m) != 9 {
t.Error("Visit fails after set")
for k, v := range m {
t.Log(k, *v)
@@ -257,6 +260,48 @@ func TestUserDefined(t *testing.T) {
}
}
+func TestUserDefinedFunc(t *testing.T) {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ var ss []string
+ flags.Func("v", "usage", func(s string) error {
+ ss = append(ss, s)
+ return nil
+ })
+ if err := flags.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err != nil {
+ t.Error(err)
+ }
+ if len(ss) != 3 {
+ t.Fatal("expected 3 args; got ", len(ss))
+ }
+ expect := "[1 2 3]"
+ if got := fmt.Sprint(ss); got != expect {
+ t.Errorf("expected value %q got %q", expect, got)
+ }
+ // test usage
+ var buf strings.Builder
+ flags.SetOutput(&buf)
+ flags.Parse([]string{"-h"})
+ if usage := buf.String(); !strings.Contains(usage, "usage") {
+ t.Errorf("usage string not included: %q", usage)
+ }
+ // test Func error
+ flags = *NewFlagSet("test", ContinueOnError)
+ flags.Func("v", "usage", func(s string) error {
+ return fmt.Errorf("test error")
+ })
+ // flag not set, so no error
+ if err := flags.Parse(nil); err != nil {
+ t.Error(err)
+ }
+ // flag set, expect error
+ if err := flags.Parse([]string{"-v", "1"}); err == nil {
+ t.Error("expected error; got none")
+ } else if errMsg := err.Error(); !strings.Contains(errMsg, "test error") {
+ t.Errorf(`error should contain "test error"; got %q`, errMsg)
+ }
+}
+
func TestUserDefinedForCommandLine(t *testing.T) {
const help = "HELP"
var result string
@@ -499,7 +544,7 @@ func TestGetters(t *testing.T) {
func TestParseError(t *testing.T) {
for _, typ := range []string{"bool", "int", "int64", "uint", "uint64", "float64", "duration"} {
fs := NewFlagSet("parse error test", ContinueOnError)
- fs.SetOutput(ioutil.Discard)
+ fs.SetOutput(io.Discard)
_ = fs.Bool("bool", false, "")
_ = fs.Int("int", 0, "")
_ = fs.Int64("int64", 0, "")
@@ -530,7 +575,7 @@ func TestRangeError(t *testing.T) {
}
for _, arg := range bad {
fs := NewFlagSet("parse error test", ContinueOnError)
- fs.SetOutput(ioutil.Discard)
+ fs.SetOutput(io.Discard)
_ = fs.Int("int", 0, "")
_ = fs.Int64("int64", 0, "")
_ = fs.Uint("uint", 0, "")