@@ -2,10 +2,11 @@ package option
22
33import (
44 "fmt"
5- "github.com/initdc/types/result"
6- "github.com/stretchr/testify/assert"
75 "strconv"
86 "testing"
7+
8+ "github.com/initdc/types/result"
9+ "github.com/stretchr/testify/assert"
910)
1011
1112func TestSome (t * testing.T ) {
@@ -267,6 +268,25 @@ func TestOrElse(t *testing.T) {
267268 assert .Equal (t , None [int ]().OrElse (nobody ), None [int ]())
268269}
269270
271+ func TestTryOr (t * testing.T ) {
272+ f := func (x int ) float32 { return float32 (x ) * 2 }
273+ x := Some (2 )
274+ y := None [int ]()
275+
276+ assert .Equal (t , TryOr (x , f , "bad" ), result.Ok [float32 , string ](4.0 ))
277+ assert .Equal (t , TryOr (y , f , "bad" ), result.Err [float32 , string ]("bad" ))
278+ }
279+
280+ func TestTryOrElse (t * testing.T ) {
281+ f := func (x int ) float32 { return float32 (x ) * 2 }
282+ closure := func () result.Result [float32 , string ] { return result.Err [float32 , string ]("bad" ) }
283+ x := Some (2 )
284+ y := None [int ]()
285+
286+ assert .Equal (t , TryOrElse (x , f , closure ), result.Ok [float32 , string ](4.0 ))
287+ assert .Equal (t , TryOrElse (y , f , closure ), result.Err [float32 , string ]("bad" ))
288+ }
289+
270290func TestXor (t * testing.T ) {
271291 x := Some (2 )
272292 y := None [int ]()
0 commit comments