55 lines
1.7 KiB
Go
55 lines
1.7 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))
|
|
suite.False(InvalidArgumentException.IsSameAs(nil))
|
|
}
|
|
|
|
func TestCustomExceptionTestSuite(t *testing.T) {
|
|
suite.Run(t, new(CustomExceptionTestSuite))
|
|
}
|