69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package cart
|
|
|
|
import "testing"
|
|
|
|
// equalOptional is the nullable-equality primitive shared by the AddItem
|
|
// merge site and any future field-comparison. Two call sites use it today
|
|
// with two distinct types — *uint32 (ParentId) and *string (StoreId) — so
|
|
// the test pins both via generic instantiation.
|
|
func TestEqualOptional(t *testing.T) {
|
|
t.Run("both nil is equal", func(t *testing.T) {
|
|
if !equalOptional[string](nil, nil) {
|
|
t.Error("both nil should be equal")
|
|
}
|
|
if !equalOptional[uint32](nil, nil) {
|
|
t.Error("both nil should be equal (uint32)")
|
|
}
|
|
})
|
|
|
|
t.Run("same address alias is equal", func(t *testing.T) {
|
|
s := "x"
|
|
if !equalOptional(&s, &s) {
|
|
t.Error("same-address *string should be equal")
|
|
}
|
|
v := uint32(5)
|
|
if !equalOptional(&v, &v) {
|
|
t.Error("same-address *uint32 should be equal")
|
|
}
|
|
})
|
|
|
|
t.Run("distinct addresses with equal values are equal", func(t *testing.T) {
|
|
a, b := "x", "x"
|
|
if !equalOptional(&a, &b) {
|
|
t.Error("equal strings at distinct addresses should be equal")
|
|
}
|
|
u, w := uint32(5), uint32(5)
|
|
if !equalOptional(&u, &w) {
|
|
t.Error("equal uint32 at distinct addresses should be equal")
|
|
}
|
|
})
|
|
|
|
t.Run("one nil, one set is not equal", func(t *testing.T) {
|
|
a := "x"
|
|
if equalOptional(&a, nil) {
|
|
t.Error("non-nil vs nil should differ (string)")
|
|
}
|
|
if equalOptional[string](nil, &a) {
|
|
t.Error("nil vs non-nil should differ (string)")
|
|
}
|
|
u := uint32(5)
|
|
if equalOptional(&u, nil) {
|
|
t.Error("non-nil vs nil should differ (uint32)")
|
|
}
|
|
if equalOptional[uint32](nil, &u) {
|
|
t.Error("nil vs non-nil should differ (uint32)")
|
|
}
|
|
})
|
|
|
|
t.Run("differing values are not equal", func(t *testing.T) {
|
|
a, b := "x", "y"
|
|
if equalOptional(&a, &b) {
|
|
t.Error("differing strings should differ")
|
|
}
|
|
u, w := uint32(5), uint32(6)
|
|
if equalOptional(&u, &w) {
|
|
t.Error("differing uint32 should differ")
|
|
}
|
|
})
|
|
}
|