Skip to content

Commit 9d9e575

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

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

any_gen test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestAny_Get(t *testing.T) {
7272
someAny := option.SomeAny("lllllllll")
7373
val, ok := someAny.Get()
7474
require.True(t, ok)
75-
assert.EqualValues(t, true, val)
75+
assert.EqualValues(t, "lllllllll", val)
7676
})
7777

7878
t.Run("none", func(t *testing.T) {
@@ -91,7 +91,7 @@ func TestAny_MustGet(t *testing.T) {
9191
t.Parallel()
9292

9393
someAny := option.SomeAny(1111.1000000)
94-
assert.EqualValues(t, true, someAny.MustGet())
94+
assert.EqualValues(t, 1111.1000000, someAny.MustGet())
9595
})
9696

9797
t.Run("none", func(t *testing.T) {
@@ -110,8 +110,8 @@ func TestAny_Unwrap(t *testing.T) {
110110
t.Run("some", func(t *testing.T) {
111111
t.Parallel()
112112

113-
someAny := option.SomeAny("HH7777")
114-
assert.EqualValues(t, true, someAny.Unwrap())
113+
someAny := option.SomeAny("HH77771111111111111111111111111111111111111111111111111111111111111111111111")
114+
assert.EqualValues(t, "HH77771111111111111111111111111111111111111111111111111111111111111111111111", someAny.Unwrap())
115115
})
116116

117117
t.Run("none", func(t *testing.T) {
@@ -131,14 +131,14 @@ func TestAny_UnwrapOr(t *testing.T) {
131131
t.Parallel()
132132

133133
someAny := option.SomeAny("(((09,111")
134-
assert.EqualValues(t, true, someAny.UnwrapOr(false))
134+
assert.EqualValues(t, "(((09,111", someAny.UnwrapOr(111111))
135135
})
136136

137137
t.Run("none", func(t *testing.T) {
138138
t.Parallel()
139139

140140
emptyAny := option.NoneAny()
141-
assert.EqualValues(t, false, emptyAny.UnwrapOr(false))
141+
assert.EqualValues(t, 11111.8880, emptyAny.UnwrapOr(11111.8880))
142142
})
143143
}
144144

@@ -149,17 +149,17 @@ func TestAny_UnwrapOrElse(t *testing.T) {
149149
t.Parallel()
150150

151151
someAny := option.SomeAny(34534534)
152-
assert.EqualValues(t, true, someAny.UnwrapOrElse(func() any {
153-
return false
152+
assert.EqualValues(t, 34534534, someAny.UnwrapOrElse(func() any {
153+
return "EXAMPLE!!!"
154154
}))
155155
})
156156

157157
t.Run("none", func(t *testing.T) {
158158
t.Parallel()
159159

160160
emptyAny := option.NoneAny()
161-
assert.EqualValues(t, false, emptyAny.UnwrapOrElse(func() any {
162-
return false
161+
assert.EqualValues(t, 145, emptyAny.UnwrapOrElse(func() any {
162+
return 145
163163
}))
164164
})
165165
}
@@ -183,7 +183,7 @@ func TestAny_EncodeDecodeMsgpack(t *testing.T) {
183183
err = unmarshaled.DecodeMsgpack(dec)
184184
require.NoError(t, err)
185185
assert.True(t, unmarshaled.IsSome())
186-
assert.EqualValues(t, true, unmarshaled.Unwrap())
186+
assert.EqualValues(t, 222222.11111, unmarshaled.Unwrap())
187187
})
188188

189189
t.Run("none", func(t *testing.T) {

any_gen.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ func (o Any) Unwrap() any {
5151
return o.value
5252
}
5353

54-
func (o Any) UnwrapOr(defaultValue func() any) any {
54+
func (o Any) UnwrapOr(defaultValue any) any {
55+
if o.exists {
56+
return o.value
57+
}
58+
59+
return defaultValue
60+
}
61+
62+
func (o Any) UnwrapOrElse(defaultValue func() any) any {
5563
if o.exists {
5664
return o.value
5765
}

0 commit comments

Comments
 (0)