badguardhome/internal/aghstrings/strings_test.go
Ainar Garipov 48d702f76a Pull request: all: fix client custom upstream comments
Updates #2947.

Squashed commit of the following:

commit 498a05459b1aa00bcffee490acfeecb567025971
Merge: 6a7a2f87 21e2c419
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 13 13:40:29 2021 +0300

    Merge branch 'master' into 2947-cli-ups-comment

commit 6a7a2f87cfd2bdd829b82889890511fef8d84b9b
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 13 13:34:28 2021 +0300

    all: imp code, tests

commit abc0be239f69cfc3e7d0cde2fc952d9157b2cd5d
Merge: 82fb3fcb 6410feeb
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Apr 13 13:17:09 2021 +0300

    Merge branch 'master' into 2947-cli-ups-comment

commit 82fb3fcb49cbc8d439cb5959c1cb84ae49b2257e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Apr 12 22:13:41 2021 +0300

    all: fix client custom upstream comments
2021-04-13 13:44:29 +03:00

130 lines
2.5 KiB
Go

package aghstrings
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCloneSlice_family(t *testing.T) {
a := []string{"1", "2", "3"}
t.Run("cloneslice_simple", func(t *testing.T) {
assert.Equal(t, a, CloneSlice(a))
})
t.Run("cloneslice_nil", func(t *testing.T) {
assert.Nil(t, CloneSlice(nil))
})
t.Run("cloneslice_empty", func(t *testing.T) {
assert.Equal(t, []string{}, CloneSlice([]string{}))
})
t.Run("clonesliceorempty_nil", func(t *testing.T) {
assert.Equal(t, []string{}, CloneSliceOrEmpty(nil))
})
t.Run("clonesliceorempty_empty", func(t *testing.T) {
assert.Equal(t, []string{}, CloneSliceOrEmpty([]string{}))
})
t.Run("clonesliceorempty_sameness", func(t *testing.T) {
assert.Equal(t, CloneSlice(a), CloneSliceOrEmpty(a))
})
}
func TestFilterOut(t *testing.T) {
strs := []string{
"1.2.3.4",
"",
"# 5.6.7.8",
}
want := []string{
"1.2.3.4",
}
got := FilterOut(strs, IsCommentOrEmpty)
assert.Equal(t, want, got)
}
func TestInSlice(t *testing.T) {
simpleStrs := []string{"1", "2", "3"}
testCases := []struct {
name string
str string
strs []string
want bool
}{{
name: "yes",
str: "2",
strs: simpleStrs,
want: true,
}, {
name: "no",
str: "4",
strs: simpleStrs,
want: false,
}, {
name: "nil",
str: "any",
strs: nil,
want: false,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, InSlice(tc.strs, tc.str))
})
}
}
func TestSplitNext(t *testing.T) {
t.Run("ordinary", func(t *testing.T) {
s := " a,b , c "
require.Equal(t, "a", SplitNext(&s, ','))
require.Equal(t, "b", SplitNext(&s, ','))
require.Equal(t, "c", SplitNext(&s, ','))
assert.Empty(t, s)
})
t.Run("nil_source", func(t *testing.T) {
assert.Equal(t, "", SplitNext(nil, 's'))
})
}
func TestWriteToBuilder(t *testing.T) {
b := &strings.Builder{}
t.Run("single", func(t *testing.T) {
assert.NotPanics(t, func() { WriteToBuilder(b, t.Name()) })
assert.Equal(t, t.Name(), b.String())
})
b.Reset()
t.Run("several", func(t *testing.T) {
const (
_1 = "one"
_2 = "two"
_123 = _1 + _2
)
assert.NotPanics(t, func() { WriteToBuilder(b, _1, _2) })
assert.Equal(t, _123, b.String())
})
b.Reset()
t.Run("nothing", func(t *testing.T) {
assert.NotPanics(t, func() { WriteToBuilder(b) })
assert.Equal(t, "", b.String())
})
t.Run("nil_builder", func(t *testing.T) {
assert.Panics(t, func() { WriteToBuilder(nil, "a") })
})
}