github.com/searKing/golang/go@v1.2.117/flag/flag_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package flag_test
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"reflect"
    11  	"sort"
    12  	"testing"
    13  
    14  	flag_ "github.com/searKing/golang/go/flag"
    15  	reflect_ "github.com/searKing/golang/go/reflect"
    16  )
    17  
    18  func TestEverything(t *testing.T) {
    19  	ResetForTesting(nil)
    20  	flag_.StringSlice("test_[]string", nil, "[]string value")
    21  
    22  	m := make(map[string]*flag.Flag)
    23  	desired := "0"
    24  	desiredStringSlice := ""
    25  	visitor := func(f *flag.Flag) {
    26  		if len(f.Name) > 5 && f.Name[0:5] == "test_" {
    27  			m[f.Name] = f
    28  			ok := false
    29  			switch {
    30  			case f.Value.String() == desired:
    31  				ok = true
    32  			case f.Name == "test_[]string" && f.Value.String() == fmt.Sprintf("[%s]", desiredStringSlice):
    33  				ok = true
    34  			}
    35  			if !ok {
    36  				t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
    37  			}
    38  		}
    39  	}
    40  	flag.VisitAll(visitor)
    41  	if len(m) != 1 {
    42  		t.Error("VisitAll misses some flags")
    43  		for k, v := range m {
    44  			t.Log(k, *v)
    45  		}
    46  	}
    47  	m = make(map[string]*flag.Flag)
    48  	flag.Visit(visitor)
    49  	if len(m) != 0 {
    50  		t.Errorf("Visit sees unset flags")
    51  		for k, v := range m {
    52  			t.Log(k, *v)
    53  		}
    54  	}
    55  	// Now set all flags
    56  	flag.Set("test_[]string", "one")
    57  	flag.Set("test_[]string", "two")
    58  	desiredStringSlice = `"one" "two"`
    59  	flag.Visit(visitor)
    60  	if len(m) != 1 {
    61  		t.Error("Visit fails after set")
    62  		for k, v := range m {
    63  			t.Log(k, *v)
    64  		}
    65  	}
    66  	// Now test they're visited in sort order.
    67  	var flagNames []string
    68  	flag.Visit(func(f *flag.Flag) { flagNames = append(flagNames, f.Name) })
    69  	if !sort.StringsAreSorted(flagNames) {
    70  		t.Errorf("flag names not sorted: %v", flagNames)
    71  	}
    72  }
    73  
    74  func TestGet(t *testing.T) {
    75  	ResetForTesting(nil)
    76  	flag_.StringSlice("test_[]string", []string{"one", "two"}, "[]string value")
    77  
    78  	visitor := func(f *flag.Flag) {
    79  		if len(f.Name) > 5 && f.Name[0:5] == "test_" {
    80  			g, ok := f.Value.(flag.Getter)
    81  			if !ok {
    82  				t.Errorf("Visit: value does not satisfy Getter: %T", f.Value)
    83  				return
    84  			}
    85  			switch f.Name {
    86  			case "test_[]string":
    87  				ok = reflect.DeepEqual(g.Get(), []string{"one", "two"})
    88  			}
    89  			if !ok {
    90  				t.Errorf("Visit: bad value %T(%v) for %s", g.Get(), g.Get(), f.Name)
    91  			}
    92  		}
    93  	}
    94  	flag.VisitAll(visitor)
    95  }
    96  
    97  func testParse(f *flag.FlagSet, t *testing.T) {
    98  	if f.Parsed() {
    99  		t.Error("f.Parse() = true before Parse")
   100  	}
   101  	stringSliceFlag := flag_.StringSliceWithFlagSet(f, "test_[]string", []string{"one", "two"}, "[]string value")
   102  	stringSlice2Flag := flag_.StringSliceWithFlagSet(f, "test_[]string2", nil, "[]string value")
   103  	stringSlice3Flag := flag_.StringSliceWithFlagSet(f, "test_[]string3", nil, "[]string value")
   104  	extra := "one-extra-argument"
   105  	args := []string{
   106  		"-test_[]string", "1",
   107  		"-test_[]string", "2",
   108  		"-test_[]string", "3",
   109  		"--test_[]string2", "one",
   110  		extra,
   111  	}
   112  	if err := f.Parse(args); err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	if !f.Parsed() {
   116  		t.Error("f.Parse() = false after Parse")
   117  	}
   118  	if !reflect.DeepEqual(*stringSliceFlag, []string{"1", "2", "3"}) {
   119  		t.Error("[]string flag should be [1 2 3], is ", *stringSliceFlag)
   120  	}
   121  	if !reflect.DeepEqual(*stringSlice2Flag, []string{"one"}) {
   122  		t.Error("[]string flag should be [one], is ", *stringSlice2Flag)
   123  	}
   124  	if reflect_.IsNil(stringSlice3Flag) {
   125  		t.Error("[]string flag should be [], is ", *stringSlice3Flag)
   126  	}
   127  	if len(f.Args()) != 1 {
   128  		t.Error("expected one argument, got", len(f.Args()))
   129  	} else if f.Args()[0] != extra {
   130  		t.Errorf("expected argument %q got %q", extra, f.Args()[0])
   131  	}
   132  }
   133  
   134  func TestParse(t *testing.T) {
   135  	ResetForTesting(func() { t.Error("bad parse") })
   136  	testParse(flag.CommandLine, t)
   137  }
   138  
   139  func TestFlagSetParse(t *testing.T) {
   140  	testParse(flag.NewFlagSet("test", flag.ContinueOnError), t)
   141  }