Skip to content

Commit ad0ce88

Browse files
author
New year
committed
addtypes: adding test for type Any
1 parent 1de4876 commit ad0ce88

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

any_gen test.go

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package option_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
"github.com/vmihailenco/msgpack/v5"
10+
)
11+
12+
func TestAny_IsSome(t *testing.T) {
13+
t.Parallel()
14+
15+
t.Run("some", func(t *testing.T) {
16+
t.Parallel()
17+
18+
someAny := option.SomeAny("aaaaaa+++++")
19+
assert.True(t, someAny.IsSome())
20+
})
21+
22+
t.Run("none", func(t *testing.T) {
23+
t.Parallel()
24+
25+
emptyAny := option.NoneAny()
26+
assert.False(t, emptyAny.IsSome())
27+
})
28+
}
29+
30+
func TestAny_IsZero(t *testing.T) {
31+
t.Parallel()
32+
33+
t.Run("some", func(t *testing.T) {
34+
t.Parallel()
35+
36+
someAny := option.SomeAny(12232.777777777)
37+
assert.False(t, someAny.IsZero())
38+
})
39+
40+
t.Run("none", func(t *testing.T) {
41+
t.Parallel()
42+
43+
emptyAny := option.NoneAny()
44+
assert.True(t, emptyAny.IsZero())
45+
})
46+
}
47+
48+
func TestAny_IsNil(t *testing.T) {
49+
t.Parallel()
50+
51+
t.Run("some", func(t *testing.T) {
52+
t.Parallel()
53+
54+
someAny := option.SomeAny(777.111111)
55+
assert.False(t, someAny.IsNil())
56+
})
57+
58+
t.Run("none", func(t *testing.T) {
59+
t.Parallel()
60+
61+
emptyAny := option.NoneAny()
62+
assert.True(t, emptyAny.IsNil())
63+
})
64+
}
65+
66+
func TestAny_Get(t *testing.T) {
67+
t.Parallel()
68+
69+
t.Run("some", func(t *testing.T) {
70+
t.Parallel()
71+
72+
someAny := option.SomeAny("lllllllll")
73+
val, ok := someAny.Get()
74+
require.True(t, ok)
75+
assert.EqualValues(t, true, val)
76+
})
77+
78+
t.Run("none", func(t *testing.T) {
79+
t.Parallel()
80+
81+
emptyAny := option.NoneAny()
82+
_, ok := emptyAny.Get()
83+
require.False(t, ok)
84+
})
85+
}
86+
87+
func TestAny_MustGet(t *testing.T) {
88+
t.Parallel()
89+
90+
t.Run("some", func(t *testing.T) {
91+
t.Parallel()
92+
93+
someAny := option.SomeAny(1111.1000000)
94+
assert.EqualValues(t, true, someAny.MustGet())
95+
})
96+
97+
t.Run("none", func(t *testing.T) {
98+
t.Parallel()
99+
100+
emptyAny := option.NoneAny()
101+
assert.Panics(t, func() {
102+
emptyAny.MustGet()
103+
})
104+
})
105+
}
106+
107+
func TestAny_Unwrap(t *testing.T) {
108+
t.Parallel()
109+
110+
t.Run("some", func(t *testing.T) {
111+
t.Parallel()
112+
113+
someAny := option.SomeAny("HH7777")
114+
assert.EqualValues(t, true, someAny.Unwrap())
115+
})
116+
117+
t.Run("none", func(t *testing.T) {
118+
t.Parallel()
119+
120+
emptyAny := option.NoneAny()
121+
assert.NotPanics(t, func() {
122+
emptyAny.Unwrap()
123+
})
124+
})
125+
}
126+
127+
func TestAny_UnwrapOr(t *testing.T) {
128+
t.Parallel()
129+
130+
t.Run("some", func(t *testing.T) {
131+
t.Parallel()
132+
133+
someAny := option.SomeAny("(((09,111")
134+
assert.EqualValues(t, true, someAny.UnwrapOr(false))
135+
})
136+
137+
t.Run("none", func(t *testing.T) {
138+
t.Parallel()
139+
140+
emptyAny := option.NoneAny()
141+
assert.EqualValues(t, false, emptyAny.UnwrapOr(false))
142+
})
143+
}
144+
145+
func TestAny_UnwrapOrElse(t *testing.T) {
146+
t.Parallel()
147+
148+
t.Run("some", func(t *testing.T) {
149+
t.Parallel()
150+
151+
someAny := option.SomeAny(34534534)
152+
assert.EqualValues(t, true, someAny.UnwrapOrElse(func() any {
153+
return false
154+
}))
155+
})
156+
157+
t.Run("none", func(t *testing.T) {
158+
t.Parallel()
159+
160+
emptyAny := option.NoneAny()
161+
assert.EqualValues(t, false, emptyAny.UnwrapOrElse(func() any {
162+
return false
163+
}))
164+
})
165+
}
166+
167+
func TestAny_EncodeDecodeMsgpack(t *testing.T) {
168+
t.Parallel()
169+
170+
t.Run("some", func(t *testing.T) {
171+
t.Parallel()
172+
173+
var buf bytes.Buffer
174+
175+
enc := msgpack.NewEncoder(&buf)
176+
dec := msgpack.NewDecoder(&buf)
177+
178+
someAny := option.SomeAny(222222.11111)
179+
err := someAny.EncodeMsgpack(enc)
180+
require.NoError(t, err)
181+
182+
var unmarshaled option.Any
183+
err = unmarshaled.DecodeMsgpack(dec)
184+
require.NoError(t, err)
185+
assert.True(t, unmarshaled.IsSome())
186+
assert.EqualValues(t, true, unmarshaled.Unwrap())
187+
})
188+
189+
t.Run("none", func(t *testing.T) {
190+
t.Parallel()
191+
192+
var buf bytes.Buffer
193+
194+
enc := msgpack.NewEncoder(&buf)
195+
dec := msgpack.NewDecoder(&buf)
196+
197+
emptyAny := option.NoneAny()
198+
err := emptyAny.EncodeMsgpack(enc)
199+
require.NoError(t, err)
200+
201+
var unmarshaled option.Any
202+
err = unmarshaled.DecodeMsgpack(dec)
203+
204+
require.NoError(t, err)
205+
assert.False(t, unmarshaled.IsSome())
206+
})
207+
}

0 commit comments

Comments
 (0)