You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB
Go

package errors
// Tests for domain error formatting, wrapping, and code matching.
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
// TestDomainError_ErrorFormatting ensures domain error error formatting behavior is handled correctly.
func TestDomainError_ErrorFormatting(t *testing.T) {
base := New(CodeNotFound, "missing")
require.Equal(t, "[NOT_FOUND] missing", base.Error())
wrapped := Wrap(CodeInvalidInput, "bad", errors.New("details"))
require.Equal(t, "[INVALID_INPUT] bad: details", wrapped.Error())
}
// TestDomainError_Is ensures domain error is behavior is handled correctly.
func TestDomainError_Is(t *testing.T) {
err := Wrap(CodeForbidden, "nope", nil)
target := New(CodeForbidden, "other")
require.True(t, errors.Is(err, target))
}
// TestDomainError_Is_DifferentCode ensures domain error is different code behavior is handled correctly.
func TestDomainError_Is_DifferentCode(t *testing.T) {
err := Wrap(CodeForbidden, "nope", nil)
target := New(CodeUnauthorized, "other")
require.False(t, errors.Is(err, target))
}
// TestDomainError_NewAndWrapFields ensures domain error new and wrap fields behavior is handled correctly.
func TestDomainError_NewAndWrapFields(t *testing.T) {
created := New(CodeConflict, "conflict")
require.Equal(t, CodeConflict, created.Code)
require.Equal(t, "conflict", created.Message)
require.Nil(t, created.Err)
require.Nil(t, errors.Unwrap(created))
root := errors.New("root")
wrapped := Wrap(CodeInternal, "internal", root)
require.Equal(t, CodeInternal, wrapped.Code)
require.Equal(t, "internal", wrapped.Message)
require.Equal(t, root, wrapped.Unwrap())
}