Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ensure/ensure.go
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 9 additions & 5 deletions tests/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/sagmor/fun/builder"
"github.com/sagmor/fun/ensure"
)

type testBuildableObject struct {
Expand All @@ -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) {
Expand Down