2021-09-07 17:09:30 +02:00
|
|
|
package errorsx
|
|
|
|
|
|
|
|
import (
|
2021-09-08 17:42:36 +02:00
|
|
|
"encoding/json"
|
2021-09-07 17:09:30 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-09-07 20:39:32 +02:00
|
|
|
func TestErrWrapper(t *testing.T) {
|
|
|
|
t.Run("Error", func(t *testing.T) {
|
|
|
|
err := &ErrWrapper{Failure: FailureDNSNXDOMAINError}
|
|
|
|
if err.Error() != FailureDNSNXDOMAINError {
|
|
|
|
t.Fatal("invalid return value")
|
|
|
|
}
|
|
|
|
})
|
2021-09-07 17:09:30 +02:00
|
|
|
|
2021-09-07 20:39:32 +02:00
|
|
|
t.Run("Unwrap", func(t *testing.T) {
|
|
|
|
err := &ErrWrapper{
|
|
|
|
Failure: FailureEOFError,
|
|
|
|
WrappedErr: io.EOF,
|
|
|
|
}
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatal("cannot unwrap error")
|
|
|
|
}
|
|
|
|
})
|
2021-09-08 17:42:36 +02:00
|
|
|
|
|
|
|
t.Run("MarshalJSON", func(t *testing.T) {
|
|
|
|
wrappedErr := &ErrWrapper{
|
|
|
|
Failure: FailureEOFError,
|
|
|
|
WrappedErr: io.EOF,
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(wrappedErr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := string(data)
|
|
|
|
if s != "\""+FailureEOFError+"\"" {
|
|
|
|
t.Fatal("invalid serialization", s)
|
|
|
|
}
|
|
|
|
})
|
2021-09-07 17:09:30 +02:00
|
|
|
}
|