|
| 1 | +import pytest |
| 2 | +from ResultContainer import Result, Ok, Err, ResultErr |
| 3 | + |
| 4 | + |
| 5 | +# Basic Initialization Tests |
| 6 | +def test_result_ok_initialization(): |
| 7 | + result = Result.Ok(42) |
| 8 | + assert result.is_Ok is True |
| 9 | + assert result.is_Err is False |
| 10 | + assert result.unwrap() == 42 |
| 11 | + assert result.expect() == Result.Ok(42) |
| 12 | + |
| 13 | + with pytest.raises(ResultErr): |
| 14 | + result.expect_Err() |
| 15 | + |
| 16 | + |
| 17 | +def test_result_err_initialization(): |
| 18 | + error_message = "An error occurred" |
| 19 | + result = Result.Err(error_message) |
| 20 | + assert result.is_Ok is False |
| 21 | + assert result.is_Err is True |
| 22 | + |
| 23 | + with pytest.raises(ResultErr): |
| 24 | + result.expect() |
| 25 | + |
| 26 | + with pytest.raises(ResultErr): |
| 27 | + result.raised() |
| 28 | + |
| 29 | + |
| 30 | +# Edge Case Initialization |
| 31 | +def test_result_ok_with_none(): |
| 32 | + result = Result.Ok(None) |
| 33 | + assert result.is_Ok is True |
| 34 | + assert result.unwrap() is None |
| 35 | + assert result.expect() is None |
| 36 | + assert result.raised() == Ok(None) |
| 37 | + |
| 38 | + |
| 39 | +def test_result_err_with_empty_string(): |
| 40 | + result = Result.Err("") |
| 41 | + assert result.is_Err is True |
| 42 | + |
| 43 | + with pytest.raises(ResultErr): |
| 44 | + result.expect() |
| 45 | + |
| 46 | + with pytest.raises(ResultErr): |
| 47 | + result.raised() |
| 48 | + |
| 49 | + |
| 50 | +# Iter Tests |
| 51 | +def test_iter_err(): |
| 52 | + result = Err(10) |
| 53 | + enter_loop = False |
| 54 | + for i in result: |
| 55 | + enter_loop = True # should never be true |
| 56 | + assert not enter_loop |
| 57 | + |
| 58 | + |
| 59 | +def test_iter_scalar(): |
| 60 | + result = Ok(10) |
| 61 | + enter_loop = False |
| 62 | + for i in result: |
| 63 | + assert i == 10 |
| 64 | + enter_loop = True |
| 65 | + assert enter_loop |
| 66 | + |
| 67 | + |
| 68 | +def test_iter_list(): |
| 69 | + lst = [0, 1, 2, 3] |
| 70 | + result = Result.Ok(lst) |
| 71 | + enter_loop = False |
| 72 | + i = 0 |
| 73 | + for j in result: |
| 74 | + assert j == lst[i] |
| 75 | + i += 1 |
| 76 | + enter_loop = True |
| 77 | + assert enter_loop |
| 78 | + |
| 79 | + enter_loop = False |
| 80 | + for i, j in enumerate(result): |
| 81 | + assert j == lst[i] |
| 82 | + enter_loop = True |
| 83 | + assert enter_loop |
| 84 | + |
| 85 | + lst[-1] = 99 # Because mutable, Ok(lst) changes too |
| 86 | + enter_loop = False |
| 87 | + for i, j in enumerate(result): |
| 88 | + assert j == lst[i] |
| 89 | + enter_loop = True |
| 90 | + assert enter_loop |
| 91 | + |
| 92 | + |
| 93 | +# Method Tests |
| 94 | +def test_is_ok_and(): |
| 95 | + result = Result.Ok(10) |
| 96 | + assert result.is_Ok_and(lambda x: x == 10) |
| 97 | + assert result.is_Ok_and(lambda x: x < 11) |
| 98 | + assert result.is_Ok_and(lambda x: x * 2 < 21) |
| 99 | + |
| 100 | + |
| 101 | +def test_result_apply(): |
| 102 | + result = Result.Ok(10) |
| 103 | + mapped_result = result.apply(lambda x: x * 2) |
| 104 | + assert mapped_result.is_Ok is True |
| 105 | + assert mapped_result.unwrap() == 20 |
| 106 | + |
| 107 | + error_result = Result.Err("Error").apply(lambda x: x * 2) |
| 108 | + assert error_result.is_Err is True |
| 109 | + |
| 110 | + with pytest.raises(ResultErr): |
| 111 | + error_result.expect() |
| 112 | + |
| 113 | + with pytest.raises(ResultErr): |
| 114 | + error_result.raised() |
| 115 | + |
| 116 | + with pytest.raises(ResultErr): |
| 117 | + # apply returns Err for bad function |
| 118 | + mapped_result = Ok(0).apply(lambda x: 10 / x) |
| 119 | + mapped_result.raised() |
| 120 | + |
| 121 | + |
| 122 | +def test_result_map(): |
| 123 | + result = Result.Ok(10) |
| 124 | + mapped_result = result.map(lambda x: x * 2) |
| 125 | + assert mapped_result.is_Ok is True |
| 126 | + assert mapped_result.unwrap() == 20 |
| 127 | + |
| 128 | + error_result = Result.Err("Error").map(lambda x: x * 2) |
| 129 | + assert error_result.is_Err is True |
| 130 | + |
| 131 | + with pytest.raises(ResultErr): |
| 132 | + error_result.expect() |
| 133 | + |
| 134 | + with pytest.raises(ResultErr): |
| 135 | + error_result.raised() |
| 136 | + |
| 137 | + with pytest.raises(ZeroDivisionError): |
| 138 | + # map raises exception for bad function |
| 139 | + mapped_result = Ok(0).map(lambda x: 10 / x) |
| 140 | + |
| 141 | + |
| 142 | +def test_result_map_or(): |
| 143 | + result = Result.Ok(0) |
| 144 | + with pytest.raises(ZeroDivisionError): |
| 145 | + mapped_result = result.map_or(100, lambda x: 10 / x) # map fails on bad function call |
| 146 | + |
| 147 | + error_result = Result.Err("Error Happened") |
| 148 | + mapped_result = error_result.map_or(99, lambda x: 10 / x) # Immediately replaces Err with 99 |
| 149 | + assert mapped_result.expect() == 99 |
| 150 | + |
| 151 | + |
| 152 | +def test_result_apply_or(): |
| 153 | + result = Ok(0) |
| 154 | + mapped_result = result.apply_or(100, lambda x: 10 / x) |
| 155 | + assert mapped_result.expect() == 100 |
| 156 | + |
| 157 | + error_result = Err("Error Happened") |
| 158 | + mapped_result = error_result.apply_or(99, lambda x: 10 / x) |
| 159 | + assert mapped_result.expect() == 99 |
| 160 | + |
| 161 | + |
| 162 | +def test_result_map_err(): |
| 163 | + result = Result.Err("Initial error") |
| 164 | + mapped_error = result.map_Err(lambda e: f"{e.str()} - mapped") |
| 165 | + assert mapped_error.is_Err is False # map_Err returns OK(f(e)) |
| 166 | + |
| 167 | + ok_result = Result.Ok(10).map_Err(lambda e: f"{e.str()} - mapped") |
| 168 | + assert ok_result.is_Ok is True |
| 169 | + assert ok_result.expect() == 10 |
| 170 | + |
| 171 | + |
| 172 | +def test_result_apply_chain(): |
| 173 | + new_result = ( |
| 174 | + Ok(10) |
| 175 | + .apply(lambda x: Result.Ok(x * 2)) # Ok(20) |
| 176 | + .apply(lambda x: Result.Ok(x * 2)) # Ok(40) |
| 177 | + .apply(lambda x: Result.Ok(x * 2)) # Ok(80) |
| 178 | + .apply(lambda x: Result.Ok(x * 2)) # Ok(160) |
| 179 | + ) |
| 180 | + assert new_result.is_Ok is True |
| 181 | + assert new_result.expect() == 160 |
| 182 | + |
| 183 | + error_result = ( |
| 184 | + Err("Error") |
| 185 | + .apply(lambda x: Result.Ok(x * 2)) # Appends to error |
| 186 | + .apply(lambda x: Result.Ok(x * 2)) # Appends to error |
| 187 | + .apply(lambda x: Result.Ok(x * 2)) # Appends to error |
| 188 | + ) |
| 189 | + assert error_result.is_Err is True |
| 190 | + with pytest.raises(ResultErr): |
| 191 | + error_result.expect() |
| 192 | + |
| 193 | + result_error = Ok(10) |
| 194 | + # Separated |
| 195 | + new_result = result_error.apply(lambda x: Result.Ok(x * 2)).apply(lambda x: Result.Ok(x * 2)) |
| 196 | + assert new_result.is_Err is False |
| 197 | + new_result = new_result.apply(lambda x: Result.Ok(x * 0)).apply(lambda x: Result.Ok(10 / x)) |
| 198 | + assert new_result.is_Err is True |
| 199 | + |
| 200 | + with pytest.raises(ResultErr): |
| 201 | + new_result = ( |
| 202 | + Ok(10) |
| 203 | + .apply(lambda x: Result.Ok(x * 2)) # Ok(20) |
| 204 | + .apply(lambda x: Result.Ok(x * 2)) # Ok(40) |
| 205 | + .apply(lambda x: Result.Ok(x * 0)) # Ok(40) |
| 206 | + .apply(lambda x: Result.Ok(10 / x)) # Raises ZeroDiv Error |
| 207 | + .apply(lambda x: Result.Ok(x + 1)) # Appends to error message |
| 208 | + .raised() # Raises Exception if in Err state |
| 209 | + ) |
| 210 | + |
| 211 | + |
| 212 | +# Arithmetic Operator Overloading |
| 213 | +def test_addition(): |
| 214 | + result1 = Ok(10) |
| 215 | + result2 = Ok(20) |
| 216 | + combined_result = result1 + result2 |
| 217 | + assert combined_result.unwrap() == 30 |
| 218 | + |
| 219 | + error_result = Result.Ok(10) + Result.Err("Error") |
| 220 | + assert error_result.is_Err is True |
| 221 | + |
| 222 | + |
| 223 | +def test_subtraction(): |
| 224 | + result1 = Result.Ok(50) |
| 225 | + result2 = Result.Ok(20) |
| 226 | + combined_result = result1 - result2 |
| 227 | + assert combined_result.unwrap() == 30 |
| 228 | + |
| 229 | + error_result = Result.Ok(50) - Result.Err("Error") |
| 230 | + assert error_result.is_Err is True |
| 231 | + |
| 232 | + combined_result = 50 - result2 |
| 233 | + assert combined_result.unwrap() == 30 |
| 234 | + |
| 235 | + combined_result = result1 - 20 |
| 236 | + assert combined_result.unwrap() == 30 |
| 237 | + |
| 238 | + |
| 239 | +def test_multiplication(): |
| 240 | + result1 = Ok(10) |
| 241 | + result2 = Ok(20) |
| 242 | + combined_result = result1 * result2 |
| 243 | + assert combined_result.unwrap() == 200 |
| 244 | + |
| 245 | + combined_result = result2 * result1 |
| 246 | + assert combined_result.unwrap() == 200 |
| 247 | + |
| 248 | + error_result = Result.Ok(10) * Result.Err("Error") |
| 249 | + assert error_result.is_Err is True |
| 250 | + |
| 251 | + |
| 252 | +# Edge Cases for Arithmetic |
| 253 | +def test_division_by_zero(): |
| 254 | + result = Ok(10) |
| 255 | + zero_result = Ok(0) |
| 256 | + with pytest.raises(ResultErr): |
| 257 | + ans = result / zero_result |
| 258 | + ans.raised() |
| 259 | + |
| 260 | + |
| 261 | +def test_modulo(): |
| 262 | + result1 = Ok(10) |
| 263 | + result2 = Ok(3) |
| 264 | + combined_result = result1 % result2 |
| 265 | + assert combined_result.unwrap() == 1 |
| 266 | + |
| 267 | + combined_result = result2 % result1 |
| 268 | + assert combined_result.unwrap() == 3 |
| 269 | + |
| 270 | + combined_result = 10 % result2 |
| 271 | + assert combined_result.unwrap() == 1 |
| 272 | + |
| 273 | + combined_result = result1 % 3 |
| 274 | + assert combined_result.unwrap() == 1 |
| 275 | + |
| 276 | + error_result = Result.Ok(10) % Result.Err("Error") |
| 277 | + assert error_result.is_Err is True |
| 278 | + |
| 279 | + |
| 280 | +def test_pow(): |
| 281 | + result1 = Ok(10) |
| 282 | + result2 = Ok(3) |
| 283 | + combined_result = result1**result2 |
| 284 | + assert combined_result.unwrap() == 1000 |
| 285 | + |
| 286 | + combined_result = result1**3 |
| 287 | + assert combined_result.unwrap() == 1000 |
| 288 | + |
| 289 | + combined_result = 10**result2 |
| 290 | + assert combined_result.unwrap() == 1000 |
| 291 | + |
| 292 | + error_result = Result.Ok(10) ** Result.Err("Error") |
| 293 | + assert error_result.is_Err is True |
| 294 | + |
| 295 | + |
| 296 | +# Error Logging Tests |
| 297 | +def test_error_message_integrity(): |
| 298 | + error_message = "Critical Error" |
| 299 | + result = Err(error_message) |
| 300 | + assert result.Err_msg == ["Critical Error"] |
| 301 | + with pytest.raises(ResultErr): |
| 302 | + result.raised() |
| 303 | + |
| 304 | + |
| 305 | +def test_error_chaining_integrity(): |
| 306 | + error_result = Err("Initial Error") |
| 307 | + chained_result = error_result.map(lambda x: x * 2).map_Err(lambda e: f"{e.str()} - chained") |
| 308 | + assert chained_result.is_Err is False # map_Err returns Ok(f(e)) |
| 309 | + |
| 310 | + |
| 311 | +# Integration Tests |
| 312 | +def test_real_world_scenario(): |
| 313 | + def divide(a, b): |
| 314 | + if b == 0: |
| 315 | + return Err("Division by zero") |
| 316 | + return Ok(a / b) |
| 317 | + |
| 318 | + result = divide(10, 2).map(lambda x: x * 5).map(lambda x: int(x)) |
| 319 | + assert result.is_Ok is True |
| 320 | + assert result.expect() == 25 |
| 321 | + |
| 322 | + error_result = divide(10, 0).map(lambda x: x * 5) |
| 323 | + assert error_result.is_Err is True |
| 324 | + with pytest.raises(ResultErr): |
| 325 | + error_result.expect() |
0 commit comments