-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lucarin91/lucarin91/add-new-utility-functions
feat: add new utility functions
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package f_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
f "go.bug.st/f" | ||
) | ||
|
||
func TestMust(t *testing.T) { | ||
t.Run("no error", func(t *testing.T) { | ||
assert.Equal(t, 12, f.Must(12, nil)) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
want := fmt.Errorf("this is an error") | ||
assert.PanicsWithValue(t, want.Error(), func() { | ||
f.Must(0, want) | ||
}) | ||
}) | ||
} | ||
|
||
func TestAssert(t *testing.T) { | ||
t.Run("true", func(_ *testing.T) { | ||
f.Assert(true, "should not panic") | ||
}) | ||
|
||
t.Run("false", func(t *testing.T) { | ||
assert.PanicsWithValue(t, "should panic", func() { | ||
f.Assert(false, "should panic") | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package f | ||
|
||
// Ptr returns a pointer to v. | ||
func Ptr[T any](v T) *T { | ||
return &v | ||
} | ||
|
||
// UnwrapOrDefault returns the ptr value if it is not nil, otherwise returns the zero value. | ||
func UnwrapOrDefault[T any](ptr *T) T { | ||
if ptr != nil { | ||
return *ptr | ||
} | ||
|
||
var zero T | ||
return zero | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package f_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
f "go.bug.st/f" | ||
) | ||
|
||
func TestPtr(t *testing.T) { | ||
t.Run("int", func(t *testing.T) { | ||
assert.Equal(t, 12, *f.Ptr(12)) | ||
}) | ||
|
||
t.Run("string", func(t *testing.T) { | ||
assert.Equal(t, "hello", *f.Ptr("hello")) | ||
}) | ||
} | ||
|
||
func TestUnwrapOrDefault(t *testing.T) { | ||
t.Run("not nil", func(t *testing.T) { | ||
given := 12 | ||
assert.Equal(t, 12, f.UnwrapOrDefault(&given)) | ||
}) | ||
|
||
t.Run("nil", func(t *testing.T) { | ||
assert.Equal(t, "", f.UnwrapOrDefault[string](nil)) | ||
}) | ||
} |