1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00
kube-arangodb/pkg/util/refs_test.go

197 lines
5.2 KiB
Go

//
// DISCLAIMER
//
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package util
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
)
func testRefs[T interface{}](t *testing.T, in, v1, def T) {
tp := reflect.TypeOf(in)
t.Run(tp.String(), func(t *testing.T) {
t.Run("New", func(t *testing.T) {
n := NewType[T](in)
tn := reflect.ValueOf(n)
require.Equal(t, reflect.Pointer, tn.Kind())
require.Equal(t, tp.Kind(), tn.Elem().Kind())
require.Equal(t, in, tn.Elem().Interface())
})
t.Run("NewOrNil", func(t *testing.T) {
t.Run("Nil", func(t *testing.T) {
n := NewTypeOrNil[T](nil)
tn := reflect.ValueOf(n)
require.Equal(t, reflect.Pointer, tn.Kind())
require.True(t, tn.IsNil())
})
t.Run("Value", func(t *testing.T) {
n := NewTypeOrNil[T](&in)
tn := reflect.ValueOf(n)
require.Equal(t, reflect.Pointer, tn.Kind())
require.False(t, tn.IsNil())
require.Equal(t, tp.Kind(), tn.Elem().Kind())
require.Equal(t, in, tn.Elem().Interface())
})
})
t.Run("Default", func(t *testing.T) {
t.Run("Ensure default", func(t *testing.T) {
var val T
require.Equal(t, def, val)
})
t.Run("With Input", func(t *testing.T) {
n := TypeOrDefault[T](&in)
tn := reflect.ValueOf(n)
require.Equal(t, tp.Kind(), tn.Kind())
require.Equal(t, in, tn.Interface())
})
t.Run("With Input & Default", func(t *testing.T) {
n := TypeOrDefault[T](&in, v1)
tn := reflect.ValueOf(n)
require.Equal(t, tp.Kind(), tn.Kind())
require.Equal(t, in, tn.Interface())
})
t.Run("With Nil & Default", func(t *testing.T) {
n := TypeOrDefault[T](nil, v1)
tn := reflect.ValueOf(n)
require.Equal(t, tp.Kind(), tn.Kind())
require.Equal(t, v1, tn.Interface())
})
t.Run("With Nil", func(t *testing.T) {
n := TypeOrDefault[T](nil)
tn := reflect.ValueOf(n)
require.Equal(t, tp.Kind(), tn.Kind())
require.Equal(t, def, tn.Interface())
})
})
})
}
func Test_Refs(t *testing.T) {
testRefs[string](t, "test", "otherValue", "")
testRefs[int](t, 777, 555, 0)
testRefs[int32](t, 777, 555, 0)
testRefs[int64](t, 777, 555, 0)
testRefs[uint16](t, 777, 555, 0)
testRefs[bool](t, false, true, false)
testRefs[time.Duration](t, time.Duration(500), time.Duration(1500), time.Duration(0))
testRefs[core.PullPolicy](t, core.PullAlways, core.PullNever, "")
}
func generateConditionalP0Function[T interface{}](ret T, ok bool) *T {
return CheckConditionalNil[T](func() (T, bool) {
return ret, ok
})
}
func generateConditionalP1Function[T interface{}](ret T, ok bool) *T {
return CheckConditionalP1Nil[T, int](func(in int) (T, bool) {
return ret, ok
}, 1)
}
func Test_CheckConditionalNil(t *testing.T) {
t.Run("P0", func(t *testing.T) {
t.Run("Nil if false", func(t *testing.T) {
v := generateConditionalP0Function(0, false)
require.Nil(t, v)
})
t.Run("NotNil if true", func(t *testing.T) {
v := generateConditionalP0Function(0, true)
require.NotNil(t, v)
require.EqualValues(t, 0, *v)
})
})
t.Run("P1", func(t *testing.T) {
t.Run("Nil if false", func(t *testing.T) {
v := generateConditionalP1Function(0, false)
require.Nil(t, v)
})
t.Run("NotNil if true", func(t *testing.T) {
v := generateConditionalP1Function(0, true)
require.NotNil(t, v)
require.EqualValues(t, 0, *v)
})
})
}
func Test_FirstNotDefault(t *testing.T) {
t.Run("String", func(t *testing.T) {
require.Equal(t, "", FirstNotDefault[string]())
require.Equal(t, "", FirstNotDefault[string](""))
require.Equal(t, "test", FirstNotDefault[string]("", "test"))
require.Equal(t, "test1", FirstNotDefault[string]("test1", "test"))
})
t.Run("Int", func(t *testing.T) {
require.Equal(t, 0, FirstNotDefault[int]())
require.Equal(t, 0, FirstNotDefault[int](0))
require.Equal(t, 1, FirstNotDefault[int](0, 1))
require.Equal(t, 2, FirstNotDefault[int](2, 1))
})
t.Run("Structs", func(t *testing.T) {
type z struct {
v int
}
require.Equal(t, z{}, FirstNotDefault[z]())
require.Equal(t, z{}, FirstNotDefault[z](z{}))
require.Equal(t, z{4}, FirstNotDefault[z](z{}, z{4}))
require.Equal(t, z{1}, FirstNotDefault[z](z{1}, z{4}))
})
t.Run("Pointers", func(t *testing.T) {
type z struct {
v int
}
var z1, z2 z
z1 = z{1}
z2 = z{2}
require.Equal(t, (*z)(nil), FirstNotDefault[*z]())
require.Equal(t, &z1, FirstNotDefault[*z](&z1))
require.NotEqual(t, nil, FirstNotDefault[*z](&z1))
require.NotEqual(t, &z2, FirstNotDefault[*z](&z1))
require.Equal(t, &z1, FirstNotDefault[*z](nil, &z1))
require.Equal(t, &z2, FirstNotDefault[*z](&z2, &z1))
})
}