|
| 1 | +# Copyright 2025 Zeppelin Bend Pty Ltd |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | +from _pytest.python_api import raises |
| 6 | + |
| 7 | +from zepben.evolve import StepAction, StepContext |
| 8 | +from zepben.evolve.services.network.tracing.traversal.step_action import T |
| 9 | + |
| 10 | + |
| 11 | +class TestStepAction: |
| 12 | + |
| 13 | + def test_can_apply_lambda(self): |
| 14 | + """Make sure we can use a lambda as the StepAction""" |
| 15 | + captured = [] |
| 16 | + step_action = StepAction(lambda it, ctx: captured.append((it, ctx))) |
| 17 | + |
| 18 | + expected_item = 1 |
| 19 | + expected_ctx = StepContext(is_start_item=True, is_branch_start_item=False) |
| 20 | + |
| 21 | + step_action.apply(expected_item, expected_ctx) |
| 22 | + |
| 23 | + assert captured == [(expected_item, expected_ctx)] |
| 24 | + |
| 25 | + def test_cant_override_apply(self): |
| 26 | + """This is testing that if you ignore the @final on apply, you will get an exception.""" |
| 27 | + with raises(Exception, match="method 'apply' should not be directly overridden, override '_apply' instead."): |
| 28 | + class MyStepAction(StepAction): |
| 29 | + |
| 30 | + # noinspection PyFinal |
| 31 | + def apply(self, item: T, context: StepContext): |
| 32 | + pass |
| 33 | + |
| 34 | + def test_can_apply_descendant(self): |
| 35 | + """Simulate someone doing what the exception told you to do""" |
| 36 | + captured = [] |
| 37 | + |
| 38 | + class MyStepAction(StepAction): |
| 39 | + |
| 40 | + def _apply(self, item: T, context: StepContext): |
| 41 | + captured.append((item, context)) |
| 42 | + |
| 43 | + step_action = MyStepAction() |
| 44 | + |
| 45 | + expected_item = 1 |
| 46 | + expected_ctx = StepContext(is_start_item=True, is_branch_start_item=False) |
| 47 | + |
| 48 | + step_action.apply(expected_item, expected_ctx) |
| 49 | + |
| 50 | + assert captured == [(expected_item, expected_ctx)] |
0 commit comments