2020-01-28 00:40:53 +02:00
|
|
|
package piperutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-03-23 11:38:31 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-28 00:40:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainsInt(t *testing.T) {
|
|
|
|
var intList []int
|
2020-04-24 10:41:49 +02:00
|
|
|
assert.Equal(t, false, ContainsInt(intList, 4))
|
2020-01-28 00:40:53 +02:00
|
|
|
|
|
|
|
intList = append(intList, 1, 2, 3, 4, 5, 6, 20)
|
2020-04-24 10:41:49 +02:00
|
|
|
assert.Equal(t, true, ContainsInt(intList, 20))
|
|
|
|
assert.Equal(t, true, ContainsInt(intList, 1))
|
|
|
|
assert.Equal(t, true, ContainsInt(intList, 4))
|
|
|
|
assert.Equal(t, false, ContainsInt(intList, 13))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainsString(t *testing.T) {
|
|
|
|
var stringList []string
|
|
|
|
assert.False(t, ContainsString(stringList, "test"))
|
|
|
|
assert.False(t, ContainsString(stringList, ""))
|
|
|
|
|
|
|
|
stringList = append(stringList, "", "foo", "bar", "foo")
|
|
|
|
assert.True(t, ContainsString(stringList, ""))
|
|
|
|
assert.True(t, ContainsString(stringList, "bar"))
|
|
|
|
assert.True(t, ContainsString(stringList, "foo"))
|
|
|
|
assert.False(t, ContainsString(stringList, "baz"))
|
2020-01-28 00:40:53 +02:00
|
|
|
}
|
2020-03-23 11:38:31 +02:00
|
|
|
|
|
|
|
func TestPrefix(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{"tree", "pie", "applejuice"}
|
|
|
|
// test
|
|
|
|
s = Prefix(s, "apple")
|
|
|
|
// assert
|
|
|
|
assert.Contains(t, s, "appletree")
|
|
|
|
assert.Contains(t, s, "applepie")
|
|
|
|
assert.Contains(t, s, "appleapplejuice")
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:55:46 +02:00
|
|
|
func TestPrefixIfNeeded(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{"tree", "pie", "applejuice"}
|
|
|
|
// test
|
|
|
|
s = PrefixIfNeeded(s, "apple")
|
|
|
|
// assert
|
|
|
|
assert.Contains(t, s, "appletree")
|
|
|
|
assert.Contains(t, s, "applepie")
|
|
|
|
assert.Contains(t, s, "applejuice")
|
|
|
|
}
|
|
|
|
|
2020-03-23 11:38:31 +02:00
|
|
|
func TestTrim(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{" orange", "banana ", " apple", "mango ", " ", ""}
|
|
|
|
// test
|
|
|
|
s = Trim(s)
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, 4, len(s))
|
|
|
|
assert.Contains(t, s, "orange")
|
|
|
|
assert.Contains(t, s, "banana")
|
|
|
|
assert.Contains(t, s, "apple")
|
|
|
|
assert.Contains(t, s, "mango")
|
|
|
|
}
|
2020-04-24 10:41:49 +02:00
|
|
|
|
|
|
|
func TestSplitTrimAndDeDup(t *testing.T) {
|
|
|
|
t.Run("Separator is not space", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{" a", "", "-a-b --c ", "d-e", "f", " f", ""}
|
|
|
|
// test
|
|
|
|
s = SplitTrimAndDeDup(s, "-")
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f"}, s)
|
|
|
|
})
|
|
|
|
t.Run("Separator is space", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{" a", " a b c ", "d e", "f", "f ", ""}
|
|
|
|
// test
|
|
|
|
s = SplitTrimAndDeDup(s, " ")
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f"}, s)
|
|
|
|
})
|
|
|
|
t.Run("Separator is multi-char", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{" a", " a** b**c ", "**d **e", "f**", "f ", ""}
|
|
|
|
// test
|
|
|
|
s = SplitTrimAndDeDup(s, "**")
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f"}, s)
|
|
|
|
})
|
|
|
|
t.Run("Separator is empty string", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
s := []string{" a", " a bc ", "d e", "f", "f ", ""}
|
|
|
|
// test
|
|
|
|
s = SplitTrimAndDeDup(s, "")
|
|
|
|
// assert
|
|
|
|
// If "sep" is empty, underlying strings.Split() splits after each UTF-8 char sequence.
|
|
|
|
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f"}, s)
|
|
|
|
})
|
|
|
|
}
|