github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/expr/use/test/use_test.go (about) 1 package test 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/m4gshm/gollections/expr/use" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 var ( 12 err = errors.New("test") 13 getErr = func() (int, error) { return 0, err } 14 getOne = func() int { return 1 } 15 getTwo = func() int { return 2 } 16 getThree = func() int { return 3 } 17 getOneErr = func() (int, error) { return 1, nil } 18 getTwoErr = func() (int, error) { return 2, nil } 19 getThreeErr = func() (int, error) { return 3, nil } 20 getTrue = func() bool { return true } 21 getFalse = func() bool { return false } 22 ) 23 24 func Test_UseIfElse(t *testing.T) { 25 result := use.If(true, 1).Else(2) 26 assert.Equal(t, 1, result) 27 28 result = use.If(false, 1).Else(2) 29 assert.Equal(t, 2, result) 30 } 31 32 func Test_UseEval(t *testing.T) { 33 result, ok := use.If(true, 1).If(true, 2).Eval() 34 assert.True(t, ok) 35 assert.Equal(t, 1, result) 36 37 result, ok = use.If(false, 1).If(true, 2).Eval() 38 assert.True(t, ok) 39 assert.Equal(t, 2, result) 40 41 result, ok = use.If(false, 1).If(false, 2).Eval() 42 assert.False(t, ok) 43 assert.Equal(t, 0, result) 44 } 45 46 func Test_UseElseZero(t *testing.T) { 47 result := use.If(true, 1).If(true, 2).ElseZero() 48 assert.Equal(t, 1, result) 49 50 result = use.If(false, 1).If(true, 2).ElseZero() 51 assert.Equal(t, 2, result) 52 53 result = use.If(false, 1).If(false, 2).ElseZero() 54 assert.Equal(t, 0, result) 55 } 56 57 func Test_UseIfIfElse(t *testing.T) { 58 result := use.If(true, 1).If(true, 2).Else(3) 59 assert.Equal(t, 1, result) 60 61 result = use.If(false, 1).If(true, 2).Else(3) 62 assert.Equal(t, 2, result) 63 64 result = use.If(false, 1).If(false, 2).Else(3) 65 assert.Equal(t, 3, result) 66 } 67 68 func Test_UseIfElseGet(t *testing.T) { 69 result := use.If(false, 1).ElseGet(getTwo) 70 assert.Equal(t, 2, result) 71 72 result = use.If(true, 1).ElseGet(getTwo) 73 assert.Equal(t, 1, result) 74 } 75 76 func Test_UseIfElseErr(t *testing.T) { 77 result, e := use.If(false, 1).ElseErr(err) 78 assert.Error(t, e) 79 80 result, e = use.If(true, 1).ElseErr(err) 81 assert.NoError(t, e) 82 assert.Equal(t, 1, result) 83 84 result = use.If(true, 1).If(true, 2).Else(3) 85 assert.Equal(t, 1, result) 86 } 87 88 func Test_UseIfElseGetErr(t *testing.T) { 89 result, _ := use.If(false, 1).ElseGetErr(getTwoErr) 90 assert.Equal(t, 2, result) 91 92 result, _ = use.If(true, 1).ElseGetErr(getTwoErr) 93 assert.Equal(t, 1, result) 94 } 95 96 func Test_UseIfIfGetElseGet(t *testing.T) { 97 result := use.If(false, 1).IfGet(false, getTwo).ElseGet(getThree) 98 assert.Equal(t, 3, result) 99 100 result = use.If(false, 1).IfGet(true, getTwo).ElseGet(getThree) 101 assert.Equal(t, 2, result) 102 103 result = use.If(true, 1).IfGet(true, getTwo).ElseGet(getThree) 104 assert.Equal(t, 1, result) 105 } 106 107 func Test_UseIfIfGetErrElseGetErr(t *testing.T) { 108 result, _ := use.If(false, 1).IfGetErr(false, getTwoErr).ElseGetErr(getThreeErr) 109 assert.Equal(t, 3, result) 110 111 result, _ = use.If(false, 1).IfGetErr(true, getTwoErr).ElseGetErr(getThreeErr) 112 assert.Equal(t, 2, result) 113 114 result, _ = use.If(true, 1).IfGetErr(true, getTwoErr).ElseGetErr(getThreeErr) 115 assert.Equal(t, 1, result) 116 } 117 118 func Test_UseIfIfGetErrElseZero(t *testing.T) { 119 result, _ := use.If(true, 1).IfGetErr(true, getTwoErr).ElseZero() 120 assert.Equal(t, 1, result) 121 122 result, _ = use.If(false, 1).IfGetErr(true, getTwoErr).ElseZero() 123 assert.Equal(t, 2, result) 124 125 result, _ = use.If(false, 1).IfGetErr(false, getTwoErr).ElseZero() 126 assert.Equal(t, 0, result) 127 } 128 129 func Test_UseIfIfGetErrEval(t *testing.T) { 130 result, ok, _ := use.If(true, 1).IfGetErr(true, getTwoErr).Eval() 131 assert.True(t, ok) 132 assert.Equal(t, 1, result) 133 134 result, ok, _ = use.If(false, 1).IfGetErr(true, getTwoErr).Eval() 135 assert.True(t, ok) 136 assert.Equal(t, 2, result) 137 138 result, ok, _ = use.If(false, 1).IfGetErr(false, getTwoErr).Eval() 139 assert.False(t, ok) 140 assert.Equal(t, 0, result) 141 } 142 143 func Test_UseIfOtherElseGet(t *testing.T) { 144 result := use.If(false, 1).Other(getFalse, getTwo).ElseGet(getThree) 145 assert.Equal(t, 3, result) 146 147 result = use.If(false, 1).Other(getTrue, getTwo).ElseGet(getThree) 148 assert.Equal(t, 2, result) 149 150 result = use.If(true, 1).Other(getTrue, getTwo).ElseGet(getThree) 151 assert.Equal(t, 1, result) 152 } 153 154 func Test_UseIfOtherErrElseGetErr(t *testing.T) { 155 result, _ := use.If(false, 1).OtherErr(getFalse, getTwoErr).ElseGetErr(getThreeErr) 156 assert.Equal(t, 3, result) 157 158 result, _ = use.If(false, 1).OtherErr(getTrue, getTwoErr).ElseGetErr(getThreeErr) 159 assert.Equal(t, 2, result) 160 161 result, _ = use.If(true, 1).OtherErr(getTrue, getTwoErr).ElseGetErr(getThreeErr) 162 assert.Equal(t, 1, result) 163 }