vitess.io/vitess@v0.16.2/go/flagutil/flagutil_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreedto in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flagutil
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/spf13/pflag"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestStringList(t *testing.T) {
    30  	p := StringListValue([]string{})
    31  	var _ pflag.Value = &p
    32  	wanted := map[string]string{
    33  		"0ala,ma,kota":   "0ala.ma.kota",
    34  		`1ala\,ma,kota`:  "1ala,ma.kota",
    35  		`2ala\\,ma,kota`: `2ala\.ma.kota`,
    36  		"3ala,":          "3ala.",
    37  	}
    38  	for in, out := range wanted {
    39  		if err := p.Set(in); err != nil {
    40  			t.Errorf("v.Set(%v): %v", in, err)
    41  			continue
    42  		}
    43  		if strings.Join(p, ".") != out {
    44  			t.Errorf("want %#v, got %#v", strings.Split(out, "."), p)
    45  		}
    46  		if p.String() != in {
    47  			t.Errorf("v.String(): want %#v, got %#v", in, p.String())
    48  		}
    49  	}
    50  }
    51  
    52  // TestEmptyStringList verifies that an empty parameter results in an empty list
    53  func TestEmptyStringList(t *testing.T) {
    54  	var p StringListValue
    55  	var _ pflag.Value = &p
    56  	if err := p.Set(""); err != nil {
    57  		t.Fatalf("p.Set(\"\"): %v", err)
    58  	}
    59  	if len(p) != 0 {
    60  		t.Fatalf("len(p) != 0: got %v", len(p))
    61  	}
    62  }
    63  
    64  type pair struct {
    65  	in  string
    66  	out map[string]string
    67  	err error
    68  }
    69  
    70  func TestStringMap(t *testing.T) {
    71  	v := StringMapValue(nil)
    72  	var _ pflag.Value = &v
    73  	wanted := []pair{
    74  		{
    75  			in:  "tag1:value1,tag2:value2",
    76  			out: map[string]string{"tag1": "value1", "tag2": "value2"},
    77  		},
    78  		{
    79  			in:  `tag1:1:value1\,,tag2:value2`,
    80  			out: map[string]string{"tag1": "1:value1,", "tag2": "value2"},
    81  		},
    82  		{
    83  			in:  `tag1:1:value1\,,tag2`,
    84  			err: errInvalidKeyValuePair,
    85  		},
    86  	}
    87  	for _, want := range wanted {
    88  		if err := v.Set(want.in); err != want.err {
    89  			t.Errorf("v.Set(%v): %v", want.in, want.err)
    90  			continue
    91  		}
    92  		if want.err != nil {
    93  			continue
    94  		}
    95  
    96  		if len(want.out) != len(v) {
    97  			t.Errorf("want %#v, got %#v", want.out, v)
    98  			continue
    99  		}
   100  		for key, value := range want.out {
   101  			if v[key] != value {
   102  				t.Errorf("want %#v, got %#v", want.out, v)
   103  				continue
   104  			}
   105  		}
   106  
   107  		if vs := v.String(); vs != want.in {
   108  			t.Errorf("v.String(): want %#v, got %#v", want.in, vs)
   109  		}
   110  	}
   111  }
   112  
   113  func TestDurationOrIntVar(t *testing.T) {
   114  	getflag := func() *DurationOrIntVar { return NewDurationOrIntVar("test-flag", time.Minute, time.Second) }
   115  
   116  	tests := []struct {
   117  		name    string
   118  		arg     string
   119  		want    time.Duration
   120  		wantErr bool
   121  	}{
   122  		{
   123  			name: "duration format",
   124  			arg:  "1h",
   125  			want: time.Hour,
   126  		},
   127  		{
   128  			name: "legacy format",
   129  			arg:  "10",
   130  			want: 10 * time.Second,
   131  		},
   132  		{
   133  			name:    "invalid",
   134  			arg:     "this is not a duration or an int",
   135  			want:    0,
   136  			wantErr: true,
   137  		},
   138  		{
   139  			name: "default value",
   140  			arg:  "",
   141  			want: time.Minute,
   142  		},
   143  	}
   144  
   145  	for _, tt := range tests {
   146  		flag := getflag()
   147  		if tt.arg == "" {
   148  			assert.Equal(t, tt.want, flag.Value())
   149  			return
   150  		}
   151  
   152  		err := flag.Set(tt.arg)
   153  		if tt.wantErr {
   154  			assert.Error(t, err)
   155  			return
   156  		}
   157  
   158  		require.NoError(t, err)
   159  		assert.Equal(t, tt.want, flag.Value())
   160  	}
   161  }