grokkit/internal/errors/errors_test.go

87 lines
1.8 KiB
Go
Raw Permalink Normal View History

package errors
import (
"errors"
"testing"
)
func TestGitError(t *testing.T) {
baseErr := errors.New("command failed")
gitErr := &GitError{
Command: "status",
Err: baseErr,
}
expected := "git status failed: command failed"
if gitErr.Error() != expected {
t.Errorf("GitError.Error() = %q, want %q", gitErr.Error(), expected)
}
if gitErr.Unwrap() != baseErr {
t.Errorf("GitError.Unwrap() did not return base error")
}
}
func TestAPIError(t *testing.T) {
tests := []struct {
name string
apiErr *APIError
expected string
shouldWrap bool
}{
{
name: "with status code",
apiErr: &APIError{
StatusCode: 404,
Message: "not found",
},
expected: "API error (status 404): not found",
},
{
name: "without status code",
apiErr: &APIError{
Message: "connection failed",
},
expected: "API error: connection failed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.apiErr.Error() != tt.expected {
t.Errorf("APIError.Error() = %q, want %q", tt.apiErr.Error(), tt.expected)
}
})
}
}
func TestFileError(t *testing.T) {
baseErr := errors.New("permission denied")
fileErr := &FileError{
Path: "/tmp/test.txt",
Op: "read",
Err: baseErr,
}
expected := "file read failed for /tmp/test.txt: permission denied"
if fileErr.Error() != expected {
t.Errorf("FileError.Error() = %q, want %q", fileErr.Error(), expected)
}
if fileErr.Unwrap() != baseErr {
t.Errorf("FileError.Unwrap() did not return base error")
}
}
func TestAPIErrorUnwrap(t *testing.T) {
baseErr := errors.New("base api err")
apiErr := &APIError{
StatusCode: 500,
Message: "internal error",
Err: baseErr,
}
if unwrap := apiErr.Unwrap(); unwrap != baseErr {
t.Errorf("APIError.Unwrap() = %v, want %v", unwrap, baseErr)
}
}