github.com/enetx/g@v1.0.80/tests/result_test.go (about) 1 package g_test 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 8 "github.com/enetx/g" 9 ) 10 11 func TestResultOf(t *testing.T) { 12 // Test 1: Result with an error 13 err1 := errors.New("something went wrong") 14 result1 := g.ResultOf(0, err1) 15 16 if result1.IsOk() { 17 t.Errorf("Test 1: Expected error, got Ok") 18 } 19 20 if !reflect.DeepEqual(result1.Err(), err1) { 21 t.Errorf("Test 1: Expected %v, got %v", err1, result1.Err()) 22 } 23 24 // Test 2: Result with a value 25 value2 := 42 26 result2 := g.ResultOf(value2, nil) 27 28 if result2.IsErr() { 29 t.Errorf("Test 2: Expected Ok, got error") 30 } 31 32 if result2.Unwrap() != value2 { 33 t.Errorf("Test 2: Expected %d, got %d", value2, result2.Unwrap()) 34 } 35 } 36 37 func TestResultMap(t *testing.T) { 38 // Test 1: Mapping over a Result with a value 39 result1 := g.Ok[int](5) 40 41 fn1 := func(x int) g.Result[int] { 42 return g.Ok(x * 2) 43 } 44 45 mappedResult1 := g.ResultMap(result1, fn1) 46 47 if mappedResult1.IsErr() { 48 t.Errorf("Test 1: Expected Ok, got error") 49 } 50 51 expectedValue1 := 10 52 if mappedResult1.Unwrap() != expectedValue1 { 53 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, mappedResult1.Unwrap()) 54 } 55 56 // Test 2: Mapping over a Result with an error 57 err2 := errors.New("some error") 58 result2 := g.Err[int](err2) 59 60 fn2 := func(x int) g.Result[int] { 61 return g.Ok(x * 2) 62 } 63 64 mappedResult2 := g.ResultMap(result2, fn2) 65 66 if mappedResult2.IsOk() { 67 t.Errorf("Test 2: Expected error, got Ok") 68 } 69 70 if !reflect.DeepEqual(mappedResult2.Err(), err2) { 71 t.Errorf("Test 2: Expected %v, got %v", err2, mappedResult2.Err()) 72 } 73 } 74 75 func TestResultOfMap(t *testing.T) { 76 // Test 1: Mapping over a Result with a value 77 result1 := g.Ok[int](5) 78 79 fn1 := func(x int) (int, error) { 80 return x * 2, nil 81 } 82 83 mappedResult1 := g.ResultOfMap(result1, fn1) 84 85 if mappedResult1.IsErr() { 86 t.Errorf("Test 1: Expected Ok, got error") 87 } 88 89 expectedValue1 := 10 90 if mappedResult1.Unwrap() != expectedValue1 { 91 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, mappedResult1.Unwrap()) 92 } 93 94 // Test 2: Mapping over a Result with an error 95 err2 := errors.New("some error") 96 result2 := g.Err[int](err2) 97 98 fn2 := func(x int) (int, error) { 99 return x * 2, nil 100 } 101 102 mappedResult2 := g.ResultOfMap(result2, fn2) 103 104 if mappedResult2.IsOk() { 105 t.Errorf("Test 2: Expected error, got Ok") 106 } 107 108 if !reflect.DeepEqual(mappedResult2.Err(), err2) { 109 t.Errorf("Test 2: Expected %v, got %v", err2, mappedResult2.Err()) 110 } 111 } 112 113 func TestResult(t *testing.T) { 114 // Test 1: Result with a value 115 result1 := g.Ok[int](42) 116 value1, err1 := result1.Result() 117 118 if err1 != nil { 119 t.Errorf("Test 1: Expected nil error, got %v", err1) 120 } 121 122 expectedValue1 := 42 123 if value1 != expectedValue1 { 124 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, value1) 125 } 126 127 // Test 2: Result with an error 128 err2 := errors.New("some error") 129 result2 := g.Err[int](err2) 130 131 _, err := result2.Result() 132 if err == nil { 133 t.Errorf("Test 2: Expected non-nil error, got nil") 134 } 135 136 if !reflect.DeepEqual(err2, err) { 137 t.Errorf("Test 2: Expected %v, got %v", err2, err) 138 } 139 } 140 141 // func TestResultUnwrap(t *testing.T) { 142 // // Test 1: Unwrapping Result with a value 143 // result1 := g.Ok[int](42) 144 // value1 := result1.Unwrap() 145 146 // expectedValue1 := 42 147 // if value1 != expectedValue1 { 148 // t.Errorf("Test 1: Expected %d, got %d", expectedValue1, value1) 149 // } 150 151 // // Test 2: Unwrapping Result with an error, should panic 152 // err2 := errors.New("some error") 153 // result2 := g.Err[int](err2) 154 // defer func() { 155 // if r := recover(); r == nil { 156 // t.Errorf("Test 2: The code did not panic") 157 // } 158 // }() 159 160 // result2.Unwrap() 161 // } 162 163 func TestResultUnwrapOr(t *testing.T) { 164 // Test 1: Unwrapping Result with a value 165 result1 := g.Ok[int](42) 166 value1 := result1.UnwrapOr(10) 167 168 expectedValue1 := 42 169 if value1 != expectedValue1 { 170 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, value1) 171 } 172 173 // Test 2: Unwrapping Result with an error, should return default value 174 err2 := errors.New("some error") 175 result2 := g.Err[int](err2) 176 defaultValue2 := 10 177 value2 := result2.UnwrapOr(defaultValue2) 178 179 if value2 != defaultValue2 { 180 t.Errorf("Test 2: Expected %d, got %d", defaultValue2, value2) 181 } 182 } 183 184 // func TestResultExpect(t *testing.T) { 185 // // Test 1: Expecting value from Result with a value 186 // result1 := g.Ok[int](42) 187 // value1 := result1.Expect("Expected Some, got None") 188 189 // expectedValue1 := 42 190 // if value1 != expectedValue1 { 191 // t.Errorf("Test 1: Expected %d, got %d", expectedValue1, value1) 192 // } 193 194 // // Test 2: Expecting panic from Result with an error 195 // err2 := errors.New("some error") 196 // result2 := g.Err[int](err2) 197 // defer func() { 198 // if r := recover(); r == nil { 199 // t.Errorf("Test 2: The code did not panic") 200 // } 201 // }() 202 203 // result2.Expect("Expected Some, got None") 204 // } 205 206 func TestResultThen(t *testing.T) { 207 // Test 1: Applying fn to Result with a value 208 result1 := g.Ok[int](42) 209 fn1 := func(x int) g.Result[int] { 210 return g.Ok(x * 2) 211 } 212 newResult1 := result1.Then(fn1) 213 214 if newResult1.IsErr() { 215 t.Errorf("Test 1: Expected Ok, got error") 216 } 217 218 expectedValue1 := 84 219 if newResult1.Unwrap() != expectedValue1 { 220 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, newResult1.Unwrap()) 221 } 222 223 // Test 2: Returning same Result for Result with an error 224 err2 := errors.New("some error") 225 result2 := g.Err[int](err2) 226 fn2 := func(x int) g.Result[int] { 227 return g.Ok(x * 2) 228 } 229 newResult2 := result2.Then(fn2) 230 231 if newResult2.IsOk() { 232 t.Errorf("Test 2: Expected error, got Ok") 233 } 234 235 if !reflect.DeepEqual(newResult2.Err(), err2) { 236 t.Errorf("Test 2: Expected %v, got %v", err2, newResult2.Err()) 237 } 238 } 239 240 func TestResultThenOf(t *testing.T) { 241 // Test 1: Applying fn to Result with a value 242 result1 := g.Ok[int](42) 243 fn1 := func(x int) (int, error) { 244 return x * 2, nil 245 } 246 247 newResult1 := result1.ThenOf(fn1) 248 249 if newResult1.IsErr() { 250 t.Errorf("Test 1: Expected Ok, got error") 251 } 252 253 expectedValue1 := 84 254 if newResult1.Unwrap() != expectedValue1 { 255 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, newResult1.Unwrap()) 256 } 257 258 // Test 2: Returning same Result for Result with an error 259 err2 := errors.New("some error") 260 result2 := g.Err[int](err2) 261 fn2 := func(x int) (int, error) { 262 return x * 2, nil 263 } 264 newResult2 := result2.ThenOf(fn2) 265 266 if newResult2.IsOk() { 267 t.Errorf("Test 2: Expected error, got Ok") 268 } 269 270 if !reflect.DeepEqual(newResult2.Err(), err2) { 271 t.Errorf("Test 2: Expected %v, got %v", err2, newResult2.Err()) 272 } 273 } 274 275 func TestResultOption(t *testing.T) { 276 // Test 1: Converting Result with a value to Option 277 result1 := g.Ok[int](42) 278 option1 := result1.Option() 279 280 if option1.IsNone() { 281 t.Errorf("Test 1: Expected Some, got None") 282 } 283 284 expectedValue1 := 42 285 if option1.Some() != expectedValue1 { 286 t.Errorf("Test 1: Expected %d, got %d", expectedValue1, option1.Some()) 287 } 288 289 // Test 2: Converting Result with an error to None 290 err2 := errors.New("some error") 291 result2 := g.Err[int](err2) 292 option2 := result2.Option() 293 294 if option2.IsSome() { 295 t.Errorf("Test 2: Expected None, got Some") 296 } 297 }