dubbo.apache.org/dubbo-go/v3@v3.1.1/config/config_utils_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  import (
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestMergeValue(t *testing.T) {
    29  	str := mergeValue("", "", "a,b")
    30  	assert.Equal(t, "a,b", str)
    31  
    32  	str = mergeValue("c,d", "e,f", "a,b")
    33  	assert.Equal(t, "a,b,c,d,e,f", str)
    34  
    35  	str = mergeValue("c,d", "e,d,f", "a,b")
    36  	assert.Equal(t, "a,b,c,d,e,d,f", str)
    37  
    38  	str = mergeValue("c,default,d", "-c,-a,e,f", "a,b")
    39  	assert.Equal(t, "b,d,e,f", str)
    40  
    41  	str = mergeValue("", "default,-b,e,f", "a,b")
    42  	assert.Equal(t, "a,e,f", str)
    43  }
    44  
    45  func TestRemoveMinus(t *testing.T) {
    46  	strList := removeMinus([]string{})
    47  	assert.Equal(t, strList, "")
    48  
    49  	strList = removeMinus([]string{"a", "b", "c", "d", "-a"})
    50  	assert.Equal(t, strList, "b,c,d")
    51  
    52  	strList = removeMinus([]string{"a", "b", "c", "d", "-a", "-b"})
    53  	assert.Equal(t, strList, "c,d")
    54  
    55  	strList = removeMinus([]string{"a", "b", "c", "-c", "-a", "-b"})
    56  	assert.Equal(t, strList, "")
    57  
    58  	strList = removeMinus([]string{"b", "a", "-c", "c"})
    59  	assert.Equal(t, strList, "b,a")
    60  
    61  	strList = removeMinus([]string{"c", "b", "a", "d", "c", "-c", "-a", "e", "f"})
    62  	assert.Equal(t, strList, "b,d,c,e,f")
    63  }
    64  
    65  type mockConfig struct {
    66  	protocol string
    67  	address  string
    68  }
    69  
    70  func (m *mockConfig) Prefix() string {
    71  	return "mock"
    72  }
    73  
    74  func TestClientNameID(t *testing.T) {
    75  	t.Run("normal", func(t *testing.T) {
    76  		m := &mockConfig{"nacos", "127.0.0.1:8848"}
    77  		assert.Equal(t, "mock-nacos-127.0.0.1:8848", clientNameID(m, m.protocol, m.address))
    78  	})
    79  	t.Run("protocolIsNil", func(t *testing.T) {
    80  		m := &mockConfig{}
    81  		assert.Equal(t, "mock--", clientNameID(m, m.protocol, m.address))
    82  	})
    83  }