2024-08-12 21:03:56 +02:00
|
|
|
package fn
|
|
|
|
|
|
|
|
import (
|
2024-08-07 00:38:14 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-08-12 21:03:56 +02:00
|
|
|
"testing"
|
2024-08-14 01:59:18 +02:00
|
|
|
"testing/quick"
|
2024-08-12 21:03:56 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOptionUnwrapOrFail(t *testing.T) {
|
|
|
|
require.Equal(t, Some(1).UnwrapOrFail(t), 1)
|
|
|
|
}
|
2024-08-07 00:38:14 +02:00
|
|
|
|
|
|
|
func TestSomeToOk(t *testing.T) {
|
|
|
|
err := errors.New("err")
|
|
|
|
require.Equal(t, Some(1).SomeToOk(err), Ok(1))
|
|
|
|
require.Equal(t, None[uint8]().SomeToOk(err), Err[uint8](err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSomeToOkf(t *testing.T) {
|
|
|
|
errStr := "err"
|
|
|
|
require.Equal(t, Some(1).SomeToOkf(errStr), Ok(1))
|
|
|
|
require.Equal(
|
|
|
|
t, None[uint8]().SomeToOkf(errStr),
|
|
|
|
Err[uint8](fmt.Errorf(errStr)),
|
|
|
|
)
|
|
|
|
}
|
2024-08-14 01:59:18 +02:00
|
|
|
|
|
|
|
func TestPropTransposeOptResInverts(t *testing.T) {
|
|
|
|
f := func(i uint) bool {
|
|
|
|
var o Option[Result[uint]]
|
|
|
|
switch i % 3 {
|
|
|
|
case 0:
|
|
|
|
o = Some(Ok(i))
|
|
|
|
case 1:
|
|
|
|
o = Some(Errf[uint]("error"))
|
|
|
|
case 2:
|
|
|
|
o = None[Result[uint]]()
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
odd := TransposeOptRes(o) ==
|
|
|
|
TransposeOptRes(TransposeResOpt(TransposeOptRes(o)))
|
|
|
|
even := TransposeResOpt(TransposeOptRes(o)) == o
|
|
|
|
|
|
|
|
return odd && even
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, quick.Check(f, nil))
|
|
|
|
}
|