mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-31 17:51:33 +02:00
fn: fix UnwrapOrFail semantics
This commit is contained in:
15
fn/option.go
15
fn/option.go
@@ -1,6 +1,10 @@
|
|||||||
package fn
|
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
|
// Option[A] represents a value which may or may not be there. This is very
|
||||||
// often preferable to nil-able pointers.
|
// often preferable to nil-able pointers.
|
||||||
@@ -61,16 +65,11 @@ func (o Option[A]) UnwrapOrFunc(f func() A) A {
|
|||||||
func (o Option[A]) UnwrapOrFail(t *testing.T) A {
|
func (o Option[A]) UnwrapOrFail(t *testing.T) A {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
if o.isSome {
|
require.True(t, o.isSome, "Option[%T] was None()", o.some)
|
||||||
|
|
||||||
return o.some
|
return o.some
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Fatalf("Option[%T] was None()", o.some)
|
|
||||||
|
|
||||||
var zero A
|
|
||||||
return zero
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnwrapOrErr is used to extract a value from an option, if the option is
|
// UnwrapOrErr is used to extract a value from an option, if the option is
|
||||||
// empty, then the specified error is returned directly.
|
// empty, then the specified error is returned directly.
|
||||||
func (o Option[A]) UnwrapOrErr(err error) (A, error) {
|
func (o Option[A]) UnwrapOrErr(err error) (A, error) {
|
||||||
|
11
fn/option_test.go
Normal file
11
fn/option_test.go
Normal file
@@ -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)
|
||||||
|
}
|
12
fn/result.go
12
fn/result.go
@@ -3,6 +3,8 @@ package fn
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Result represents a value that can either be a success (T) or an error.
|
// 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 {
|
func (r Result[T]) UnwrapOrFail(t *testing.T) T {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
if r.IsErr() {
|
require.True(
|
||||||
t.Fatalf("Result[%T] contained error: %v", r.left, r.right)
|
t, r.IsOk(), "Result[%T] contained error: %v", r.left, r.right,
|
||||||
}
|
)
|
||||||
|
|
||||||
var zero T
|
return r.left
|
||||||
|
|
||||||
return zero
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlatMap applies a function that returns a Result to the success value if it
|
// FlatMap applies a function that returns a Result to the success value if it
|
||||||
|
11
fn/result_test.go
Normal file
11
fn/result_test.go
Normal file
@@ -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)
|
||||||
|
}
|
Reference in New Issue
Block a user