2023-07-07 22:31:06 +02:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-18 15:57:54 +02:00
|
|
|
F "github.com/IBM/fp-go/function"
|
|
|
|
O "github.com/IBM/fp-go/option"
|
2023-07-07 22:31:06 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MyError struct{}
|
|
|
|
|
|
|
|
func (m *MyError) Error() string {
|
|
|
|
return "boom"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAs(t *testing.T) {
|
|
|
|
root := &MyError{}
|
|
|
|
err := fmt.Errorf("This is my custom error, %w", root)
|
|
|
|
|
|
|
|
errO := F.Pipe1(
|
|
|
|
err,
|
|
|
|
As[*MyError](),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.Equal(t, O.Of(root), errO)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotAs(t *testing.T) {
|
|
|
|
err := fmt.Errorf("This is my custom error")
|
|
|
|
|
|
|
|
errO := F.Pipe1(
|
|
|
|
err,
|
|
|
|
As[*MyError](),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.Equal(t, O.None[*MyError](), errO)
|
|
|
|
}
|