...

Source file src/k8s.io/utils/strings/strings_test.go

Documentation: k8s.io/utils/strings

     1  /*
     2  Copyright 2014 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 strings
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestSplitQualifiedName(t *testing.T) {
    24  	testCases := []struct {
    25  		input  string
    26  		output []string
    27  	}{
    28  		{"kubernetes.io/blah", []string{"kubernetes.io", "blah"}},
    29  		{"blah", []string{"", "blah"}},
    30  		{"kubernetes.io/blah/blah", []string{"kubernetes.io", "blah"}},
    31  	}
    32  	for i, tc := range testCases {
    33  		namespace, name := SplitQualifiedName(tc.input)
    34  		if namespace != tc.output[0] || name != tc.output[1] {
    35  			t.Errorf("case[%d]: expected (%q, %q), got (%q, %q)", i, tc.output[0], tc.output[1], namespace, name)
    36  		}
    37  	}
    38  }
    39  
    40  func TestJoinQualifiedName(t *testing.T) {
    41  	testCases := []struct {
    42  		input  []string
    43  		output string
    44  	}{
    45  		{[]string{"kubernetes.io", "blah"}, "kubernetes.io/blah"},
    46  		{[]string{"blah", ""}, "blah"},
    47  		{[]string{"kubernetes.io", "blah"}, "kubernetes.io/blah"},
    48  	}
    49  	for i, tc := range testCases {
    50  		res := JoinQualifiedName(tc.input[0], tc.input[1])
    51  		if res != tc.output {
    52  			t.Errorf("case[%d]: expected %q, got %q", i, tc.output, res)
    53  		}
    54  	}
    55  }
    56  
    57  func TestShortenString(t *testing.T) {
    58  	testCases := []struct {
    59  		input  string
    60  		outLen int
    61  		output string
    62  	}{
    63  		{"kubernetes.io", 5, "kuber"},
    64  		{"blah", 34, "blah"},
    65  		{"kubernetes.io", 13, "kubernetes.io"},
    66  	}
    67  	for i, tc := range testCases {
    68  		res := ShortenString(tc.input, tc.outLen)
    69  		if res != tc.output {
    70  			t.Errorf("case[%d]: expected %q, got %q", i, tc.output, res)
    71  		}
    72  	}
    73  }
    74  

View as plain text