def test_string_slot_types():
    b = StringArrays()
    assert hasattr(b, 'SLOT_TYPES')
    assert hasattr(b, '__slots__')
    string_slot_types = StringArrays.SLOT_TYPES
    string_slots = getattr(b, '__slots__')
    assert len(string_slot_types) == len(string_slots)

    assert isinstance(string_slot_types[0], Array)
    assert isinstance(string_slot_types[0].value_type, BoundedString)
    assert string_slot_types[0].size == 3
    assert string_slot_types[0].value_type.maximum_size == 5

    assert isinstance(string_slot_types[1], BoundedSequence)
    assert isinstance(string_slot_types[1].value_type, BoundedString)
    assert string_slot_types[1].maximum_size == 10
    assert string_slot_types[1].value_type.maximum_size == 5

    assert isinstance(string_slot_types[2], UnboundedSequence)
    assert isinstance(string_slot_types[2].value_type, BoundedString)
    assert string_slot_types[2].value_type.maximum_size == 5

    assert isinstance(string_slot_types[3], UnboundedSequence)
    assert isinstance(string_slot_types[3].value_type, UnboundedString)

    assert isinstance(string_slot_types[4], Array)
    assert isinstance(string_slot_types[4].value_type, UnboundedString)
    assert string_slot_types[4].size == 3
def test_string_slot_attributes():
    b = StringArrays()
    assert hasattr(b, 'get_fields_and_field_types')
    assert hasattr(b, '__slots__')
    string_slot_types_dict = getattr(b, 'get_fields_and_field_types')()
    string_slots = getattr(b, '__slots__')
    assert len(string_slot_types_dict) == len(string_slots)
    expected_string_slot_types_dict = {
        'ub_string_static_array_value': 'string<5>[3]',
        'ub_string_ub_array_value': 'sequence<string<5>, 10>',
        'ub_string_dynamic_array_value': 'sequence<string<5>>',
        'string_dynamic_array_value': 'sequence<string>',
        'string_static_array_value': 'string[3]',
        'string_bounded_array_value': 'sequence<string, 10>',
        'def_string_dynamic_array_value': 'sequence<string>',
        'def_string_static_array_value': 'string[3]',
        'def_string_bounded_array_value': 'sequence<string, 10>',
        'def_various_quotes': 'sequence<string>',
        'def_various_commas': 'sequence<string>',
    }

    assert len(string_slot_types_dict) == len(expected_string_slot_types_dict)

    for expected_field, expected_slot_type in expected_string_slot_types_dict.items(
    ):
        assert expected_field in string_slot_types_dict.keys()
        assert expected_slot_type == string_slot_types_dict[expected_field]
Example #3
0
def test_default_values():
    a = Strings()

    assert '' == a.empty_string
    assert 'Hello world!' == a.def_string
    a.def_string = 'Bye world'
    assert 'Bye world' == a.def_string
    assert 'Hello world!' == Strings.DEF_STRING__DEFAULT
    assert 'Hello world!' == a.DEF_STRING__DEFAULT

    assert 'Hello\'world!' == a.DEF_STRING2__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING3__DEFAULT
    assert 'Hello\'world!' == a.DEF_STRING4__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING5__DEFAULT
    with pytest.raises(AttributeError):
        setattr(Strings, 'DEF_STRING__DEFAULT', 'bar')

    b = StringArrays()
    assert ['What', 'a', 'wonderful', 'world',
            '!'] == b.DEF_STRING_DYNAMIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_STATIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_BOUNDED_ARRAY_VALUE__DEFAULT

    assert ['H"el\'lo', 'Wo\'r"ld'] == b.DEF_VARIOUS_QUOTES__DEFAULT
    assert ['Hel,lo', ',World', 'abcd', '!,'] == b.DEF_VARIOUS_COMMAS__DEFAULT

    c = Various()
    assert [5, 23] == c.TWO_UINT16_VALUE__DEFAULT

    assert [5, 23] == c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT

    assert '\x01' == c.CHAR_VALUE__DEFAULT
    assert '1' != c.CHAR_VALUE__DEFAULT
    assert b'\x01' == c.BYTE_VALUE__DEFAULT
    assert b'1' != c.BYTE_VALUE__DEFAULT
def test_modifying_slot_fields_and_types():
    b = StringArrays()
    assert hasattr(b, 'get_fields_and_field_types')
    string_slot_types_dict = getattr(b, 'get_fields_and_field_types')()
    string_slot_types_dict_len = len(string_slot_types_dict)
    string_slot_types_dict[1] = 2
    assert len(getattr(
        b, 'get_fields_and_field_types')()) == string_slot_types_dict_len
