github.com/Axway/agent-sdk@v1.1.101/pkg/util/errors/agenterror_test.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestNewError(t *testing.T) {
    11  	code := 1001
    12  	msg := "this is a test error"
    13  	newErr := New(code, msg)
    14  
    15  	assert.NotNil(t, newErr, "The error returned by New was nil")
    16  	assert.IsType(t, &AgentError{}, newErr, "The new error was not of AgentError type")
    17  	assert.Implements(t, (*error)(nil), newErr, "The AgentError struct does not implement error")
    18  	assert.Contains(t, newErr.Error(), msg, "The error msg returned was incorrect")
    19  	assert.Contains(t, newErr.FormatError().Error(), msg, "The error msg returned was incorrect")
    20  	assert.Equal(t, code, newErr.GetErrorCode(), "The error code returned was incorrect")
    21  }
    22  
    23  func TestNewfError(t *testing.T) {
    24  	code := 1001
    25  	msg := "format %s test error"
    26  	newErr := Newf(code, msg)
    27  
    28  	assert.NotNil(t, newErr, "The error returned by New was nil")
    29  	assert.IsType(t, &AgentError{}, newErr, "The new error was not of AgentError type")
    30  	assert.Implements(t, (*error)(nil), newErr, "The AgentError struct does not implement error")
    31  	assert.Contains(t, newErr.FormatError("value").Error(), fmt.Sprintf(msg, "value"), "The error msg returned was incorrect")
    32  	assert.Equal(t, code, newErr.GetErrorCode(), "The error code returned was incorrect")
    33  }
    34  
    35  func TestWrapError(t *testing.T) {
    36  	code := 1001
    37  	msg := "this is a test error"
    38  	newErr := New(code, msg)
    39  
    40  	wrapMsg := "wrapped message"
    41  	wrapErr := Wrap(newErr, wrapMsg)
    42  
    43  	assert.NotNil(t, wrapErr, "The error returned by Wrap was nil")
    44  	assert.IsType(t, &AgentError{}, wrapErr, "The new error was not of AgentError type")
    45  	assert.Implements(t, (*error)(nil), wrapErr, "The AgentError struct does not implement error")
    46  	assert.Contains(t, wrapErr.Error(), msg+": "+wrapMsg, "The error msg returned was incorrect")
    47  	assert.Contains(t, wrapErr.FormatError().Error(), msg+": "+wrapMsg, "The error msg returned was incorrect")
    48  	assert.Equal(t, code, wrapErr.GetErrorCode(), "The error code returned was incorrect")
    49  }