Beispiel #1
0
    def test_should_return_correct_byte_value_when_calling_raw_method(
            self, value, expected_data):
        packet = DummyPacket()
        packet.apples = value
        field = ConditionalField(ShortField('foo', 2),
                                 lambda pkt: pkt.apples in [0, 2])

        assert expected_data == field.raw(packet)  # type: ignore
Beispiel #2
0
    def test_should_return_a_copy_of_field_when_calling_clone_method(self):
        field = ConditionalField(ShortField('foo', 2),
                                 lambda pkt: pkt.apples in [0, 2])
        cloned_field = field.clone()

        assert cloned_field == field
        assert cloned_field is not field
        assert cloned_field._field is not field._field
Beispiel #3
0
    def test_should_find_inner_field_method_when_called_method_is_not_defined_in_conditional_field(
            self):
        class CustomShortField(ShortField):
            @staticmethod
            def return_hello():
                return 'hello'

        field = ConditionalField(CustomShortField('foo', 2), lambda x: x)
        assert 'hello' == field.return_hello()
Beispiel #4
0
    def test_should_correctly_compute_field_value_when_calling_compute_value_method(
            self, apples, expected_data, value, value_was_computed):
        packet = DummyPacket()
        packet.apples = apples
        field = ConditionalField(ShortField('foo', 2),
                                 lambda pkt: pkt.apples in [0, 2])

        assert field.value_was_computed is False
        assert expected_data == field.compute_value(b'\x00\x04hello',
                                                    packet)  # type: ignore
        assert value == field.value
        assert field.value_was_computed is value_was_computed
Beispiel #5
0
    def test_should_correctly_initialize_field(self):
        field = ConditionalField(ShortField('foo', 3), lambda x: x)

        assert 'foo' == field.name
        assert 3 == field.value == field.default
        assert '!H' == field.struct_format
        assert 2 == field.size
        assert callable(field.condition)
Beispiel #6
0
    def test_should_raise_error_if_callable_does_not_take_exactly_one_argument(
            self):
        def condition(x, y):
            return x + y

        with pytest.raises(TypeError) as exc_info:
            ConditionalField(ShortField('foo', 2), condition)  # type: ignore

        message = f'callable {condition.__name__} must takes one parameter (a packet instance) but yours has 2'
        assert message == str(exc_info.value)
Beispiel #7
0
 def test_should_raise_error_when_given_arguments_are_not_correct(
         self, arguments):
     with pytest.raises(TypeError):
         ConditionalField(*arguments)
Beispiel #8
0
    def test_should_return_correct_field_representation_when_calling_repr_function(
            self):
        inner_field = ShortField('foo', 2)
        field = ConditionalField(inner_field, lambda x: x)

        assert repr(inner_field) == repr(field)
Beispiel #9
0
 def test_should_return_a_correct_value_when_calling_random_value_method(
         self):
     field = ConditionalField(ShortField('foo', 2),
                              lambda pkt: pkt.apples in [0, 2])
     assert 0 <= field.random_value() <= RIGHT_SHORT
Beispiel #10
0
    def test_should_update_field_value_when_giving_correct_value(self):
        field = ConditionalField(ShortField('foo', 2), lambda x: x)
        field.value = 5

        assert 5 == field.value
Beispiel #11
0
class Fruit(Packet):
    __fields__ = [
        ShortField('apples', 2),
        ConditionalField(ShortField('pie', 1), lambda p: p.apples <= 2),
        ConditionalField(ShortField('juice', 1), lambda p: p.apples > 2)
    ]