72 строки
1.5 KiB
Go
72 строки
1.5 KiB
Go
package servicebus
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestErrMissingField_Error(t *testing.T) {
|
|
const fieldName = "fieldName"
|
|
var subject ErrMissingField = fieldName
|
|
var cast error = subject
|
|
|
|
got := cast.Error()
|
|
const want = `missing value "` + fieldName + `"`
|
|
|
|
if got != want {
|
|
t.Logf("\n\tgot: \t%q\n\twant:\t%q", got, want)
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestErrIncorrectType_Error(t *testing.T) {
|
|
var a int
|
|
var b map[string]interface{}
|
|
var c *float64
|
|
|
|
types := map[reflect.Type]interface{}{
|
|
reflect.TypeOf(a): 7.0,
|
|
reflect.TypeOf(b): map[string]string{},
|
|
reflect.TypeOf(c): int(2),
|
|
}
|
|
|
|
const key = "myFieldName"
|
|
for expected, actual := range types {
|
|
actualType := reflect.TypeOf(actual)
|
|
t.Run(fmt.Sprintf("%s-%s", expected, actualType), func(t *testing.T) {
|
|
expectedMessage := fmt.Sprintf(
|
|
"value at %q was expected to be of type %q but was actually of type %q",
|
|
key,
|
|
expected.String(),
|
|
actualType.String())
|
|
|
|
subject := ErrIncorrectType{
|
|
Key: key,
|
|
ActualValue: actual,
|
|
ExpectedType: expected,
|
|
}
|
|
|
|
var cast error = subject
|
|
|
|
got := cast.Error()
|
|
if got != expectedMessage {
|
|
t.Logf("\n\tgot: \t%q\n\twant:\t%q", got, expectedMessage)
|
|
t.Fail()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestErrNotFound_Error(t *testing.T) {
|
|
err := ErrNotFound{EntityPath: "/foo/bar"}
|
|
assert.Equal(t, "entity at /foo/bar not found", err.Error())
|
|
assert.True(t, IsErrNotFound(err))
|
|
|
|
otherErr := errors.New("foo")
|
|
assert.False(t, IsErrNotFound(otherErr))
|
|
}
|