Files
opencloud/pkg/structs/structs_test.go

174 lines
4.7 KiB
Go

package structs
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type example struct {
Attribute1 string
Attribute2 string
}
func TestCopyOrZeroValue(t *testing.T) {
var e *example
zv := CopyOrZeroValue(e)
if zv == nil {
t.Error("CopyOrZeroValue returned nil")
}
if zv.Attribute1 != "" || zv.Attribute2 != "" {
t.Error("CopyOrZeroValue didn't return zero value")
}
e2 := &example{Attribute1: "One", Attribute2: "Two"}
cp := CopyOrZeroValue(e2)
if cp == nil {
t.Error("CopyOrZeroValue returned nil")
}
if cp == e2 {
t.Error("CopyOrZeroValue returned reference with same address")
}
if cp.Attribute1 != e2.Attribute1 || cp.Attribute2 != e2.Attribute2 {
t.Error("CopyOrZeroValue didn't correctly copy attributes")
}
}
func TestUniqWithInts(t *testing.T) {
tests := []struct {
input []int
expected []int
}{
{[]int{5, 1, 3, 1, 4}, []int{5, 1, 3, 4}},
{[]int{1, 1, 1}, []int{1}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d: testing %v", i+1, tt.input), func(t *testing.T) {
result := Uniq(tt.input)
assert.EqualValues(t, tt.expected, result)
})
}
}
type u struct {
x int
y string
}
var (
u1 = u{x: 1, y: "un"}
u2 = u{x: 2, y: "deux"}
u3 = u{x: 3, y: "trois"}
)
func TestUniqWithStructs(t *testing.T) {
tests := []struct {
input []u
expected []u
}{
{[]u{u3, u1, u2, u3, u2, u1}, []u{u3, u1, u2}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d: testing %v", i+1, tt.input), func(t *testing.T) {
result := Uniq(tt.input)
assert.EqualValues(t, tt.expected, result)
})
}
}
func TestKeys(t *testing.T) {
tests := []struct {
input map[int]string
expected []int
}{
{map[int]string{5: "cinq", 1: "un", 3: "trois", 4: "vier"}, []int{5, 1, 3, 4}},
{map[int]string{1: "un"}, []int{1}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d: testing %v", i+1, tt.input), func(t *testing.T) {
result := Keys(tt.input)
assert.ElementsMatch(t, tt.expected, result)
})
}
}
func TestMissing(t *testing.T) {
tests := []struct {
source []string
input []string
expected []string
}{
{[]string{"a", "b", "c"}, []string{"c", "b", "a"}, []string{}},
{[]string{"a", "b", "c"}, []string{"c", "b"}, []string{"a"}},
{[]string{"a", "b", "c"}, []string{"c", "b", "a", "d"}, []string{}},
{[]string{}, []string{"c", "b"}, []string{}},
{[]string{"a", "b", "c"}, []string{}, []string{"a", "b", "c"}},
{[]string{"a", "b", "b", "c"}, []string{"a", "b"}, []string{"c"}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d: testing [%v] <-> [%v] == [%v]", i+1, strings.Join(tt.source, ", "), strings.Join(tt.input, ", "), strings.Join(tt.expected, ", ")), func(t *testing.T) {
result := Missing(tt.source, tt.input)
assert.Equal(t, tt.expected, result)
})
}
}
func TestAny(t *testing.T) {
always := func(s string) bool { return true }
never := func(s string) bool { return false }
assert.True(t, Any([]string{"a", "b", "c"}, always))
assert.False(t, Any([]string{}, always))
assert.False(t, Any(nil, always))
assert.False(t, Any([]string{"a", "b", "c"}, never))
assert.False(t, Any(nil, never))
}
func TestAnyKey(t *testing.T) {
always := func(s string) bool { return true }
never := func(s string) bool { return false }
assert.True(t, AnyKey(map[string]bool{"a": true, "b": false}, always))
assert.False(t, AnyKey(map[string]bool{}, always))
assert.False(t, AnyKey[string, bool](nil, always))
assert.False(t, AnyKey(map[string]bool{"a": true, "b": false}, never))
assert.False(t, AnyKey[string, bool](nil, never))
}
func TestAnyValue(t *testing.T) {
always := func(b bool) bool { return true }
never := func(b bool) bool { return false }
assert.True(t, AnyValue(map[string]bool{"a": true, "b": false}, always))
assert.False(t, AnyValue(map[string]bool{}, always))
assert.False(t, AnyValue[string](nil, always))
assert.False(t, AnyValue(map[string]bool{"a": true, "b": false}, never))
assert.False(t, AnyValue[string](nil, never))
}
func TestAnyItem(t *testing.T) {
always := func(s string, b bool) bool { return true }
never := func(s string, b bool) bool { return false }
assert.True(t, AnyItem(map[string]bool{"a": true, "b": false}, always))
assert.False(t, AnyItem(map[string]bool{}, always))
assert.False(t, AnyItem(nil, always))
assert.False(t, AnyItem(map[string]bool{"a": true, "b": false}, never))
assert.False(t, AnyItem(nil, never))
}
func TestConcat(t *testing.T) {
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f"}, Concat([]string{"a", "b"}, []string{"c"}, []string{"d", "e", "f"}))
assert.Equal(t, []string{"a"}, Concat([]string{"a"}))
assert.Equal(t, []string{"a"}, Concat([]string{}, nil, []string{"a"}))
assert.Equal(t, []string{}, Concat[string]())
}