def test_should_raise_error_when_all_items_in_tuple_are_not_integers(self): field = BitsField( parts=[FieldPart('version', 4, 4), FieldPart('IHL', 5, 4)], format='B') with pytest.raises(ValueError) as exc_info: field.value = (2, 3.4) assert 'all items in tuple must be integers' == str(exc_info.value)
def test_should_raise_error_when_tuple_is_empty(self): field = BitsField( parts=[FieldPart('version', 4, 4), FieldPart('IHL', 5, 4)], format='B') with pytest.raises(ValueError) as exc_info: field.value = () assert 'value must not be an empty tuple' == str(exc_info.value)
def test_should_raise_error_when_tuple_length_is_different_field_parts_length( self): field = BitsField( parts=[FieldPart('version', 4, 4), FieldPart('IHL', 5, 4)], format='B') with pytest.raises(ValueError) as exc_info: field.value = (2, 3, 4) assert 'tuple length is different from field parts length' == str( exc_info.value)
def test_should_raise_error_when_value_is_not_integer_or_tuple( self, value): field = BitsField( parts=[FieldPart('version', 4, 4), FieldPart('IHL', 5, 4)], format='B') with pytest.raises(TypeError) as exc_info: field.value = value assert 'value must be an integer or a tuple of integers' == str( exc_info.value)
def test_should_raise_error_when_int_value_is_not_in_valid_boundaries( self, size, format_, value_1, value_2): field = BitsField( parts=[FieldPart('version', 4, size), FieldPart('IHL', 5, size)], format=format_) for value in [value_1, value_2]: with pytest.raises(ValueError) as exc_info: field.value = value assert f'integer value must be between 0 and {value_2 - 1}' == str( exc_info.value)
def test_should_raise_error_when_items_dont_have_a_correct_value( self, size, format_, value_1, value_2): field = BitsField( parts=[FieldPart('version', 4, size), FieldPart('IHL', 5, size)], format=format_) for value in [value_1, value_2]: with pytest.raises(ValueError) as exc_info: field.value = (value, 4) message = f'item version must be between 0 and {2 ** size - 1} according to the field part size' assert message == str(exc_info.value)
def test_should_not_raise_error_when_setting_a_correct_tuple_value( self, size, format_): field = BitsField( parts=[FieldPart('version', 4, size), FieldPart('IHL', 5, size)], format=format_) value = (5, 6) try: field.value = value except ValueError: pytest.fail(f'unexpected error when setting value with {value}') assert value == field.value_as_tuple