Files
goext/langext/coalesce_test.go
T
Mikescher 02d6894ec6
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s
[🤖] Add Unit-Tests
2026-04-27 16:31:29 +02:00

135 lines
3.3 KiB
Go

package langext
import (
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
"testing"
"time"
)
type stringerImpl struct{ v string }
func (s stringerImpl) String() string { return s.v }
func TestCoalesceWithValue(t *testing.T) {
v := 42
tst.AssertEqual(t, Coalesce(&v, 0), 42)
}
func TestCoalesceWithNil(t *testing.T) {
var p *int
tst.AssertEqual(t, Coalesce(p, 99), 99)
}
func TestCoalesceOpt(t *testing.T) {
v := 1
w := 2
tst.AssertDeRefEqual(t, CoalesceOpt(&v, &w), 1)
var p *int
tst.AssertDeRefEqual(t, CoalesceOpt(p, &w), 2)
tst.AssertPtrEqual(t, CoalesceOpt[int](nil, nil), nil)
}
func TestCoalesce3(t *testing.T) {
v := 1
w := 2
tst.AssertEqual(t, Coalesce3(&v, &w, 99), 1)
tst.AssertEqual(t, Coalesce3[int](nil, &w, 99), 2)
tst.AssertEqual(t, Coalesce3[int](nil, nil, 99), 99)
}
func TestCoalesce3Opt(t *testing.T) {
v := 1
tst.AssertDeRefEqual(t, Coalesce3Opt[int](nil, nil, &v), 1)
tst.AssertPtrEqual(t, Coalesce3Opt[int](nil, nil, nil), nil)
}
func TestCoalesce4(t *testing.T) {
v := 1
w := 2
x := 3
tst.AssertEqual(t, Coalesce4(&v, &w, &x, 99), 1)
tst.AssertEqual(t, Coalesce4[int](nil, &w, &x, 99), 2)
tst.AssertEqual(t, Coalesce4[int](nil, nil, &x, 99), 3)
tst.AssertEqual(t, Coalesce4[int](nil, nil, nil, 99), 99)
}
func TestCoalesce4Opt(t *testing.T) {
v := 4
tst.AssertDeRefEqual(t, Coalesce4Opt[int](nil, nil, nil, &v), 4)
tst.AssertPtrEqual(t, Coalesce4Opt[int](nil, nil, nil, nil), nil)
}
func TestCoalesceString(t *testing.T) {
s := "hello"
tst.AssertEqual(t, CoalesceString(&s, "def"), "hello")
tst.AssertEqual(t, CoalesceString(nil, "def"), "def")
}
func TestCoalesceInt(t *testing.T) {
v := 5
tst.AssertEqual(t, CoalesceInt(&v, 99), 5)
tst.AssertEqual(t, CoalesceInt(nil, 99), 99)
}
func TestCoalesceInt32(t *testing.T) {
v := int32(7)
tst.AssertEqual(t, CoalesceInt32(&v, 99), int32(7))
tst.AssertEqual(t, CoalesceInt32(nil, 99), int32(99))
}
func TestCoalesceBool(t *testing.T) {
v := true
tst.AssertEqual(t, CoalesceBool(&v, false), true)
tst.AssertEqual(t, CoalesceBool(nil, true), true)
tst.AssertEqual(t, CoalesceBool(nil, false), false)
}
func TestCoalesceTime(t *testing.T) {
now := time.Now()
def := time.Unix(0, 0)
tst.AssertEqual(t, CoalesceTime(&now, def), now)
tst.AssertEqual(t, CoalesceTime(nil, def), def)
}
func TestCoalesceStringer(t *testing.T) {
s := stringerImpl{v: "hi"}
tst.AssertEqual(t, CoalesceStringer(s, "def"), "hi")
var nilStringer *stringerImpl
tst.AssertEqual(t, CoalesceStringer(nilStringer, "def"), "def")
}
func TestCoalesceDefault(t *testing.T) {
tst.AssertEqual(t, CoalesceDefault(0, 99), 99)
tst.AssertEqual(t, CoalesceDefault(5, 99), 5)
tst.AssertEqual(t, CoalesceDefault("", "def"), "def")
tst.AssertEqual(t, CoalesceDefault("v", "def"), "v")
}
func TestSafeCastMatching(t *testing.T) {
var v any = "hello"
tst.AssertEqual(t, SafeCast(v, "default"), "hello")
}
func TestSafeCastMismatch(t *testing.T) {
var v any = 42
tst.AssertEqual(t, SafeCast(v, "default"), "default")
}
func TestSafeCastNil(t *testing.T) {
tst.AssertEqual(t, SafeCast(nil, 99), 99)
}
func TestCoalesceDblPtrWithValue(t *testing.T) {
v := 1
pv := &v
tst.AssertDeRefEqual(t, CoalesceDblPtr(&pv, nil), 1)
}
func TestCoalesceDblPtrFallback(t *testing.T) {
w := 2
tst.AssertDeRefEqual(t, CoalesceDblPtr[int](nil, &w), 2)
}