1
0
mirror of synced 2024-11-29 00:26:12 +03:00
messenger/unmarshal_error_test.go

58 lines
1.7 KiB
Go
Raw Normal View History

2022-01-26 14:35:38 +03:00
package messenger
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewUnmarshalError(t *testing.T) {
2022-01-28 12:24:58 +03:00
err := errors.New("some error")
unmarshalError := NewUnmarshalError(err)
assert.True(t, errors.Is(unmarshalError, ErrUnmarshal))
2022-01-26 14:35:38 +03:00
}
func TestUnmarshalError_Error(t *testing.T) {
2022-01-28 12:24:58 +03:00
err := errors.New("some error")
2022-01-26 14:35:38 +03:00
content := []byte("test content")
2022-01-28 12:24:58 +03:00
actual := NewUnmarshalError(err).WithContent(content).Error()
expected := "can not unmarshal content: test content; error: some error"
2022-01-26 14:35:38 +03:00
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_Unwrap(t *testing.T) {
2022-01-28 12:24:58 +03:00
err := errors.New("some error")
actual := NewUnmarshalError(err).Unwrap()
2022-01-26 14:35:38 +03:00
expected := ErrUnmarshal
assert.Equal(t, expected, actual)
}
2022-01-28 12:24:58 +03:00
func TestUnmarshalError_WithContent(t *testing.T) {
err := errors.New("some error")
2022-01-26 14:35:38 +03:00
content := []byte("test content")
2022-01-28 12:24:58 +03:00
actual := NewUnmarshalError(err).WithContent(content)
expected := &UnmarshalError{Err: ErrUnmarshal, Content: content, ErrorText: err.Error()}
2022-01-26 14:35:38 +03:00
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_WithReader(t *testing.T) {
2022-01-28 12:24:58 +03:00
err := errors.New("some error")
2022-01-26 14:35:38 +03:00
content := []byte("test content")
reader := bytes.NewReader(content)
2022-01-28 12:24:58 +03:00
actual := NewUnmarshalError(err).WithReader(reader)
expected := &UnmarshalError{Err: ErrUnmarshal, Content: content, ErrorText: err.Error()}
2022-01-26 14:35:38 +03:00
assert.Equal(t, expected, actual)
}
func TestUnmarshalError_WithErr(t *testing.T) {
2022-01-28 12:24:58 +03:00
someError := errors.New("some error")
otherError := errors.New("other error")
actual := NewUnmarshalError(someError).WithErr(otherError)
expected := &UnmarshalError{Err: otherError, ErrorText: someError.Error()}
2022-01-26 14:35:38 +03:00
assert.Equal(t, expected, actual)
}