istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/xds/util_test.go (about)

     1  // Copyright Istio 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 xds
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  func TestAtMostNJoin(t *testing.T) {
    24  	tests := []struct {
    25  		data  []string
    26  		limit int
    27  		want  string
    28  	}{
    29  		{
    30  			[]string{"a", "b", "c"},
    31  			2,
    32  			"a, and 2 others",
    33  		},
    34  		{
    35  			[]string{"a", "b", "c"},
    36  			4,
    37  			"a, b, c",
    38  		},
    39  		{
    40  			[]string{"a", "b", "c"},
    41  			1,
    42  			"a, b, c",
    43  		},
    44  		{
    45  			[]string{"a", "b", "c"},
    46  			0,
    47  			"a, b, c",
    48  		},
    49  		{
    50  			[]string{},
    51  			3,
    52  			"",
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(fmt.Sprintf("%s-%d", strings.Join(tt.data, "-"), tt.limit), func(t *testing.T) {
    57  			if got := atMostNJoin(tt.data, tt.limit); got != tt.want {
    58  				t.Errorf("got %v, want %v", got, tt.want)
    59  			}
    60  		})
    61  	}
    62  }