forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_manipulation_test.go
123 lines (92 loc) · 2.21 KB
/
type_manipulation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package lo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestToPtr(t *testing.T) {
is := assert.New(t)
result1 := ToPtr([]int{1, 2})
is.Equal(*result1, []int{1, 2})
}
func TestToSlicePtr(t *testing.T) {
is := assert.New(t)
str1 := "foo"
str2 := "bar"
result1 := ToSlicePtr([]string{str1, str2})
is.Equal(result1, []*string{&str1, &str2})
}
func TestToAnySlice(t *testing.T) {
is := assert.New(t)
in1 := []int{0, 1, 2, 3}
in2 := []int{}
out1 := ToAnySlice(in1)
out2 := ToAnySlice(in2)
is.Equal([]any{0, 1, 2, 3}, out1)
is.Equal([]any{}, out2)
}
func TestFromAnySlice(t *testing.T) {
is := assert.New(t)
is.NotPanics(func() {
out1, ok1 := FromAnySlice[string]([]any{"foobar", 42})
out2, ok2 := FromAnySlice[string]([]any{"foobar", "42"})
is.Equal([]string{}, out1)
is.False(ok1)
is.Equal([]string{"foobar", "42"}, out2)
is.True(ok2)
})
}
func TestEmpty(t *testing.T) {
is := assert.New(t)
//nolint:unused
type test struct {
foobar string
}
is.Empty(Empty[string]())
is.Empty(Empty[int64]())
is.Empty(Empty[test]())
is.Empty(Empty[chan string]())
}
func TestCoalesce(t *testing.T) {
is := assert.New(t)
newStr := func(v string) *string { return &v }
var nilStr *string
str1 := newStr("str1")
str2 := newStr("str2")
type structType struct {
field1 int
field2 float64
}
var zeroStruct structType
struct1 := structType{1, 1.0}
struct2 := structType{2, 2.0}
result1, ok1 := Coalesce[int]()
result2, ok2 := Coalesce(3)
result3, ok3 := Coalesce(nil, nilStr)
result4, ok4 := Coalesce(nilStr, str1)
result5, ok5 := Coalesce(nilStr, str1, str2)
result6, ok6 := Coalesce(str1, str2, nilStr)
result7, ok7 := Coalesce(0, 1, 2, 3)
result8, ok8 := Coalesce(zeroStruct)
result9, ok9 := Coalesce(zeroStruct, struct1)
result10, ok10 := Coalesce(zeroStruct, struct1, struct2)
is.Equal(0, result1)
is.False(ok1)
is.Equal(3, result2)
is.True(ok2)
is.Nil(result3)
is.False(ok3)
is.Equal(str1, result4)
is.True(ok4)
is.Equal(str1, result5)
is.True(ok5)
is.Equal(str1, result6)
is.True(ok6)
is.Equal(result7, 1)
is.True(ok7)
is.Equal(result8, zeroStruct)
is.False(ok8)
is.Equal(result9, struct1)
is.True(ok9)
is.Equal(result10, struct1)
is.True(ok10)
}