github.com/puellanivis/breton@v0.2.16/lib/gnuflag/slice_test.go (about)

     1  package gnuflag
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestStructVar_StringSlices(t *testing.T) {
     9  	var Flags struct {
    10  		A   []string `flag:",def=a"`
    11  		AB  []string `flag:",def=a,b"`
    12  		ABC []string `flag:",def=a,b,c"`
    13  		S   []string
    14  	}
    15  
    16  	Flags.S = []string{"x", "y"}
    17  
    18  	var fs FlagSet
    19  
    20  	if err := fs.structVar("", reflect.ValueOf(&Flags).Elem()); err != nil {
    21  		t.Fatal("unexpected error running structVar:", err)
    22  	}
    23  
    24  	if len(fs.formal)+len(fs.short) == 0 {
    25  		t.Fatal("no flags set on FlagSet")
    26  	}
    27  
    28  	checkedKeys := make(map[string]bool)
    29  	checkFlag := func(name, value, usage string, val, expect interface{}) {
    30  		checkedKeys[name] = true
    31  
    32  		f, ok := fs.formal[name]
    33  		if !ok {
    34  			t.Errorf("expected flag %q to exist", name)
    35  			return
    36  		}
    37  		if f.DefValue != value {
    38  			t.Errorf("flag %q has default value %q, but epected %q", name, f.Value, value)
    39  		}
    40  		if f.Usage != usage {
    41  			t.Errorf("flag %q has usage %q, but expected %q", name, f.Usage, usage)
    42  		}
    43  		if !reflect.DeepEqual(val, expect) {
    44  			t.Errorf("flag %q is %#v, but expected %#v", name, val, expect)
    45  		}
    46  	}
    47  
    48  	checkFlag("a", "a", "A `[]string`", Flags.A, []string{"a"})
    49  	checkFlag("ab", "a,b", "AB `[]string`", Flags.AB, []string{"a", "b"})
    50  	checkFlag("abc", "a,b,c", "ABC `[]string`", Flags.ABC, []string{"a", "b", "c"})
    51  	checkFlag("s", "x,y", "S `[]string`", Flags.S, []string{"x", "y"})
    52  
    53  	for k := range fs.formal {
    54  		if !checkedKeys[k] {
    55  			t.Errorf("unexpected key found: %q", k)
    56  		}
    57  	}
    58  
    59  	if err := fs.Set("a", "d"); err != nil {
    60  		t.Fatal("unexpected error setting flag:", err)
    61  	}
    62  
    63  	if err := fs.Set("ab", "d"); err != nil {
    64  		t.Fatal("unexpected error setting flag:", err)
    65  	}
    66  
    67  	if err := fs.Set("abc", "d"); err != nil {
    68  		t.Fatal("unexpected error setting flag:", err)
    69  	}
    70  
    71  	checkFlag("a", "a", "A `[]string`", Flags.A, []string{"a", "d"})
    72  	checkFlag("ab", "a,b", "AB `[]string`", Flags.AB, []string{"a", "b", "d"})
    73  	checkFlag("abc", "a,b,c", "ABC `[]string`", Flags.ABC, []string{"a", "b", "c", "d"})
    74  }
    75  
    76  func TestStructVar_IntSlices(t *testing.T) {
    77  	var Flags struct {
    78  		A   []int `flag:",def=2"`
    79  		AB  []int `flag:",def=2,3"`
    80  		ABC []int `flag:",def=2,3,5"`
    81  		S   []int
    82  	}
    83  
    84  	Flags.S = []int{42, 13}
    85  
    86  	var fs FlagSet
    87  
    88  	if err := fs.structVar("", reflect.ValueOf(&Flags).Elem()); err != nil {
    89  		t.Fatal("unexpected error running structVar:", err)
    90  	}
    91  
    92  	if len(fs.formal)+len(fs.short) == 0 {
    93  		t.Fatal("no flags set on FlagSet")
    94  	}
    95  
    96  	checkedKeys := make(map[string]bool)
    97  	checkFlag := func(name, value, usage string, val, expect interface{}) {
    98  		checkedKeys[name] = true
    99  
   100  		f, ok := fs.formal[name]
   101  		if !ok {
   102  			t.Errorf("expected flag %q to exist", name)
   103  			return
   104  		}
   105  		if f.DefValue != value {
   106  			t.Errorf("flag %q has default value %q, but epected %q", name, f.Value, value)
   107  		}
   108  		if f.Usage != usage {
   109  			t.Errorf("flag %q has usage %q, but expected %q", name, f.Usage, usage)
   110  		}
   111  		if !reflect.DeepEqual(val, expect) {
   112  			t.Errorf("flag %q is %#v, but expected %#v", name, val, expect)
   113  		}
   114  	}
   115  
   116  	checkFlag("a", "2", "A `[]int`", Flags.A, []int{2})
   117  	checkFlag("ab", "2,3", "AB `[]int`", Flags.AB, []int{2, 3})
   118  	checkFlag("abc", "2,3,5", "ABC `[]int`", Flags.ABC, []int{2, 3, 5})
   119  	checkFlag("s", "42,13", "S `[]int`", Flags.S, []int{42, 13})
   120  
   121  	for k := range fs.formal {
   122  		if !checkedKeys[k] {
   123  			t.Errorf("unexpected key found: %q", k)
   124  		}
   125  	}
   126  
   127  	if err := fs.Set("a", "7"); err != nil {
   128  		t.Fatal("unexpected error setting flag:", err)
   129  	}
   130  
   131  	if err := fs.Set("ab", "7"); err != nil {
   132  		t.Fatal("unexpected error setting flag:", err)
   133  	}
   134  
   135  	if err := fs.Set("abc", "7"); err != nil {
   136  		t.Fatal("unexpected error setting flag:", err)
   137  	}
   138  
   139  	checkFlag("a", "2", "A `[]int`", Flags.A, []int{2, 7})
   140  	checkFlag("ab", "2,3", "AB `[]int`", Flags.AB, []int{2, 3, 7})
   141  	checkFlag("abc", "2,3,5", "ABC `[]int`", Flags.ABC, []int{2, 3, 5, 7})
   142  }
   143  
   144  func TestStructVar_Int64Slices(t *testing.T) {
   145  	var Flags struct {
   146  		A   []int64 `flag:",def=2"`
   147  		AB  []int64 `flag:",def=2,3"`
   148  		ABC []int64 `flag:",def=2,3,5"`
   149  		S   []int64
   150  	}
   151  
   152  	Flags.S = []int64{42, 13}
   153  
   154  	var fs FlagSet
   155  
   156  	if err := fs.structVar("", reflect.ValueOf(&Flags).Elem()); err != nil {
   157  		t.Fatal("unexpected error running structVar:", err)
   158  	}
   159  
   160  	if len(fs.formal)+len(fs.short) == 0 {
   161  		t.Fatal("no flags set on FlagSet")
   162  	}
   163  
   164  	checkedKeys := make(map[string]bool)
   165  	checkFlag := func(name, value, usage string, val, expect interface{}) {
   166  		checkedKeys[name] = true
   167  
   168  		f, ok := fs.formal[name]
   169  		if !ok {
   170  			t.Errorf("expected flag %q to exist", name)
   171  			return
   172  		}
   173  		if f.DefValue != value {
   174  			t.Errorf("flag %q has default value %q, but epected %q", name, f.Value, value)
   175  		}
   176  		if f.Usage != usage {
   177  			t.Errorf("flag %q has usage %q, but expected %q", name, f.Usage, usage)
   178  		}
   179  		if !reflect.DeepEqual(val, expect) {
   180  			t.Errorf("flag %q is %#v, but expected %#v", name, val, expect)
   181  		}
   182  	}
   183  
   184  	checkFlag("a", "2", "A `[]int64`", Flags.A, []int64{2})
   185  	checkFlag("ab", "2,3", "AB `[]int64`", Flags.AB, []int64{2, 3})
   186  	checkFlag("abc", "2,3,5", "ABC `[]int64`", Flags.ABC, []int64{2, 3, 5})
   187  	checkFlag("s", "42,13", "S `[]int64`", Flags.S, []int64{42, 13})
   188  
   189  	for k := range fs.formal {
   190  		if !checkedKeys[k] {
   191  			t.Errorf("unexpected key found: %q", k)
   192  		}
   193  	}
   194  
   195  	if err := fs.Set("a", "7"); err != nil {
   196  		t.Fatal("unexpected error setting flag:", err)
   197  	}
   198  
   199  	if err := fs.Set("ab", "7"); err != nil {
   200  		t.Fatal("unexpected error setting flag:", err)
   201  	}
   202  
   203  	if err := fs.Set("abc", "7"); err != nil {
   204  		t.Fatal("unexpected error setting flag:", err)
   205  	}
   206  
   207  	checkFlag("a", "2", "A `[]int64`", Flags.A, []int64{2, 7})
   208  	checkFlag("ab", "2,3", "AB `[]int64`", Flags.AB, []int64{2, 3, 7})
   209  	checkFlag("abc", "2,3,5", "ABC `[]int64`", Flags.ABC, []int64{2, 3, 5, 7})
   210  }
   211  
   212  func TestStructVar_UintSlices(t *testing.T) {
   213  	var Flags struct {
   214  		A   []uint `flag:",def=2"`
   215  		AB  []uint `flag:",def=2,3"`
   216  		ABC []uint `flag:",def=2,3,5"`
   217  		S   []uint
   218  	}
   219  
   220  	Flags.S = []uint{42, 13}
   221  
   222  	var fs FlagSet
   223  
   224  	if err := fs.structVar("", reflect.ValueOf(&Flags).Elem()); err != nil {
   225  		t.Fatal("unexpected error running structVar:", err)
   226  	}
   227  
   228  	if len(fs.formal)+len(fs.short) == 0 {
   229  		t.Fatal("no flags set on FlagSet")
   230  	}
   231  
   232  	checkedKeys := make(map[string]bool)
   233  	checkFlag := func(name, value, usage string, val, expect interface{}) {
   234  		checkedKeys[name] = true
   235  
   236  		f, ok := fs.formal[name]
   237  		if !ok {
   238  			t.Errorf("expected flag %q to exist", name)
   239  			return
   240  		}
   241  		if f.DefValue != value {
   242  			t.Errorf("flag %q has default value %q, but epected %q", name, f.Value, value)
   243  		}
   244  		if f.Usage != usage {
   245  			t.Errorf("flag %q has usage %q, but expected %q", name, f.Usage, usage)
   246  		}
   247  		if !reflect.DeepEqual(val, expect) {
   248  			t.Errorf("flag %q is %#v, but expected %#v", name, val, expect)
   249  		}
   250  	}
   251  
   252  	checkFlag("a", "2", "A `[]uint`", Flags.A, []uint{2})
   253  	checkFlag("ab", "2,3", "AB `[]uint`", Flags.AB, []uint{2, 3})
   254  	checkFlag("abc", "2,3,5", "ABC `[]uint`", Flags.ABC, []uint{2, 3, 5})
   255  	checkFlag("s", "42,13", "S `[]uint`", Flags.S, []uint{42, 13})
   256  
   257  	for k := range fs.formal {
   258  		if !checkedKeys[k] {
   259  			t.Errorf("unexpected key found: %q", k)
   260  		}
   261  	}
   262  
   263  	if err := fs.Set("a", "7"); err != nil {
   264  		t.Fatal("unexpected error setting flag:", err)
   265  	}
   266  
   267  	if err := fs.Set("ab", "7"); err != nil {
   268  		t.Fatal("unexpected error setting flag:", err)
   269  	}
   270  
   271  	if err := fs.Set("abc", "7"); err != nil {
   272  		t.Fatal("unexpected error setting flag:", err)
   273  	}
   274  
   275  	checkFlag("a", "2", "A `[]uint`", Flags.A, []uint{2, 7})
   276  	checkFlag("ab", "2,3", "AB `[]uint`", Flags.AB, []uint{2, 3, 7})
   277  	checkFlag("abc", "2,3,5", "ABC `[]uint`", Flags.ABC, []uint{2, 3, 5, 7})
   278  }
   279  
   280  func TestStructVar_Uint64Slices(t *testing.T) {
   281  	var Flags struct {
   282  		A   []uint64 `flag:",def=2"`
   283  		AB  []uint64 `flag:",def=2,3"`
   284  		ABC []uint64 `flag:",def=2,3,5"`
   285  		S   []uint64
   286  	}
   287  
   288  	Flags.S = []uint64{42, 13}
   289  
   290  	var fs FlagSet
   291  
   292  	if err := fs.structVar("", reflect.ValueOf(&Flags).Elem()); err != nil {
   293  		t.Fatal("unexpected error running structVar:", err)
   294  	}
   295  
   296  	if len(fs.formal)+len(fs.short) == 0 {
   297  		t.Fatal("no flags set on FlagSet")
   298  	}
   299  
   300  	checkedKeys := make(map[string]bool)
   301  	checkFlag := func(name, value, usage string, val, expect interface{}) {
   302  		checkedKeys[name] = true
   303  
   304  		f, ok := fs.formal[name]
   305  		if !ok {
   306  			t.Errorf("expected flag %q to exist", name)
   307  			return
   308  		}
   309  		if f.DefValue != value {
   310  			t.Errorf("flag %q has default value %q, but epected %q", name, f.Value, value)
   311  		}
   312  		if f.Usage != usage {
   313  			t.Errorf("flag %q has usage %q, but expected %q", name, f.Usage, usage)
   314  		}
   315  		if !reflect.DeepEqual(val, expect) {
   316  			t.Errorf("flag %q is %#v, but expected %#v", name, val, expect)
   317  		}
   318  	}
   319  
   320  	checkFlag("a", "2", "A `[]uint64`", Flags.A, []uint64{2})
   321  	checkFlag("ab", "2,3", "AB `[]uint64`", Flags.AB, []uint64{2, 3})
   322  	checkFlag("abc", "2,3,5", "ABC `[]uint64`", Flags.ABC, []uint64{2, 3, 5})
   323  	checkFlag("s", "42,13", "S `[]uint64`", Flags.S, []uint64{42, 13})
   324  
   325  	for k := range fs.formal {
   326  		if !checkedKeys[k] {
   327  			t.Errorf("unexpected key found: %q", k)
   328  		}
   329  	}
   330  
   331  	if err := fs.Set("a", "7"); err != nil {
   332  		t.Fatal("unexpected error setting flag:", err)
   333  	}
   334  
   335  	if err := fs.Set("ab", "7"); err != nil {
   336  		t.Fatal("unexpected error setting flag:", err)
   337  	}
   338  
   339  	if err := fs.Set("abc", "7"); err != nil {
   340  		t.Fatal("unexpected error setting flag:", err)
   341  	}
   342  
   343  	checkFlag("a", "2", "A `[]uint64`", Flags.A, []uint64{2, 7})
   344  	checkFlag("ab", "2,3", "AB `[]uint64`", Flags.AB, []uint64{2, 3, 7})
   345  	checkFlag("abc", "2,3,5", "ABC `[]uint64`", Flags.ABC, []uint64{2, 3, 5, 7})
   346  }