tests: add backend tests
This commit is contained in:
53
backend/pkg/ptr/ptr_test.go
Normal file
53
backend/pkg/ptr/ptr_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package ptr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDerefOrZero(t *testing.T) {
|
||||
// Test with nil pointer
|
||||
var nilPtr *int
|
||||
result := DerefOrZero(nilPtr)
|
||||
assert.Equal(t, 0, result)
|
||||
|
||||
// Test with non-nil pointer
|
||||
value := 42
|
||||
ptr := &value
|
||||
result = DerefOrZero(ptr)
|
||||
assert.Equal(t, 42, result)
|
||||
|
||||
// Test with string
|
||||
var nilString *string
|
||||
resultStr := DerefOrZero(nilString)
|
||||
assert.Equal(t, "", resultStr)
|
||||
|
||||
// Test with non-nil string
|
||||
strValue := "hello"
|
||||
strPtr := &strValue
|
||||
resultStr = DerefOrZero(strPtr)
|
||||
assert.Equal(t, "hello", resultStr)
|
||||
}
|
||||
|
||||
func TestOf(t *testing.T) {
|
||||
// Test with int
|
||||
value := 42
|
||||
ptr := Of(value)
|
||||
assert.NotNil(t, ptr)
|
||||
assert.Equal(t, 42, *ptr)
|
||||
|
||||
// Test with string - using explicit typing
|
||||
var strPtr *string
|
||||
strValue := "hello"
|
||||
strPtr = Of(strValue)
|
||||
assert.NotNil(t, strPtr)
|
||||
assert.Equal(t, "hello", *strPtr)
|
||||
|
||||
// Test with bool - using explicit typing
|
||||
var boolPtr *bool
|
||||
boolValue := true
|
||||
boolPtr = Of(boolValue)
|
||||
assert.NotNil(t, boolPtr)
|
||||
assert.Equal(t, true, *boolPtr)
|
||||
}
|
||||
83
backend/pkg/slices/map_test.go
Normal file
83
backend/pkg/slices/map_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package slices
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
// Test with integers
|
||||
ints := []int{1, 2, 3, 4, 5}
|
||||
result := Map(ints, func(x int) int {
|
||||
return x * 2
|
||||
})
|
||||
assert.Equal(t, []int{2, 4, 6, 8, 10}, result)
|
||||
|
||||
// Test with strings
|
||||
strings := []string{"a", "b", "c"}
|
||||
resultStr := Map(strings, func(s string) string {
|
||||
return s + "_suffix"
|
||||
})
|
||||
assert.Equal(t, []string{"a_suffix", "b_suffix", "c_suffix"}, resultStr)
|
||||
|
||||
// Test with empty slice
|
||||
empty := []int{}
|
||||
resultEmpty := Map(empty, func(x int) int {
|
||||
return x * 2
|
||||
})
|
||||
assert.Equal(t, []int{}, resultEmpty)
|
||||
}
|
||||
|
||||
func TestFirst(t *testing.T) {
|
||||
// Test with non-empty slice
|
||||
ints := []int{1, 2, 3}
|
||||
item, found := First(ints)
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 1, item)
|
||||
|
||||
// Test with empty slice
|
||||
empty := []int{}
|
||||
item, found = First(empty)
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, 0, item)
|
||||
}
|
||||
|
||||
func TestLast(t *testing.T) {
|
||||
// Test with non-empty slice
|
||||
ints := []int{1, 2, 3}
|
||||
item, found := Last(ints)
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 3, item)
|
||||
|
||||
// Test with empty slice
|
||||
empty := []int{}
|
||||
item, found = Last(empty)
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, 0, item)
|
||||
}
|
||||
|
||||
func TestFindFirst(t *testing.T) {
|
||||
// Test with matching element
|
||||
ints := []int{1, 2, 3, 4, 5}
|
||||
item, found := FindFirst(ints, func(x int) bool {
|
||||
return x > 3
|
||||
})
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 4, item)
|
||||
|
||||
// Test with no matching element
|
||||
item, found = FindFirst(ints, func(x int) bool {
|
||||
return x > 10
|
||||
})
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, 0, item)
|
||||
|
||||
// Test with empty slice
|
||||
empty := []int{}
|
||||
item, found = FindFirst(empty, func(x int) bool {
|
||||
return x > 3
|
||||
})
|
||||
assert.False(t, found)
|
||||
assert.Equal(t, 0, item)
|
||||
}
|
||||
51
backend/pkg/values/values_test.go
Normal file
51
backend/pkg/values/values_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package values
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFirstNonZero(t *testing.T) {
|
||||
// Test with all zero values
|
||||
result := FirstNonZero(0, 0, 0)
|
||||
assert.Equal(t, 0, result)
|
||||
|
||||
// Test with first non-zero
|
||||
result = FirstNonZero(0, 5, 10)
|
||||
assert.Equal(t, 5, result)
|
||||
|
||||
// Test with middle non-zero
|
||||
result = FirstNonZero(0, 0, 15, 0, 20)
|
||||
assert.Equal(t, 15, result)
|
||||
|
||||
// Test with strings
|
||||
resultStr := FirstNonZero("", "hello", "")
|
||||
assert.Equal(t, "hello", resultStr)
|
||||
|
||||
// Test with empty strings (empty string is zero value for string)
|
||||
resultStr = FirstNonZero("", "", "")
|
||||
assert.Equal(t, "", resultStr)
|
||||
}
|
||||
|
||||
func TestCountNonZero(t *testing.T) {
|
||||
// Test with all zero values
|
||||
count := CountNonZero(0, 0, 0)
|
||||
assert.Equal(t, 0, count)
|
||||
|
||||
// Test with some non-zero values
|
||||
count = CountNonZero(0, 5, 10)
|
||||
assert.Equal(t, 2, count)
|
||||
|
||||
// Test with all non-zero values
|
||||
count = CountNonZero(1, 2, 3, 4)
|
||||
assert.Equal(t, 4, count)
|
||||
|
||||
// Test with strings
|
||||
count = CountNonZero("", "hello", "", "world")
|
||||
assert.Equal(t, 2, count)
|
||||
|
||||
// Test with empty slice
|
||||
count = CountNonZero[int]()
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
Reference in New Issue
Block a user