This repository has been archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OptionalOrNull helper * Add files to .fernignore
- Loading branch information
Showing
3 changed files
with
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,7 @@ | |
|
||
README.md | ||
LICENSE | ||
|
||
# Temporarily ignored for the OptionalOrNull helper. | ||
optional.go | ||
optional_test.go |
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,55 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
core "github.com/hookdeck/hookdeck-go-sdk/core" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOptionalOrNull(t *testing.T) { | ||
t.Run("nil primitive", func(t *testing.T) { | ||
var value *string | ||
optional := OptionalOrNull(value) | ||
assert.Equal(t, &core.Optional[string]{Null: true}, optional) | ||
}) | ||
|
||
t.Run("zero primitive", func(t *testing.T) { | ||
var zero int | ||
optional := OptionalOrNull(&zero) | ||
assert.Equal(t, &core.Optional[int]{Value: zero}, optional) | ||
}) | ||
|
||
t.Run("non-zero primitive", func(t *testing.T) { | ||
value := "test" | ||
optional := OptionalOrNull(&value) | ||
assert.Equal(t, &core.Optional[string]{Value: value}, optional) | ||
}) | ||
|
||
t.Run("nil type", func(t *testing.T) { | ||
type foo struct { | ||
Name string | ||
} | ||
var zero *foo | ||
optional := OptionalOrNull(zero) | ||
assert.Equal(t, &core.Optional[foo]{Null: true}, optional) | ||
}) | ||
|
||
t.Run("zero type", func(t *testing.T) { | ||
type foo struct { | ||
Name string | ||
} | ||
zero := new(foo) | ||
optional := OptionalOrNull(zero) | ||
assert.Equal(t, &core.Optional[foo]{Value: *zero}, optional) | ||
}) | ||
|
||
t.Run("non-zero type", func(t *testing.T) { | ||
type foo struct { | ||
Name string | ||
} | ||
value := &foo{Name: "test"} | ||
optional := OptionalOrNull(value) | ||
assert.Equal(t, &core.Optional[foo]{Value: *value}, optional) | ||
}) | ||
} |