diff --git a/fn/option.go b/fn/option.go index 797f3a0ff..b1f2e237b 100644 --- a/fn/option.go +++ b/fn/option.go @@ -1,6 +1,10 @@ package fn -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/require" +) // Option[A] represents a value which may or may not be there. This is very // often preferable to nil-able pointers. @@ -61,14 +65,9 @@ func (o Option[A]) UnwrapOrFunc(f func() A) A { func (o Option[A]) UnwrapOrFail(t *testing.T) A { t.Helper() - if o.isSome { - return o.some - } + require.True(t, o.isSome, "Option[%T] was None()", o.some) - t.Fatalf("Option[%T] was None()", o.some) - - var zero A - return zero + return o.some } // UnwrapOrErr is used to extract a value from an option, if the option is diff --git a/fn/option_test.go b/fn/option_test.go new file mode 100644 index 000000000..53a924827 --- /dev/null +++ b/fn/option_test.go @@ -0,0 +1,11 @@ +package fn + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptionUnwrapOrFail(t *testing.T) { + require.Equal(t, Some(1).UnwrapOrFail(t), 1) +} diff --git a/fn/result.go b/fn/result.go index 93d2dd7d6..e94675e6e 100644 --- a/fn/result.go +++ b/fn/result.go @@ -3,6 +3,8 @@ package fn import ( "fmt" "testing" + + "github.com/stretchr/testify/require" ) // Result represents a value that can either be a success (T) or an error. @@ -114,13 +116,11 @@ func (r Result[T]) UnwrapOrElse(f func() T) T { func (r Result[T]) UnwrapOrFail(t *testing.T) T { t.Helper() - if r.IsErr() { - t.Fatalf("Result[%T] contained error: %v", r.left, r.right) - } + require.True( + t, r.IsOk(), "Result[%T] contained error: %v", r.left, r.right, + ) - var zero T - - return zero + return r.left } // FlatMap applies a function that returns a Result to the success value if it diff --git a/fn/result_test.go b/fn/result_test.go new file mode 100644 index 000000000..38ebed84a --- /dev/null +++ b/fn/result_test.go @@ -0,0 +1,11 @@ +package fn + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestResultUnwrapOrFail(t *testing.T) { + require.Equal(t, Ok(1).UnwrapOrFail(t), 1) +}