64 lines
1.0 KiB
Go
64 lines
1.0 KiB
Go
package generics
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPtr(t *testing.T) {
|
|
num := new(int)
|
|
*num = 8
|
|
|
|
require.Equal(t, num, *Ptr(num))
|
|
}
|
|
|
|
func TestVal(t *testing.T) {
|
|
num := new(int)
|
|
*num = 8
|
|
|
|
tests := map[string]struct {
|
|
input *int
|
|
expectedResult int
|
|
}{
|
|
"non-nil pointer": {
|
|
input: num,
|
|
expectedResult: *num,
|
|
},
|
|
"nil pointer": {},
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
name, tc := name, tc
|
|
t.Run(name, func(t *testing.T) {
|
|
require.Equal(t, tc.expectedResult, Val(tc.input))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFallback(t *testing.T) {
|
|
num := new(int)
|
|
*num = 8
|
|
fallback := 9
|
|
|
|
tests := map[string]struct {
|
|
input *int
|
|
expectedResult int
|
|
}{
|
|
"non-nil pointer": {
|
|
input: num,
|
|
expectedResult: *num,
|
|
},
|
|
"nil pointer": {
|
|
expectedResult: fallback,
|
|
},
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
name, tc := name, tc
|
|
t.Run(name, func(t *testing.T) {
|
|
require.Equal(t, tc.expectedResult, Fallback(tc.input, tc.expectedResult))
|
|
})
|
|
}
|
|
}
|