2016-04-23 12:31:40 +00:00
|
|
|
package str_test
|
2016-04-15 23:13:39 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-12 11:34:50 +00:00
|
|
|
"testing"
|
2016-04-15 23:13:39 +00:00
|
|
|
|
2023-10-12 11:34:50 +00:00
|
|
|
. "github.com/Luzifer/go_helpers/v2/str"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-15 23:13:39 +00:00
|
|
|
)
|
|
|
|
|
2023-10-12 11:34:50 +00:00
|
|
|
func TestAppendIfMissing(t *testing.T) {
|
|
|
|
sl := []string{
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not append existing elements
|
|
|
|
assert.Len(t, AppendIfMissing(sl, "test1"), 3)
|
|
|
|
assert.Len(t, AppendIfMissing(sl, "test2"), 3)
|
|
|
|
assert.Len(t, AppendIfMissing(sl, "test3"), 3)
|
|
|
|
|
|
|
|
// should append not existing elements
|
|
|
|
assert.Len(t, AppendIfMissing(sl, "test4"), 4)
|
|
|
|
assert.Len(t, AppendIfMissing(sl, "test5"), 4)
|
|
|
|
assert.Len(t, AppendIfMissing(sl, "test6"), 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringInSlice(t *testing.T) {
|
|
|
|
sl := []string{
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
}
|
|
|
|
|
|
|
|
// should find elements of slice
|
|
|
|
assert.True(t, StringInSlice("test1", sl))
|
|
|
|
assert.True(t, StringInSlice("test2", sl))
|
|
|
|
assert.True(t, StringInSlice("test3", sl))
|
|
|
|
|
|
|
|
// should not find elements not in slice
|
|
|
|
assert.False(t, StringInSlice("test4", sl))
|
|
|
|
assert.False(t, StringInSlice("test5", sl))
|
|
|
|
assert.False(t, StringInSlice("test6", sl))
|
|
|
|
}
|