def test_default_values():
    a = Strings()

    assert '' == a.empty_string
    assert 'Hello world!' == a.def_string
    a.def_string = 'Bye world'
    assert 'Bye world' == a.def_string
    assert 'Hello world!' == Strings.DEF_STRING__DEFAULT
    assert 'Hello world!' == a.DEF_STRING__DEFAULT

    assert "Hello'world!" == a.DEF_STRING2__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING3__DEFAULT
    assert "Hello'world!" == a.DEF_STRING4__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING5__DEFAULT
    with pytest.raises(AttributeError):
        setattr(Strings, 'DEF_STRING__DEFAULT', 'bar')

    a = WStrings()
    assert '' == a.empty_wstring
    assert 'Hello world!' == a.def_wstring
    a.def_wstring = 'Bye world'
    assert 'Bye world' == a.def_wstring
    assert 'Hello world!' == WStrings.DEF_WSTRING__DEFAULT
    assert 'Hello world!' == a.DEF_WSTRING__DEFAULT

    assert "Hello'world!" == a.DEF_WSTRING2__DEFAULT
    assert 'Hello"world!' == a.DEF_WSTRING3__DEFAULT
    assert "Hello'world!" == a.DEF_WSTRING4__DEFAULT
    assert 'Hello"world!' == a.DEF_WSTRING5__DEFAULT
    with pytest.raises(AttributeError):
        setattr(WStrings, 'DEF_WSTRING__DEFAULT', 'bar')

    b = StringArrays()
    assert ['What', 'a', 'wonderful', 'world',
            '!'] == b.DEF_STRING_DYNAMIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_STATIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_BOUNDED_ARRAY_VALUE__DEFAULT

    assert ['H"el\'lo', 'Wo\'r"ld'] == b.DEF_VARIOUS_QUOTES__DEFAULT
    assert ['Hel,lo', ',World', 'abcd', '!,'] == b.DEF_VARIOUS_COMMAS__DEFAULT

    c = Various()
    assert isinstance(c.TWO_UINT16_VALUE__DEFAULT, numpy.ndarray)
    assert (2, ) == c.TWO_UINT16_VALUE__DEFAULT.shape
    assert numpy.uint16 == c.TWO_UINT16_VALUE__DEFAULT.dtype
    assert [5, 23] == c.TWO_UINT16_VALUE__DEFAULT.tolist()

    assert isinstance(c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT,
                      array.array)
    assert 'i' == \
        c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT.typecode
    assert [5, 23] == \
        c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT.tolist()

    assert 1 == c.CHAR_VALUE__DEFAULT
    assert '1' != c.CHAR_VALUE__DEFAULT
    assert b'\x01' == c.BYTE_VALUE__DEFAULT
    assert b'1' != c.BYTE_VALUE__DEFAULT
def test_arrays_of_bounded_strings():
    a = StringArrays()
    array_valid_string_length = ['a' * 2, 'b' * 3, 'c' * 4]
    array_too_long_strings = ['a' * 2, 'b' * 3, 'c' * 6]
    assert ['', '', ''] == a.ub_string_static_array_value
    a.ub_string_static_array_value = array_valid_string_length
    assert array_valid_string_length == a.ub_string_static_array_value

    with pytest.raises(AssertionError):
        setattr(a, 'ub_string_static_array_value', array_too_long_strings)

    with pytest.raises(AssertionError):
        setattr(a, 'ub_string_static_array_value', ['a' * 2, 'b' * 3])

    assert [] == a.ub_string_ub_array_value
    a.ub_string_ub_array_value = array_valid_string_length
    assert array_valid_string_length == a.ub_string_ub_array_value

    with pytest.raises(AssertionError):
        setattr(a, 'ub_string_ub_array_value', array_too_long_strings)

    array10strings = [] + [str(i) for i in range(10)]
    a.ub_string_ub_array_value = array10strings
    assert array10strings == a.ub_string_ub_array_value

    with pytest.raises(AssertionError):
        setattr(a, 'ub_string_ub_array_value', array10strings + ['gfg'])

    assert [] == a.ub_string_dynamic_array_value
    a.ub_string_dynamic_array_value = array_valid_string_length
    assert array_valid_string_length == a.ub_string_dynamic_array_value

    with pytest.raises(AssertionError):
        setattr(a, 'ub_string_dynamic_array_value', array_too_long_strings)

    array10strings = [] + [str(i) for i in range(10)]
    a.ub_string_dynamic_array_value = array10strings
    assert array10strings == a.ub_string_dynamic_array_value
    array10strings += ['gfg']
    a.ub_string_dynamic_array_value = array10strings
    assert array10strings == a.ub_string_dynamic_array_value