-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
733339e
commit ad06035
Showing
3 changed files
with
131 additions
and
12 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,120 @@ | ||
package test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/sivaosorg/wrapify" | ||
) | ||
|
||
func TestWithError(t *testing.T) { | ||
err := wrapify.WithError("Test error") | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
if err.Error() != "Test error" { | ||
t.Errorf("Expected 'Test error', got %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestWithErrorf(t *testing.T) { | ||
err := wrapify.WithErrorf("Failed to load file %s", "test.txt") | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
if err.Error() != "Failed to load file test.txt" { | ||
t.Errorf("Expected 'Failed to load file test.txt', got %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestWithErrStack(t *testing.T) { | ||
originalErr := errors.New("original error") | ||
errWithStack := wrapify.WithErrStack(originalErr) | ||
if errWithStack == nil { | ||
t.Errorf("Expected error with stack, got nil") | ||
} | ||
if errWithStack.Error() != "original error" { | ||
t.Errorf("Expected 'original error', got %s", errWithStack.Error()) | ||
} | ||
} | ||
|
||
func TestWrap(t *testing.T) { | ||
originalErr := errors.New("file not found") | ||
wrappedErr := wrapify.WithErrWrap(originalErr, "Failed to read the file") | ||
if wrappedErr == nil { | ||
t.Errorf("Expected wrapped error, got nil") | ||
} | ||
if wrappedErr.Error() != "Failed to read the file: file not found" { | ||
t.Errorf("Expected 'Failed to read the file: file not found', got %s", wrappedErr.Error()) | ||
} | ||
} | ||
|
||
func TestWrapf(t *testing.T) { | ||
originalErr := errors.New("file not found") | ||
wrappedErr := wrapify.WithErrWrapf(originalErr, "Failed to open file %s", "data.txt") | ||
if wrappedErr == nil { | ||
t.Errorf("Expected wrapped error, got nil") | ||
} | ||
if wrappedErr.Error() != "Failed to open file data.txt: file not found" { | ||
t.Errorf("Expected 'Failed to open file data.txt: file not found', got %s", wrappedErr.Error()) | ||
} | ||
} | ||
|
||
func TestWithMessage(t *testing.T) { | ||
originalErr := errors.New("original error") | ||
errWithMessage := wrapify.WithMessage(originalErr, "Additional context") | ||
if errWithMessage == nil { | ||
t.Errorf("Expected error with message, got nil") | ||
} | ||
if errWithMessage.Error() != "Additional context: original error" { | ||
t.Errorf("Expected 'Additional context: original error', got %s", errWithMessage.Error()) | ||
} | ||
} | ||
|
||
func TestWithMessagef(t *testing.T) { | ||
originalErr := errors.New("original error") | ||
errWithMessagef := wrapify.WithMessagef(originalErr, "Context: %s", "something went wrong") | ||
if errWithMessagef == nil { | ||
t.Errorf("Expected error with message, got nil") | ||
} | ||
if errWithMessagef.Error() != "Context: something went wrong: original error" { | ||
t.Errorf("Expected 'Context: something went wrong: original error', got %s", errWithMessagef.Error()) | ||
} | ||
} | ||
|
||
func TestCause(t *testing.T) { | ||
originalErr := errors.New("file not found") | ||
wrappedErr := wrapify.WithErrWrap(originalErr, "Failed to open file") | ||
causeErr := wrapify.Cause(wrappedErr) | ||
if causeErr == nil { | ||
t.Errorf("Expected cause error, got nil") | ||
} | ||
if causeErr.Error() != "file not found" { | ||
t.Errorf("Expected 'file not found', got %s", causeErr.Error()) | ||
} | ||
} | ||
|
||
func TestErrorFormatting(t *testing.T) { | ||
err := wrapify.WithError("Test error") | ||
if err.Error() != "Test error" { | ||
t.Errorf("Expected 'Test error', got %s", err.Error()) | ||
} | ||
|
||
// Ensure that the format includes the stack trace if the '+' flag is set | ||
if errFmt := fmt.Sprintf("%+v", err); errFmt == "Test error" { | ||
t.Errorf("Expected formatted string with stack trace, got %s", errFmt) | ||
} | ||
} | ||
|
||
// func TestUnwrap(t *testing.T) { | ||
// originalErr := errors.New("file not found") | ||
// wrappedErr := wrapify.Wrap(originalErr, "Failed to open file") | ||
// unwrappedErr := wrappedErr.(*underlyingStack).Unwrap() | ||
// if unwrappedErr == nil { | ||
// t.Errorf("Expected unwrapped error, got nil") | ||
// } | ||
// if unwrappedErr.Error() != "file not found" { | ||
// t.Errorf("Expected 'file not found', got %s", unwrappedErr.Error()) | ||
// } | ||
// } |
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