-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add setenv and error support (#85)
Signed-off-by: Tronje Krop <[email protected]>
- Loading branch information
Tronje Krop
committed
Sep 21, 2024
1 parent
272fdce
commit 1699ddc
Showing
10 changed files
with
262 additions
and
49 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 |
---|---|---|
@@ -1 +1 @@ | ||
0.0.14 | ||
0.0.15 |
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
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
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,52 @@ | ||
package test | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// Error creates an Accessor for the given error to access and modify its | ||
// unexported fields by field name. | ||
// | ||
// Example: | ||
// | ||
// err := test.Error(errors.New("error message")).Set("text", "new message").Get("") | ||
// fmt.Println(err.Error()) // Output: new message | ||
// | ||
// err := test.Error(errors.New("error message")).Set("text", "new message").Get("text") | ||
// fmt.Println(err) // Output: new message | ||
func Error(err error) *Accessor[error] { | ||
return NewAccessor[error](err) | ||
} | ||
|
||
// Accessor allows you to access and modify unexported fields of a struct. | ||
type Accessor[T any] struct { | ||
target T | ||
} | ||
|
||
// NewAccessor creates a generic accessor for the given target. | ||
func NewAccessor[T any](target T) *Accessor[T] { | ||
return &Accessor[T]{target: target} | ||
} | ||
|
||
// Set sets the value of the accessor target's field with the given name. | ||
func (a *Accessor[T]) Set(name string, value any) *Accessor[T] { | ||
field := reflect.ValueOf(a.target).Elem().FieldByName(name) | ||
// #nosec G103,G115 // This is a safe use of unsafe.Pointer. | ||
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). | ||
Elem().Set(reflect.ValueOf(value)) | ||
|
||
return a | ||
} | ||
|
||
// Get returns the value of the field with the given name. If the name is empty, | ||
// it returns the accessor target itself. | ||
func (a *Accessor[T]) Get(name string) any { | ||
if name == "" { | ||
return a.target | ||
} | ||
field := reflect.ValueOf(a.target).Elem().FieldByName(name) | ||
// #nosec G103,G115 // This is a safe use of unsafe.Pointer. | ||
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). | ||
Elem().Interface() | ||
} |
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,53 @@ | ||
package test_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tkrop/go-testing/test" | ||
) | ||
|
||
type testErrorParam struct { | ||
error error | ||
setup func(*test.Accessor[error]) | ||
test func(test.Test, *test.Accessor[error]) | ||
} | ||
|
||
var testErrorParams = map[string]testErrorParam{ | ||
"test get": { | ||
error: errors.New("test get"), | ||
test: func(t test.Test, a *test.Accessor[error]) { | ||
assert.Equal(t, "test get", a.Get("s")) | ||
assert.Equal(t, errors.New("test get"), a.Get("")) | ||
}, | ||
}, | ||
|
||
"test set": { | ||
error: errors.New("test set"), | ||
setup: func(a *test.Accessor[error]) { | ||
a.Set("s", "test set first"). | ||
Set("s", "test set final") | ||
}, | ||
test: func(t test.Test, a *test.Accessor[error]) { | ||
assert.Equal(t, "test set final", a.Get("s")) | ||
assert.Equal(t, errors.New("test set final"), a.Get("")) | ||
}, | ||
}, | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
test.Map(t, testErrorParams). | ||
Run(func(t test.Test, param testErrorParam) { | ||
// Given | ||
accessor := test.Error(param.error) | ||
|
||
// When | ||
if param.setup != nil { | ||
param.setup(accessor) | ||
} | ||
|
||
// The | ||
param.test(t, accessor) | ||
}) | ||
} |
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
Oops, something went wrong.