...

Source file src/go.etcd.io/etcd/pkg/v3/flags/selective_string_test.go

Documentation: go.etcd.io/etcd/pkg/v3/flags

     1  // Copyright 2018 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package flags
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestSelectiveStringValue(t *testing.T) {
    22  	tests := []struct {
    23  		vals []string
    24  
    25  		val  string
    26  		pass bool
    27  	}{
    28  		// known values
    29  		{[]string{"abc", "def"}, "abc", true},
    30  		{[]string{"on", "off", "false"}, "on", true},
    31  
    32  		// unrecognized values
    33  		{[]string{"abc", "def"}, "ghi", false},
    34  		{[]string{"on", "off"}, "", false},
    35  	}
    36  	for i, tt := range tests {
    37  		sf := NewSelectiveStringValue(tt.vals...)
    38  		if sf.v != tt.vals[0] {
    39  			t.Errorf("#%d: want default val=%v,but got %v", i, tt.vals[0], sf.v)
    40  		}
    41  		err := sf.Set(tt.val)
    42  		if tt.pass != (err == nil) {
    43  			t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
    44  		}
    45  	}
    46  }
    47  
    48  func TestSelectiveStringsValue(t *testing.T) {
    49  	tests := []struct {
    50  		vals []string
    51  
    52  		val  string
    53  		pass bool
    54  	}{
    55  		{[]string{"abc", "def"}, "abc", true},
    56  		{[]string{"abc", "def"}, "abc,def", true},
    57  		{[]string{"abc", "def"}, "abc, def", false},
    58  		{[]string{"on", "off", "false"}, "on,false", true},
    59  		{[]string{"abc", "def"}, "ghi", false},
    60  		{[]string{"on", "off"}, "", false},
    61  		{[]string{"a", "b", "c", "d", "e"}, "a,c,e", true},
    62  	}
    63  	for i, tt := range tests {
    64  		sf := NewSelectiveStringsValue(tt.vals...)
    65  		err := sf.Set(tt.val)
    66  		if tt.pass != (err == nil) {
    67  			t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
    68  		}
    69  	}
    70  }
    71  

View as plain text