154 lines
4.4 KiB
Go
154 lines
4.4 KiB
Go
![]() |
package array_test
|
|||
|
|
|||
|
import (
|
|||
|
"strings"
|
|||
|
"testing"
|
|||
|
|
|||
|
"github.com/stretchr/testify/assert"
|
|||
|
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/pkg/array"
|
|||
|
)
|
|||
|
|
|||
|
func TestContains(t *testing.T) {
|
|||
|
testCasesWithPrimitives := []struct {
|
|||
|
name string
|
|||
|
inputArray []any
|
|||
|
inputCallback func(any, int, []any) bool
|
|||
|
expect bool
|
|||
|
}{
|
|||
|
{
|
|||
|
name: "Проверка наличие строк по значению",
|
|||
|
inputArray: []any{"test1", "test2"},
|
|||
|
inputCallback: func(element any, index int, array []any) bool {
|
|||
|
assert.Equal(t, array, []any{"test1", "test2"})
|
|||
|
|
|||
|
return element == "test1"
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие строк по значению (неудачная)",
|
|||
|
inputArray: []any{"test1", "test2"},
|
|||
|
inputCallback: func(element any, index int, array []any) bool {
|
|||
|
assert.Equal(t, array, []any{"test1", "test2"})
|
|||
|
|
|||
|
return element == "tttt"
|
|||
|
},
|
|||
|
expect: false,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие строк по индексу",
|
|||
|
inputArray: []any{"test1", "test2"},
|
|||
|
inputCallback: func(element any, index int, array []any) bool {
|
|||
|
assert.Equal(t, array, []any{"test1", "test2"})
|
|||
|
|
|||
|
return index == 1
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие чисел по значению",
|
|||
|
inputArray: []any{1, 4},
|
|||
|
inputCallback: func(element any, index int, array []any) bool {
|
|||
|
assert.Equal(t, array, []any{1, 4})
|
|||
|
|
|||
|
return element == 1
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие строк по значению с несколькими схожими значениями",
|
|||
|
inputArray: []any{"test1", "test2"},
|
|||
|
inputCallback: func(element any, index int, array []any) bool {
|
|||
|
assert.Equal(t, array, []any{"test1", "test2"})
|
|||
|
|
|||
|
return strings.Contains(element.(string), "test")
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
}
|
|||
|
|
|||
|
testCasesWithObjects := []struct {
|
|||
|
name string
|
|||
|
inputArray []struct{ Name string }
|
|||
|
inputCallback func(struct{ Name string }, int, []struct{ Name string }) bool
|
|||
|
expect bool
|
|||
|
}{
|
|||
|
{
|
|||
|
name: "Проверка наличие объектов по индексу",
|
|||
|
inputArray: []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
},
|
|||
|
inputCallback: func(element struct{ Name string }, index int, array []struct{ Name string }) bool {
|
|||
|
assert.Equal(t, array, []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
})
|
|||
|
|
|||
|
return index == 1
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие объектов по имени (неудачная)",
|
|||
|
inputArray: []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
},
|
|||
|
inputCallback: func(element struct{ Name string }, index int, array []struct{ Name string }) bool {
|
|||
|
assert.Equal(t, array, []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
})
|
|||
|
|
|||
|
return element.Name == "tttt"
|
|||
|
},
|
|||
|
expect: false,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие объектов по значению поля",
|
|||
|
inputArray: []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
},
|
|||
|
inputCallback: func(element struct{ Name string }, index int, array []struct{ Name string }) bool {
|
|||
|
assert.Equal(t, array, []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
})
|
|||
|
|
|||
|
return element.Name == "test1"
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
{
|
|||
|
name: "Проверка наличие объектов по совпадению значения поля",
|
|||
|
inputArray: []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
},
|
|||
|
inputCallback: func(element struct{ Name string }, index int, array []struct{ Name string }) bool {
|
|||
|
assert.Equal(t, array, []struct{ Name string }{
|
|||
|
{Name: "test1"},
|
|||
|
{Name: "test2"},
|
|||
|
})
|
|||
|
|
|||
|
return strings.Contains(element.Name, "test")
|
|||
|
},
|
|||
|
expect: true,
|
|||
|
},
|
|||
|
}
|
|||
|
|
|||
|
for _, test := range testCasesWithPrimitives {
|
|||
|
t.Run(test.name, func(t *testing.T) {
|
|||
|
assert.Equal(t, test.expect, array.Contains(test.inputArray, test.inputCallback))
|
|||
|
})
|
|||
|
}
|
|||
|
|
|||
|
for _, test := range testCasesWithObjects {
|
|||
|
t.Run(test.name, func(t *testing.T) {
|
|||
|
assert.Equal(t, test.expect, array.Contains(test.inputArray, test.inputCallback))
|
|||
|
})
|
|||
|
}
|
|||
|
}
|