mirror of
https://github.com/IBM/fp-go.git
synced 2025-06-23 00:27:49 +02:00
40 lines
815 B
Go
40 lines
815 B
Go
package either
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
F "github.com/ibm/fp-go/function"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestWithResource(t *testing.T) {
|
|
onCreate := func() Either[error, *os.File] {
|
|
return TryCatchError(func() (*os.File, error) {
|
|
return os.CreateTemp("", "*")
|
|
})
|
|
}
|
|
onDelete := F.Flow2(
|
|
func(f *os.File) Either[error, string] {
|
|
return TryCatchError(func() (string, error) {
|
|
return f.Name(), f.Close()
|
|
})
|
|
},
|
|
Chain(func(name string) Either[error, any] {
|
|
return TryCatchError(func() (any, error) {
|
|
return name, os.Remove(name)
|
|
})
|
|
}),
|
|
)
|
|
|
|
onHandler := func(f *os.File) Either[error, string] {
|
|
return Of[error](f.Name())
|
|
}
|
|
|
|
tempFile := WithResource[error, *os.File, string](onCreate, onDelete)
|
|
|
|
resE := tempFile(onHandler)
|
|
|
|
assert.True(t, IsRight(resE))
|
|
}
|