|
| 1 | +from exceptiongroup import ExceptionGroup, split |
| 2 | + |
| 3 | + |
| 4 | +def raise_error(err): |
| 5 | + raise err |
| 6 | + |
| 7 | + |
| 8 | +def raise_error_from_another(out_err, another_err): |
| 9 | + # use try..except approache so out_error have meaningful |
| 10 | + # __context__, __cause__ attribute. |
| 11 | + try: |
| 12 | + raise another_err |
| 13 | + except Exception as e: |
| 14 | + raise out_err from e |
| 15 | + |
| 16 | + |
| 17 | +def test_split_for_none_exception(): |
| 18 | + matched, unmatched = split(RuntimeError, None) |
| 19 | + assert matched is None |
| 20 | + assert unmatched is None |
| 21 | + |
| 22 | + |
| 23 | +def test_split_when_all_exception_matched(): |
| 24 | + group = ExceptionGroup( |
| 25 | + "Many Errors", |
| 26 | + [RuntimeError("Runtime Error1"), RuntimeError("Runtime Error2")], |
| 27 | + ["Runtime Error1", "Runtime Error2"] |
| 28 | + ) |
| 29 | + matched, unmatched = split(RuntimeError, group) |
| 30 | + assert matched is group |
| 31 | + assert unmatched is None |
| 32 | + |
| 33 | + |
| 34 | +def test_split_when_all_exception_unmatched(): |
| 35 | + group = ExceptionGroup( |
| 36 | + "Many Errors", |
| 37 | + [RuntimeError("Runtime Error1"), RuntimeError("Runtime Error2")], |
| 38 | + ["Runtime Error1", "Runtime Error2"] |
| 39 | + ) |
| 40 | + matched, unmatched = split(ValueError, group) |
| 41 | + assert matched is None |
| 42 | + assert unmatched is group |
| 43 | + |
| 44 | + |
| 45 | +def test_split_when_contains_matched_and_unmatched(): |
| 46 | + error1 = RuntimeError("Runtime Error1") |
| 47 | + error2 = ValueError("Value Error2") |
| 48 | + group = ExceptionGroup( |
| 49 | + "Many Errors", |
| 50 | + [error1, error2], |
| 51 | + ['Runtime Error1', 'Value Error2'] |
| 52 | + ) |
| 53 | + matched, unmatched = split(RuntimeError, group) |
| 54 | + assert isinstance(matched, ExceptionGroup) |
| 55 | + assert isinstance(unmatched, ExceptionGroup) |
| 56 | + assert matched.exceptions == [error1] |
| 57 | + assert matched.message == "Many Errors" |
| 58 | + assert matched.sources == ['Runtime Error1'] |
| 59 | + assert unmatched.exceptions == [error2] |
| 60 | + assert unmatched.message == "Many Errors" |
| 61 | + assert unmatched.sources == ['Value Error2'] |
| 62 | + |
| 63 | + |
| 64 | +def test_split_with_predicate(): |
| 65 | + def _match(err): |
| 66 | + return str(err) != 'skip' |
| 67 | + |
| 68 | + error1 = RuntimeError("skip") |
| 69 | + error2 = RuntimeError("Runtime Error") |
| 70 | + group = ExceptionGroup( |
| 71 | + "Many Errors", |
| 72 | + [error1, error2], |
| 73 | + ['skip', 'Runtime Error'] |
| 74 | + ) |
| 75 | + matched, unmatched = split(RuntimeError, group, match=_match) |
| 76 | + assert matched.exceptions == [error2] |
| 77 | + assert unmatched.exceptions == [error1] |
| 78 | + |
| 79 | + |
| 80 | +def test_split_with_single_exception(): |
| 81 | + err = RuntimeError("Error") |
| 82 | + matched, unmatched = split(RuntimeError, err) |
| 83 | + assert matched is err |
| 84 | + assert unmatched is None |
| 85 | + |
| 86 | + matched, unmatched = split(ValueError, err) |
| 87 | + assert matched is None |
| 88 | + assert unmatched is err |
| 89 | + |
| 90 | + |
| 91 | +def test_split_and_check_attributes_same(): |
| 92 | + try: |
| 93 | + raise_error(RuntimeError("RuntimeError")) |
| 94 | + except Exception as e: |
| 95 | + run_error = e |
| 96 | + |
| 97 | + try: |
| 98 | + raise_error(ValueError("ValueError")) |
| 99 | + except Exception as e: |
| 100 | + val_error = e |
| 101 | + |
| 102 | + group = ExceptionGroup( |
| 103 | + "ErrorGroup", [run_error, val_error], ["RuntimeError", "ValueError"] |
| 104 | + ) |
| 105 | + # go and check __traceback__, __cause__ attributes |
| 106 | + try: |
| 107 | + raise_error_from_another(group, RuntimeError("Cause")) |
| 108 | + except BaseException as e: |
| 109 | + new_group = e |
| 110 | + |
| 111 | + matched, unmatched = split(RuntimeError, group) |
| 112 | + assert matched.__traceback__ is new_group.__traceback__ |
| 113 | + assert matched.__cause__ is new_group.__cause__ |
| 114 | + assert matched.__context__ is new_group.__context__ |
| 115 | + assert unmatched.__traceback__ is new_group.__traceback__ |
| 116 | + assert unmatched.__cause__ is new_group.__cause__ |
| 117 | + assert unmatched.__context__ is new_group.__context__ |
0 commit comments