diff --git a/ensure/ensure.go b/ensure/ensure.go new file mode 100644 index 0000000..95d0241 --- /dev/null +++ b/ensure/ensure.go @@ -0,0 +1,16 @@ +package ensure + +import ( + "testing" + + "github.com/sagmor/fun" +) + +// Value extracts a result Value or halts the test. +func Value[T any](t *testing.T, r fun.Result[T]) T { + if r.IsFailure() { + t.Fatal(r.Error()) + } + + return r.RequireValue() +} diff --git a/tests/builder_test.go b/tests/builder_test.go index addc4b0..54f19e2 100644 --- a/tests/builder_test.go +++ b/tests/builder_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/sagmor/fun/builder" + "github.com/sagmor/fun/ensure" ) type testBuildableObject struct { @@ -19,11 +20,14 @@ func TestBuildReturnsAnObject(t *testing.T) { } func TestBuildExecutesOptions(t *testing.T) { - o := builder.Build(func(o *testBuildableObject) error { - o.Name = "other_name" - return nil - }) - assert.Equal(t, "other_name", o.RequireValue().Name) + o := ensure.Value(t, + builder.Build(func(o *testBuildableObject) error { + o.Name = "other_name" + return nil + }), + ) + + assert.Equal(t, "other_name", o.Name) } func TestBuildPassThroughOptionErrors(t *testing.T) {