diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2025-07-19 16:59:53 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2025-07-19 16:59:53 -0400 |
| commit | 566b9f7fde255a2307113f6b6fdf0979751ecc83 (patch) | |
| tree | e53935076e2c856f33011ffb2fa1004d05b447ab /errors_test.go | |
| parent | Add .DS_Store to gitignore (diff) | |
| download | envelope-566b9f7fde255a2307113f6b6fdf0979751ecc83.zip | |
Add support for nested structs and additional types
Diffstat (limited to 'errors_test.go')
| -rw-r--r-- | errors_test.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/errors_test.go b/errors_test.go new file mode 100644 index 0000000..fb739a0 --- /dev/null +++ b/errors_test.go @@ -0,0 +1,80 @@ +package envelope + +import ( + "errors" + "reflect" + "testing" + + "git.sr.ht/~eugenetriguba/checkmate/assert" + "git.sr.ht/~eugenetriguba/checkmate/check" +) + +// GIVEN a BaseError +// WHEN we call Error on it +// THEN it should return the error message if there's no underlying error +// or a formatted message including the underlying error when one exists +func TestBaseError(t *testing.T) { + err := BaseError{ + Message: "test error", + Err: nil, + } + check.Equal(t, err.Error(), err.Message) + + err.Err = errors.New("underlying error") + check.Equal(t, err.Error(), "test error: underlying error") + +} + +// GIVEN a NotPointerToStructError +// WHEN we create it with a non-pointer type +// THEN it should: +// - Return an error message indicating the value must be a pointer to a struct +// - Be identifiable via errors.As +// - Contain the actual type that was provided +func TestNotPointerToStructError(t *testing.T) { + var s string + err := newNotPointerToStructError(reflect.TypeOf(s)) + assert.Equal(t, "value must be a pointer to a struct", err.Error()) + + var notPtrErr *NotPointerToStructError + assert.NotNil(t, notPtrErr) + assert.True(t, errors.As(err, ¬PtrErr)) + assert.Equal(t, reflect.TypeOf(s), notPtrErr.ActualType) +} + +// GIVEN an UnsupportedTypeError +// WHEN we create it with a field name and type +// THEN it should: +// - Return an error message indicating the field type is unsupported +// - Be identifiable via errors.As +// - Contain the field name and type that caused the error +func TestUnsupportedTypeError(t *testing.T) { + err := newUnsupportedTypeError("TestField", reflect.TypeOf(complex128(0))) + assert.Equal(t, "unsupported field type for field TestField", err.Error()) + + var typeErr *UnsupportedTypeError + assert.True(t, errors.As(err, &typeErr)) + assert.Equal(t, "TestField", typeErr.FieldName) + assert.Equal(t, reflect.TypeOf(complex128(0)), typeErr.FieldType) +} + +// GIVEN a ParseError created with field information and an underlying error +// WHEN we examine its properties and behavior +// THEN it should: +// - Contain the correct error message with field and environment variable context +// - Wrap the original error properly +// - Provide access to its field name, environment variable name, value, and target type +func TestParseError(t *testing.T) { + originalErr := errors.New("test error") + err := newParseError("TestField", "TEST_ENV", "invalid", reflect.TypeOf(int(0)), originalErr) + assert.ErrorContains(t, err, "error parsing value 'invalid' to 'int' for field 'TestField' from environment variable 'TEST_ENV'") + + var parseErr *ParseError + assert.True(t, errors.As(err, &parseErr)) + assert.Equal(t, "TestField", parseErr.FieldName) + assert.Equal(t, "TEST_ENV", parseErr.EnvName) + assert.Equal(t, "invalid", parseErr.Value) + assert.Equal(t, reflect.TypeOf(int(0)), parseErr.TargetType) + + assert.True(t, errors.Is(err, originalErr)) +} |