github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xcondition/xcondition_test.go (about) 1 package xcondition 2 3 import ( 4 "errors" 5 "github.com/Aoi-hosizora/ahlib/xtesting" 6 "testing" 7 ) 8 9 func TestIfThen(t *testing.T) { 10 // IfThen 11 xtesting.Equal(t, IfThen(true, "a"), "a") 12 xtesting.Equal(t, IfThen(false, "a"), nil) 13 xtesting.Equal(t, IfThen(true, 1), 1) 14 xtesting.Equal(t, IfThen(false, 1), nil) 15 16 // type of IfThen 17 i1 := IfThen(true, "a") 18 i2 := IfThen(false, 1) 19 xtesting.SameType(t, &i1, new(interface{})) 20 xtesting.SameType(t, &i2, new(interface{})) 21 } 22 23 func TestIfThenElse(t *testing.T) { 24 // IfThenElse 25 xtesting.Equal(t, IfThenElse(true, "a", "b"), "a") 26 xtesting.Equal(t, IfThenElse(false, "a", "b"), "b") 27 xtesting.Equal(t, IfThenElse(true, uint(1), 2), uint(1)) 28 xtesting.Equal(t, IfThenElse(false, uint(1), 2), 2) 29 30 // If 31 xtesting.Equal(t, If(true, "a", "b"), "a") 32 xtesting.Equal(t, If(false, "a", "b"), "b") 33 xtesting.Equal(t, If(true, uint(1), 2), uint(1)) 34 xtesting.Equal(t, If(false, uint(1), 2), 2) 35 36 // type of IfThenElse 37 i1 := IfThenElse(true, uint(1), 2) 38 i2 := IfThenElse(false, uint(1), 2) 39 xtesting.SameType(t, &i1, new(interface{})) 40 xtesting.SameType(t, &i2, new(interface{})) 41 } 42 43 func TestDefaultIfNil(t *testing.T) { 44 // DefaultIfNil 45 xtesting.Equal(t, DefaultIfNil(1, 2), 1) 46 xtesting.Equal(t, DefaultIfNil(1, "2"), 1) 47 xtesting.Equal(t, DefaultIfNil(nil, 2), 2) 48 xtesting.Equal(t, DefaultIfNil(nil, nil), nil) 49 xtesting.Equal(t, DefaultIfNil([]int(nil), []int{1, 2, 3}), []int{1, 2, 3}) 50 xtesting.Equal(t, DefaultIfNil(map[int]int(nil), map[int]int{1: 1, 2: 2}), map[int]int{1: 1, 2: 2}) 51 52 // type of DefaultIfNil 53 i1 := DefaultIfNil(1, 2) 54 i2 := DefaultIfNil([]int(nil), []int{}) 55 xtesting.SameType(t, &i1, new(interface{})) 56 xtesting.SameType(t, &i2, new(interface{})) 57 } 58 59 func TestPanicIfNil(t *testing.T) { 60 // PanicIfNil 61 xtesting.Equal(t, PanicIfNil(1), 1) 62 xtesting.Equal(t, PanicIfNil(1, ""), 1) 63 xtesting.PanicWithValue(t, "nil value", func() { PanicIfNil(nil, "nil value") }) 64 xtesting.PanicWithValue(t, "nil value", func() { PanicIfNil([]int(nil), "nil value") }) 65 xtesting.PanicWithValue(t, "xcondition: nil value for <nil>", func() { PanicIfNil(nil) }) 66 xtesting.PanicWithValue(t, "xcondition: nil value for []int", func() { PanicIfNil([]int(nil), nil, "x") }) 67 68 // Un & Unp 69 xtesting.Equal(t, Un(1), 1) 70 xtesting.Equal(t, Unp(1, ""), 1) 71 xtesting.PanicWithValue(t, "nil value", func() { Unp(nil, "nil value") }) 72 xtesting.PanicWithValue(t, "nil value", func() { Unp([]int(nil), "nil value") }) 73 xtesting.PanicWithValue(t, "xcondition: nil value for <nil>", func() { Un(nil) }) 74 xtesting.PanicWithValue(t, "xcondition: nil value for []int", func() { Unp([]int(nil), nil) }) 75 76 // type of PanicIfNil 77 i1 := PanicIfNil(1) 78 i2 := PanicIfNil([]int{}, "") 79 xtesting.SameType(t, &i1, new(interface{})) 80 xtesting.SameType(t, &i2, new(interface{})) 81 } 82 83 func TestPanicIfErr(t *testing.T) { 84 // PanicIfErr & Ue 85 xtesting.Equal(t, PanicIfErr(0, nil), 0) 86 xtesting.Equal(t, Ue("0", nil), "0") 87 xtesting.PanicWithValue(t, "test", func() { PanicIfErr(nil, errors.New("test")) }) 88 xtesting.PanicWithValue(t, "test", func() { Ue("xxx", errors.New("test")) }) 89 90 // PanicIfErr2 & Ue2 91 v1, v2 := PanicIfErr2("1", 2, nil) 92 xtesting.Equal(t, v1, "1") 93 xtesting.Equal(t, v2, 2) 94 v1, v2 = Ue2(3.3, uint(4), nil) 95 xtesting.Equal(t, v1, 3.3) 96 xtesting.Equal(t, v2, uint(4)) 97 xtesting.PanicWithValue(t, "test", func() { PanicIfErr2(nil, nil, errors.New("test")) }) 98 xtesting.PanicWithValue(t, "test", func() { Ue2("xxx", "yyy", errors.New("test")) }) 99 100 // PanicIfErr3 & Ue3 101 v1, v2, v3 := PanicIfErr3("1", 2, '3', nil) 102 xtesting.Equal(t, v1, "1") 103 xtesting.Equal(t, v2, 2) 104 xtesting.Equal(t, v3, '3') 105 v1, v2, v3 = Ue3(4.4, uint(5), true, nil) 106 xtesting.Equal(t, v1, 4.4) 107 xtesting.Equal(t, v2, uint(5)) 108 xtesting.Equal(t, v3, true) 109 xtesting.PanicWithValue(t, "test", func() { PanicIfErr3(nil, nil, nil, errors.New("test")) }) 110 xtesting.PanicWithValue(t, "test", func() { Ue3("xxx", "yyy", "zzz", errors.New("test")) }) 111 112 // type of PanicIfErr & PanicIfErr3 113 i1 := PanicIfErr(0, nil) 114 i2 := PanicIfErr("0", nil) 115 xtesting.SameType(t, &i1, new(interface{})) 116 xtesting.SameType(t, &i2, new(interface{})) 117 xtesting.SameType(t, &v1, new(interface{})) 118 xtesting.SameType(t, &v2, new(interface{})) 119 xtesting.SameType(t, &v3, new(interface{})) 120 } 121 122 func TestLet(t *testing.T) { 123 xtesting.Equal(t, Let(nil, nil), nil) 124 xtesting.Equal(t, NillableLet(nil, nil), nil) 125 xtesting.Equal(t, Let(0, nil), nil) 126 xtesting.Equal(t, NillableLet(0, nil), nil) 127 128 visited := false 129 xtesting.Equal(t, Let(0, func(t interface{}) interface{} { visited = true; return t.(int) + 1 }), 1) 130 xtesting.Equal(t, visited, true) 131 visited = false 132 xtesting.Equal(t, NillableLet(0, func(t interface{}) interface{} { visited = true; return t.(int) + 1 }), 1) 133 xtesting.Equal(t, visited, true) 134 135 visited = false 136 xtesting.Equal(t, Let(nil, func(t interface{}) interface{} { visited = true; return *(t.(*uint64)) + 1 }), nil) // uint64(0) 137 xtesting.Equal(t, visited, false) 138 visited = false 139 xtesting.Equal(t, NillableLet(nil, func(t interface{}) interface{} { visited = true; return uint64(1) }), uint64(1)) 140 xtesting.Equal(t, visited, true) 141 142 visited = false 143 v := 3.0 144 xtesting.Equal(t, Let(&v, func(t interface{}) interface{} { visited = true; return *(t.(*float64)) + 1.0 }), 4.0) 145 xtesting.Equal(t, visited, true) 146 visited = false 147 xtesting.Equal(t, NillableLet(nil, func(t interface{}) interface{} { visited = true; return 1.0 }), 1.0) 148 xtesting.Equal(t, visited, true) 149 150 visited = false 151 xtesting.Equal(t, Let(visited, func(t interface{}) interface{} { visited = true; return !(t.(bool)) }), true) 152 xtesting.Equal(t, visited, true) 153 visited = false 154 xtesting.Equal(t, NillableLet(visited, func(t interface{}) interface{} { visited = true; return !(t.(bool)) }), true) 155 xtesting.Equal(t, visited, true) 156 } 157 158 var ( 159 f1 = func() int { return 1 } 160 f2 = func() (int, int) { return 1, 2 } 161 f3 = func() (int, int, int) { return 1, 2, 3 } 162 f4 = func() (int, int, int, int) { return 1, 2, 3, 4 } 163 ) 164 165 func TestFirst(t *testing.T) { 166 xtesting.Panic(t, func() { First() }) 167 xtesting.Equal(t, First(f1()), 1) 168 xtesting.Equal(t, First(f2()), 1) 169 xtesting.Equal(t, First(f3()), 1) 170 xtesting.Equal(t, First(f4()), 1) 171 } 172 173 func TestSecond(t *testing.T) { 174 xtesting.Panic(t, func() { Second() }) 175 xtesting.Panic(t, func() { Second(f1()) }) 176 xtesting.Equal(t, Second(f2()), 2) 177 xtesting.Equal(t, Second(f3()), 2) 178 xtesting.Equal(t, Second(f4()), 2) 179 } 180 181 func TestThird(t *testing.T) { 182 xtesting.Panic(t, func() { Third() }) 183 xtesting.Panic(t, func() { Third(f1()) }) 184 xtesting.Panic(t, func() { Third(f2()) }) 185 xtesting.Equal(t, Third(f3()), 3) 186 xtesting.Equal(t, Third(f4()), 3) 187 } 188 189 func TestLast(t *testing.T) { 190 xtesting.Panic(t, func() { Last() }) 191 xtesting.Equal(t, Last(f1()), 1) 192 xtesting.Equal(t, Last(f2()), 2) 193 xtesting.Equal(t, Last(f3()), 3) 194 xtesting.Equal(t, Last(f4()), 4) 195 }