github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/expr/get/test/errorable/get_err_test.go (about)

     1  package errorable
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/m4gshm/gollections/expr/get"
    10  )
    11  
    12  var (
    13  	err         = errors.New("test")
    14  	getErr      = func() (int, error) { return 0, err }
    15  	getOne      = func() int { return 1 }
    16  	getTwo      = func() int { return 2 }
    17  	getThree    = func() int { return 3 }
    18  	getOneErr   = func() (int, error) { return 1, nil }
    19  	getTwoErr   = func() (int, error) { return 2, nil }
    20  	getThreeErr = func() (int, error) { return 3, nil }
    21  	getTrue     = func() bool { return true }
    22  	getFalse    = func() bool { return false }
    23  )
    24  
    25  func Test_GetIfElse(t *testing.T) {
    26  	result, err := get.If_(true, getErr).Else(2)
    27  	assert.Error(t, err)
    28  
    29  	result, err = get.If_(false, getErr).Else(2)
    30  	assert.NoError(t, err)
    31  	assert.Equal(t, 2, result)
    32  }
    33  
    34  func Test_UseIfOtherElseGet(t *testing.T) {
    35  	result, _ := get.If_(false, getOneErr).Other(getFalse, getTwo).ElseGet(getThree)
    36  	assert.Equal(t, 3, result)
    37  
    38  	result, _ = get.If_(false, getOneErr).Other(getTrue, getTwo).ElseGet(getThree)
    39  	assert.Equal(t, 2, result)
    40  
    41  	result, _ = get.If_(true, getOneErr).Other(getTrue, getTwo).ElseGet(getThree)
    42  	assert.Equal(t, 1, result)
    43  }
    44  
    45  func Test_UseIfOtherErrElseGetErr(t *testing.T) {
    46  	result, _ := get.If_(false, getOneErr).OtherErr(getFalse, getTwoErr).ElseGetErr(getThreeErr)
    47  	assert.Equal(t, 3, result)
    48  
    49  	result, _ = get.If_(false, getOneErr).OtherErr(getTrue, getTwoErr).ElseGetErr(getThreeErr)
    50  	assert.Equal(t, 2, result)
    51  
    52  	result, _ = get.If_(true, getOneErr).OtherErr(getTrue, getTwoErr).ElseGetErr(getThreeErr)
    53  	assert.Equal(t, 1, result)
    54  }