github.com/erda-project/erda-infra@v1.0.9/pkg/strutil/strutil_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     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 strutil
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestTrim(t *testing.T) {
    24  	assert.Equal(t, "trim", Trim("trim "))
    25  	assert.Equal(t, "this", Trim(" this  "))
    26  	assert.Equal(t, "thi", Trim("athisb", "abs"))
    27  }
    28  
    29  func TestCollapseWhitespace(t *testing.T) {
    30  	assert.Equal(t, "only one space", CollapseWhitespace("only    one   space"))
    31  	assert.Equal(t, "collapse all sorts of whitespace", CollapseWhitespace("collapse \n   all \t  sorts of \r \n \r\n whitespace"))
    32  }
    33  
    34  func TestCenter(t *testing.T) {
    35  	assert.Equal(t, "  a  ", Center("a", 5))
    36  	assert.Equal(t, "  ab ", Center("ab", 5))
    37  	assert.Equal(t, "abc", Center("abc", 1))
    38  }
    39  
    40  func TestTrimSuffixes(t *testing.T) {
    41  	assert.Equal(t, "test", TrimSuffixes("test.go", ".go"))
    42  	assert.Equal(t, "test", TrimSuffixes("test.go", ".go", ".md", ".sh"))
    43  	assert.Equal(t, "test.go", TrimSuffixes("test.go.tmp", ".go", ".tmp"))
    44  }
    45  
    46  func TestTrimPrefixes(t *testing.T) {
    47  	assert.Equal(t, "/file", TrimPrefixes("/tmp/file", "/tmp"))
    48  	assert.Equal(t, "/tmp/file", TrimPrefixes("/tmp/tmp/file", "/tmp", "/tmp/tmp"))
    49  }
    50  
    51  func TestSplit(t *testing.T) {
    52  	assert.Equal(t, []string{"a", "bc", "12", "", "3"}, Split("a|bc|12||3", "|"))
    53  	assert.Equal(t, []string{"a", "bc", "12", "3"}, Split("a|bc|12||3", "|", true))
    54  	assert.Equal(t, []string{"a,b,c"}, Split("a,b,c", ":"))
    55  
    56  }
    57  
    58  func TestLines(t *testing.T) {
    59  	assert.Equal(t, []string{"abc", "def", "ghi"}, Lines("abc\ndef\nghi"))
    60  	assert.Equal(t, []string{"abc", "def", "ghi"}, Lines("abc\rdef\rghi"))
    61  	assert.Equal(t, []string{"abc", "def", "ghi", ""}, Lines("abc\r\ndef\r\nghi\n"))
    62  	assert.Equal(t, []string{"abc", "def", "ghi"}, Lines("abc\r\ndef\r\nghi\n", true))
    63  }
    64  
    65  func TestContains(t *testing.T) {
    66  	assert.True(t, Contains("test contains.", "t c", "iii"))
    67  	assert.False(t, Contains("test contains.", "t cc", "test  "))
    68  	assert.True(t, Contains("test contains.", "iii", "uuu", "ont"))
    69  }
    70  func TestEqual(t *testing.T) {
    71  	assert.False(t, Equal("aaa", "AAA"))
    72  	assert.True(t, Equal("aaa", "AaA", true))
    73  }
    74  
    75  func TestHasPrefixes(t *testing.T) {
    76  	assert.False(t, HasPrefixes("asd", "ddd", "uuu"))
    77  	assert.True(t, HasPrefixes("asd", "sd", "as"))
    78  	assert.True(t, HasPrefixes("asd", "asd"))
    79  }
    80  
    81  func TestHasSuffixes(t *testing.T) {
    82  	assert.True(t, HasSuffixes("asd", "ddd", "d"))
    83  	assert.True(t, HasSuffixes("asd", "sd"))
    84  	assert.False(t, HasSuffixes("asd", "iid", "as"))
    85  }
    86  
    87  func TestTrimSlice(t *testing.T) {
    88  	assert.Equal(t, []string{"trim", "trim", "trim"}, TrimSlice([]string{"trim ", " trim", " trim "}))
    89  }
    90  
    91  func TestTrimSliceSuffixes(t *testing.T) {
    92  	assert.Equal(t, []string{"test", "test.go"}, TrimSliceSuffixes([]string{"test.go", "test.go.tmp"}, ".go", ".tmp"))
    93  }
    94  
    95  func TestTrimSlicePrefixes(t *testing.T) {
    96  	assert.Equal(t, []string{"/file", "/tmp/file"}, TrimSlicePrefixes([]string{"/tmp/file", "/tmp/tmp/file"}, "/tmp", "/tmp/tmp"))
    97  }
    98  
    99  func TestDedupSlice(t *testing.T) {
   100  	assert.Equal(t, []string{"c", "", "b", "a", "d"}, DedupSlice([]string{"c", "", "b", "a", "", "a", "b", "c", "", "d"}))
   101  	assert.Equal(t, []string{"c", "b", "a", "d"}, DedupSlice([]string{"c", "", "b", "a", "", "a", "b", "c", "", "d"}, true))
   102  }
   103  
   104  func TestDedupUint64Slice(t *testing.T) {
   105  	assert.Equal(t, []uint64{3, 1, 2, 0}, DedupUint64Slice([]uint64{3, 3, 1, 2, 1, 2, 3, 3, 2, 1, 0, 1, 2}))
   106  	assert.Equal(t, []uint64{3, 1, 2}, DedupUint64Slice([]uint64{3, 3, 1, 2, 1, 2, 3, 3, 2, 1, 0, 1, 2}, true))
   107  }
   108  
   109  func TestIntersectionUin64Slice(t *testing.T) {
   110  	assert.Equal(t, []uint64{3, 0}, IntersectionUin64Slice([]uint64{3, 1, 2, 0}, []uint64{0, 3}))
   111  	assert.Equal(t, []uint64{1, 2, 1, 0}, IntersectionUin64Slice([]uint64{3, 1, 2, 1, 0}, []uint64{1, 2, 0}))
   112  }
   113  
   114  func TestRemoveSlice(t *testing.T) {
   115  	assert.Equal(t, []string{"b", "c"}, RemoveSlice([]string{"a", "b", "c", "a"}, "a"))
   116  	assert.Equal(t, []string{"a", "a"}, RemoveSlice([]string{"a", "b", "c", "a"}, "b", "c"))
   117  	assert.Equal(t, []string{}, RemoveSlice([]string{"a", "b", "c", "a"}, "a", "b", "c"))
   118  }
   119  
   120  func TestNormalizeNewlines(t *testing.T) {
   121  	unixStyle := []byte("version: 1.1\nstages: \n[]")
   122  	macStyle := []byte("version: 1.1\rstages: \r[]")
   123  	windowsStyle := []byte("version: 1.1\r\nstages: \r\n[]")
   124  	assert.Equal(t, unixStyle, NormalizeNewlines(unixStyle))
   125  	assert.Equal(t, unixStyle, NormalizeNewlines(macStyle))
   126  	assert.Equal(t, unixStyle, NormalizeNewlines(windowsStyle))
   127  }
   128  
   129  func TestReverseSlice(t *testing.T) {
   130  	ss := []string{"s1", "s2", "s3"}
   131  	ReverseSlice(ss)
   132  	assert.Equal(t, "s3", ss[0])
   133  	assert.Equal(t, "s2", ss[1])
   134  	assert.Equal(t, "s1", ss[2])
   135  }