exception/exception_test.go
2025-08-07 15:50:06 +02:00

54 lines
1.6 KiB
Go

package exception_test
import (
"fmt"
"testing"
"git.apihub24.de/admin/exception"
"github.com/stretchr/testify/suite"
)
type CustomExceptionTestSuite struct {
suite.Suite
}
func (suite *CustomExceptionTestSuite) TestShouldCreateCustomException() {
ex := exception.NewCustom("fail")
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:")
}
func (suite *CustomExceptionTestSuite) TestShouldAddInnerError() {
ex := exception.NewCustom("fail", fmt.Errorf("innerError"))
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError")
}
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrors() {
ex := exception.NewCustom("fail", fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError1\ninnerError2")
}
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() {
ex := exception.NewCustom("fail")
ex.With(fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError1\ninnerError2")
}
func (suite *CustomExceptionTestSuite) TestShouldCompareExceptions() {
InvalidArgumentException := exception.NewCustom("InvalidArgumentException")
NotFoundException := exception.NewCustom("NotFoundException")
customErr := fmt.Errorf("something")
suite.True(InvalidArgumentException.IsSameAs(InvalidArgumentException))
suite.False(NotFoundException.IsSameAs(InvalidArgumentException))
suite.False(NotFoundException.IsSameAs(customErr))
suite.False(InvalidArgumentException.IsSameAs(customErr))
}
func TestCustomExceptionTestSuite(t *testing.T) {
suite.Run(t, new(CustomExceptionTestSuite))
}