istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/tools/featuresgen/cmd/root_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 cmd
    16  
    17  import (
    18  	"sort"
    19  	"strings"
    20  	"testing"
    21  
    22  	"gopkg.in/yaml.v2"
    23  )
    24  
    25  func TestCreateConstantString(t *testing.T) {
    26  	s1 := createConstantString([]string{"lab-el1", "label2"})
    27  	if s1 != "\tLabEl1_Label2\tFeature = \"lab-el1.label2\"" {
    28  		t.Errorf("Expected '\tLabEl1_Label2\tFeature = \"lab-el1.label2\"' got '%s'", s1)
    29  	}
    30  
    31  	s2 := createConstantString([]string{"lab.*)($)#el1", "lab.el2"})
    32  	if s2 != "\tLabel1_Label2\tFeature = \"lab*)($)#el1.label2\"" {
    33  		t.Errorf("Expected '\tLabel1_Label2\tFeature = \"lab*)($)#el1.label2\"' got '%s'", s2)
    34  	}
    35  }
    36  
    37  var testYaml = `
    38  features:
    39    values: [hello1, hello2]
    40    key2:
    41      values: [val1, val2]
    42  `
    43  
    44  const expectedResult = `	Hello1	Feature = "hello1"
    45  	Hello2	Feature = "hello2"
    46  	Key2_Val1	Feature = "key2.val1"
    47  	Key2_Val2	Feature = "key2.val2"`
    48  
    49  func TestReadVal(t *testing.T) {
    50  	m := make(map[any]any)
    51  
    52  	err := yaml.Unmarshal([]byte(testYaml), &m)
    53  	if err != nil {
    54  		t.Errorf(err.Error())
    55  	}
    56  
    57  	s1 := readVal(m, make([]string, 0))
    58  
    59  	sort.Strings(s1)
    60  
    61  	if strings.Join(s1, "\n") != expectedResult {
    62  		t.Errorf("Expected '%s' got '%s'", expectedResult, strings.Join(s1, "\n"))
    63  	}
    64  }