...

Source file src/k8s.io/component-base/cli/flag/langle_separated_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 TestStringLangleSeparatedMapStringString(t *testing.T) {
    25  	var nilMap map[string]string
    26  	cases := []struct {
    27  		desc   string
    28  		m      *LangleSeparatedMapStringString
    29  		expect string
    30  	}{
    31  		{"nil", NewLangleSeparatedMapStringString(&nilMap), ""},
    32  		{"empty", NewLangleSeparatedMapStringString(&map[string]string{}), ""},
    33  		{"one key", NewLangleSeparatedMapStringString(&map[string]string{"one": "foo"}), "one<foo"},
    34  		{"two keys", NewLangleSeparatedMapStringString(&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 TestSetLangleSeparatedMapStringString(t *testing.T) {
    47  	var nilMap map[string]string
    48  	cases := []struct {
    49  		desc   string
    50  		vals   []string
    51  		start  *LangleSeparatedMapStringString
    52  		expect *LangleSeparatedMapStringString
    53  		err    string
    54  	}{
    55  		// we initialize the map with a default key that should be cleared by Set
    56  		{"clears defaults", []string{""},
    57  			NewLangleSeparatedMapStringString(&map[string]string{"default": ""}),
    58  			&LangleSeparatedMapStringString{
    59  				initialized: true,
    60  				Map:         &map[string]string{},
    61  			}, ""},
    62  		// make sure we still allocate for "initialized" maps where Map was initially set to a nil map
    63  		{"allocates map if currently nil", []string{""},
    64  			&LangleSeparatedMapStringString{initialized: true, Map: &nilMap},
    65  			&LangleSeparatedMapStringString{
    66  				initialized: true,
    67  				Map:         &map[string]string{},
    68  			}, ""},
    69  		// for most cases, we just reuse nilMap, which should be allocated by Set, and is reset before each test case
    70  		{"empty", []string{""},
    71  			NewLangleSeparatedMapStringString(&nilMap),
    72  			&LangleSeparatedMapStringString{
    73  				initialized: true,
    74  				Map:         &map[string]string{},
    75  			}, ""},
    76  		{"one key", []string{"one<foo"},
    77  			NewLangleSeparatedMapStringString(&nilMap),
    78  			&LangleSeparatedMapStringString{
    79  				initialized: true,
    80  				Map:         &map[string]string{"one": "foo"},
    81  			}, ""},
    82  		{"two keys", []string{"one<foo,two<bar"},
    83  			NewLangleSeparatedMapStringString(&nilMap),
    84  			&LangleSeparatedMapStringString{
    85  				initialized: true,
    86  				Map:         &map[string]string{"one": "foo", "two": "bar"},
    87  			}, ""},
    88  		{"two keys, multiple Set invocations", []string{"one<foo", "two<bar"},
    89  			NewLangleSeparatedMapStringString(&nilMap),
    90  			&LangleSeparatedMapStringString{
    91  				initialized: true,
    92  				Map:         &map[string]string{"one": "foo", "two": "bar"},
    93  			}, ""},
    94  		{"two keys with space", []string{"one<foo, two<bar"},
    95  			NewLangleSeparatedMapStringString(&nilMap),
    96  			&LangleSeparatedMapStringString{
    97  				initialized: true,
    98  				Map:         &map[string]string{"one": "foo", "two": "bar"},
    99  			}, ""},
   100  		{"empty key", []string{"<foo"},
   101  			NewLangleSeparatedMapStringString(&nilMap),
   102  			&LangleSeparatedMapStringString{
   103  				initialized: true,
   104  				Map:         &map[string]string{"": "foo"},
   105  			}, ""},
   106  		{"missing value", []string{"one"},
   107  			NewLangleSeparatedMapStringString(&nilMap),
   108  			nil,
   109  			"malformed pair, expect string<string"},
   110  		{"no target", []string{"a:foo"},
   111  			NewLangleSeparatedMapStringString(nil),
   112  			nil,
   113  			"no target (nil pointer to map[string]string)"},
   114  	}
   115  	for _, c := range cases {
   116  		nilMap = nil
   117  		t.Run(c.desc, func(t *testing.T) {
   118  			var err error
   119  			for _, val := range c.vals {
   120  				err = c.start.Set(val)
   121  				if err != nil {
   122  					break
   123  				}
   124  			}
   125  			if c.err != "" {
   126  				if err == nil || err.Error() != c.err {
   127  					t.Fatalf("expect error %s but got %v", c.err, err)
   128  				}
   129  				return
   130  			} else if err != nil {
   131  				t.Fatalf("unexpected error: %v", err)
   132  			}
   133  			if !reflect.DeepEqual(c.expect, c.start) {
   134  				t.Fatalf("expect %#v but got %#v", c.expect, c.start)
   135  			}
   136  		})
   137  	}
   138  }
   139  
   140  func TestEmptyLangleSeparatedMapStringString(t *testing.T) {
   141  	var nilMap map[string]string
   142  	cases := []struct {
   143  		desc   string
   144  		val    *LangleSeparatedMapStringString
   145  		expect bool
   146  	}{
   147  		{"nil", NewLangleSeparatedMapStringString(&nilMap), true},
   148  		{"empty", NewLangleSeparatedMapStringString(&map[string]string{}), true},
   149  		{"populated", NewLangleSeparatedMapStringString(&map[string]string{"foo": ""}), false},
   150  	}
   151  	for _, c := range cases {
   152  		t.Run(c.desc, func(t *testing.T) {
   153  			result := c.val.Empty()
   154  			if result != c.expect {
   155  				t.Fatalf("expect %t but got %t", c.expect, result)
   156  			}
   157  		})
   158  	}
   159  }
   160  

View as plain text