Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Sep 24, 2024
1 parent 659886a commit a915fcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"cmp"
"context"
"fmt"
"log/slog"
"reflect"
"slices"
Expand Down Expand Up @@ -80,6 +81,18 @@ func ToPtrs[T any](input []T) []*T {
return results
}

// PointersToValues converts the pointer slice to value slice
func PointersToValues[T any](input []*T) ([]T, error) {
results := make([]T, len(input))
for i, v := range input {
if IsNil(v) {
return nil, fmt.Errorf("element at %d must not be nil", i)
}
results[i] = *v
}
return results, nil
}

// UnwrapPointerFromReflectValue unwraps pointers from the reflect value
func UnwrapPointerFromReflectValue(reflectValue reflect.Value) (reflect.Value, bool) {
for reflectValue.Kind() == reflect.Pointer {
Expand Down
9 changes: 8 additions & 1 deletion utils/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import (
)

func TestToPtrs(t *testing.T) {
assertDeepEqual(t, []*string{ToPtr("a"), ToPtr("b"), ToPtr("c")}, ToPtrs([]string{"a", "b", "c"}))
_, err := PointersToValues([]*string{ToPtr(""), nil})
assertError(t, err, "element at 1 must not be nil")
input, err := PointersToValues(ToPtrs([]string{"a", "b", "c"}))
assertNoError(t, err)
expected, err := PointersToValues([]*string{ToPtr("a"), ToPtr("b"), ToPtr("c")})
assertNoError(t, err)

assertDeepEqual(t, expected, input)
}

func TestIsDebug(t *testing.T) {
Expand Down

0 comments on commit a915fcb

Please sign in to comment.