...

Source file src/k8s.io/component-base/cli/flag/map_string_string_test.go

Documentation: k8s.io/component-base/cli/flag

     1  /*
     2  Copyright 2017 The Kubernetes 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 agreed to 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 flag
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestStringMapStringString(t *testing.T) {
    25  	var nilMap map[string]string
    26  	cases := []struct {
    27  		desc   string
    28  		m      *MapStringString
    29  		expect string
    30  	}{
    31  		{"nil", NewMapStringString(&nilMap), ""},
    32  		{"empty", NewMapStringString(&map[string]string{}), ""},
    33  		{"one key", NewMapStringString(&map[string]string{"one": "foo"}), "one=foo"},
    34  		{"two keys", NewMapStringString(&map[string]string{"one": "foo", "two": "bar"}), "one=foo,two=bar"},
    35  	}
    36  	for _, c := range cases {
    37  		t.Run(c.desc, func(t *testing.T) {
    38  			str := c.m.String()
    39  			if c.expect != str {
    40  				t.Fatalf("expect %q but got %q", c.expect, str)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func TestSetMapStringString(t *testing.T) {
    47  	var nilMap map[string]string
    48  	cases := []struct {
    49  		desc   string
    50  		vals   []string
    51  		start  *MapStringString
    52  		expect *MapStringString
    53  		err    string
    54  	}{
    55  		// we initialize the map with a default key that should be cleared by Set
    56  		{"clears defaults", []string{""},
    57  			NewMapStringString(&map[string]string{"default": ""}),
    58  			&MapStringString{
    59  				initialized: true,
    60  				Map:         &map[string]string{},
    61  				NoSplit:     false,
    62  			}, ""},
    63  		// make sure we still allocate for "initialized" maps where Map was initially set to a nil map
    64  		{"allocates map if currently nil", []string{""},
    65  			&MapStringString{initialized: true, Map: &nilMap},
    66  			&MapStringString{
    67  				initialized: true,
    68  				Map:         &map[string]string{},
    69  				NoSplit:     false,
    70  			}, ""},
    71  		// for most cases, we just reuse nilMap, which should be allocated by Set, and is reset before each test case
    72  		{"empty", []string{""},
    73  			NewMapStringString(&nilMap),
    74  			&MapStringString{
    75  				initialized: true,
    76  				Map:         &map[string]string{},
    77  				NoSplit:     false,
    78  			}, ""},
    79  		{"one key", []string{"one=foo"},
    80  			NewMapStringString(&nilMap),
    81  			&MapStringString{
    82  				initialized: true,
    83  				Map:         &map[string]string{"one": "foo"},
    84  				NoSplit:     false,
    85  			}, ""},
    86  		{"two keys", []string{"one=foo,two=bar"},
    87  			NewMapStringString(&nilMap),
    88  			&MapStringString{
    89  				initialized: true,
    90  				Map:         &map[string]string{"one": "foo", "two": "bar"},
    91  				NoSplit:     false,
    92  			}, ""},
    93  		{"one key, multi flag invocation only", []string{"one=foo,bar"},
    94  			NewMapStringStringNoSplit(&nilMap),
    95  			&MapStringString{
    96  				initialized: true,
    97  				Map:         &map[string]string{"one": "foo,bar"},
    98  				NoSplit:     true,
    99  			}, ""},
   100  		{"two keys, multi flag invocation only", []string{"one=foo,bar", "two=foo,bar"},
   101  			NewMapStringStringNoSplit(&nilMap),
   102  			&MapStringString{
   103  				initialized: true,
   104  				Map:         &map[string]string{"one": "foo,bar", "two": "foo,bar"},
   105  				NoSplit:     true,
   106  			}, ""},
   107  		{"two keys, multiple Set invocations", []string{"one=foo", "two=bar"},
   108  			NewMapStringString(&nilMap),
   109  			&MapStringString{
   110  				initialized: true,
   111  				Map:         &map[string]string{"one": "foo", "two": "bar"},
   112  				NoSplit:     false,
   113  			}, ""},
   114  		{"two keys with space", []string{"one=foo, two=bar"},
   115  			NewMapStringString(&nilMap),
   116  			&MapStringString{
   117  				initialized: true,
   118  				Map:         &map[string]string{"one": "foo", "two": "bar"},
   119  				NoSplit:     false,
   120  			}, ""},
   121  		{"empty key", []string{"=foo"},
   122  			NewMapStringString(&nilMap),
   123  			&MapStringString{
   124  				initialized: true,
   125  				Map:         &map[string]string{"": "foo"},
   126  				NoSplit:     false,
   127  			}, ""},
   128  		{"missing value", []string{"one"},
   129  			NewMapStringString(&nilMap),
   130  			nil,
   131  			"malformed pair, expect string=string"},
   132  		{"no target", []string{"a:foo"},
   133  			NewMapStringString(nil),
   134  			nil,
   135  			"no target (nil pointer to map[string]string)"},
   136  	}
   137  	for _, c := range cases {
   138  		nilMap = nil
   139  		t.Run(c.desc, func(t *testing.T) {
   140  			var err error
   141  			for _, val := range c.vals {
   142  				err = c.start.Set(val)
   143  				if err != nil {
   144  					break
   145  				}
   146  			}
   147  			if c.err != "" {
   148  				if err == nil || err.Error() != c.err {
   149  					t.Fatalf("expect error %s but got %v", c.err, err)
   150  				}
   151  				return
   152  			} else if err != nil {
   153  				t.Fatalf("unexpected error: %v", err)
   154  			}
   155  			if !reflect.DeepEqual(c.expect, c.start) {
   156  				t.Fatalf("expect %#v but got %#v", c.expect, c.start)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func TestEmptyMapStringString(t *testing.T) {
   163  	var nilMap map[string]string
   164  	cases := []struct {
   165  		desc   string
   166  		val    *MapStringString
   167  		expect bool
   168  	}{
   169  		{"nil", NewMapStringString(&nilMap), true},
   170  		{"empty", NewMapStringString(&map[string]string{}), true},
   171  		{"populated", NewMapStringString(&map[string]string{"foo": ""}), false},
   172  	}
   173  	for _, c := range cases {
   174  		t.Run(c.desc, func(t *testing.T) {
   175  			result := c.val.Empty()
   176  			if result != c.expect {
   177  				t.Fatalf("expect %t but got %t", c.expect, result)
   178  			}
   179  		})
   180  	}
   181  }
   182  

View as plain text