def test_constructor(): msg = Strings(string_value='foo') assert 'foo' == msg.string_value with pytest.raises(AssertionError): Strings(unknown_field='test')
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_constructor(): a = Strings(empty_string='foo') assert 'foo' == a.empty_string with pytest.raises(AssertionError): Strings(unknown_field='test')
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_check_constraints(): a = Strings() a.empty_string = 'test' assert(a.empty_string == 'test') assert_raises(AssertionError, setattr, a, 'empty_string', 1234) a.ub_string = 'a' * 22 assert(a.ub_string == 'a' * 22) assert_raises(AssertionError, setattr, a, 'ub_string', 'a' * 23) b = Nested() primitives = Primitives() b.primitives = primitives assert(b.primitives == primitives) assert_raises(AssertionError, setattr, b, 'primitives', 'foo') list_of_primitives = [primitives, primitives] tuple_of_primitives = (primitives, primitives) b.two_primitives = list_of_primitives assert(b.two_primitives == list_of_primitives) assert(type(b.two_primitives) == list) b.two_primitives = tuple_of_primitives assert(b.two_primitives == tuple_of_primitives) assert(type(b.two_primitives) == tuple) assert_raises(AssertionError, setattr, b, 'two_primitives', Primitives()) assert_raises(AssertionError, setattr, b, 'two_primitives', [Primitives()]) assert_raises(AssertionError, setattr, b, 'two_primitives', [primitives, primitives, primitives]) # TODO(wjwwood): reenable when bounded arrays are working C type support. # b.up_to_three_primitives = [] # assert(b.up_to_three_primitives == []) # b.up_to_three_primitives = [primitives] # assert(b.up_to_three_primitives == [primitives]) # b.up_to_three_primitives = [primitives, primitives] # assert(b.up_to_three_primitives == [primitives, primitives]) # b.up_to_three_primitives = [primitives, primitives, primitives] # assert(b.up_to_three_primitives == [primitives, primitives, primitives]) # assert_raises(AssertionError, setattr, b, 'up_to_three_primitives', # [primitives, primitives, primitives, primitives]) b.unbounded_primitives = [primitives, primitives] assert(b.unbounded_primitives == [primitives, primitives]) c = Various() c.byte_value = b'a' assert(c.byte_value == b'a') assert(c.byte_value != 'a') assert_raises(AssertionError, setattr, c, 'byte_value', 'a') assert_raises(AssertionError, setattr, c, 'byte_value', b'abc') assert_raises(AssertionError, setattr, c, 'byte_value', 'abc') c.char_value = 'a' assert(c.char_value == 'a') assert(c.char_value != b'a') assert_raises(AssertionError, setattr, c, 'char_value', b'a') assert_raises(AssertionError, setattr, c, 'char_value', 'abc') assert_raises(AssertionError, setattr, c, 'char_value', b'abc')
def test_default_values(): a = Strings() assert(a.empty_string is None) assert(a.def_string == 'Hello world!') a.def_string = 'Bye world' assert(a.def_string == 'Bye world') assert(Strings.DEF_STRING__DEFAULT == 'Hello world!') assert(a.DEF_STRING__DEFAULT == 'Hello world!') assert_raises(AttributeError, setattr, Strings, 'DEF_STRING__DEFAULT', 'bar') assert(Various.TWO_UINT16_VALUE__DEFAULT == (5, 23))
def test_invalid_attribute(): a = Strings() with pytest.raises(AttributeError): setattr(a, 'invalid_string1', 'foo') with pytest.raises(AttributeError): getattr(a, 'invalid_string2')
def test_strings(): msg = Strings() # types assert isinstance(msg.string_value, str) assert isinstance(msg.string_value_default1, str) assert isinstance(msg.bounded_string_value_default1, str) # default values assert '' == msg.string_value assert 'Hello world!' == msg.string_value_default1 assert "Hello'world!" == msg.string_value_default2 assert 'Hello"world!' == msg.string_value_default3 assert "Hello'world!" == msg.string_value_default4 assert 'Hello"world!' == msg.string_value_default5 assert 'Hello world!' == Strings.STRING_CONST assert '' == msg.bounded_string_value assert 'Hello world!' == msg.bounded_string_value_default1 assert "Hello'world!" == msg.bounded_string_value_default2 assert 'Hello"world!' == msg.bounded_string_value_default3 assert "Hello'world!" == msg.bounded_string_value_default4 assert 'Hello"world!' == msg.bounded_string_value_default5 # assignment msg.string_value = 'foo' assert 'foo' == msg.string_value msg.string_value_default1 = 'bar' assert 'bar' == msg.string_value_default1 msg.bounded_string_value = 'foo bounded' assert 'foo bounded' == msg.bounded_string_value msg.bounded_string_value_default1 = 'bar bounded' assert 'bar bounded' == msg.bounded_string_value_default1 # set invalid value type with pytest.raises(AssertionError): setattr(msg, 'string_value', 1234) # get/set invalid attribute with pytest.raises(AttributeError): setattr(msg, 'invalid_string1', 'foo') with pytest.raises(AttributeError): getattr(msg, 'invalid_string2') # out of bounds msg.bounded_string_value = 'a' * 22 assert 'a' * 22 == msg.bounded_string_value with pytest.raises(AssertionError): setattr(msg, 'bounded_string_value', 'a' * 23) msg.bounded_string_value_default1 = 'a' * 22 assert 'a' * 22 == msg.bounded_string_value_default1 with pytest.raises(AssertionError): setattr(msg, 'bounded_string_value_default1', 'a' * 23)
def test_default_values(): a = Strings() assert a.empty_string is '' 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_raises(AttributeError, setattr, Strings, 'DEF_STRING__DEFAULT', 'bar') b = Various() assert [5, 23] == b.TWO_UINT16_VALUE__DEFAULT assert [5, 23] == b.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT assert '\x01' == b.CHAR_VALUE__DEFAULT assert '1' != b.CHAR_VALUE__DEFAULT assert b'\x01' == b.BYTE_VALUE__DEFAULT assert b'1' != b.BYTE_VALUE__DEFAULT
def test_default_values(): a = Strings() assert a.empty_string is '' 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_STRING_DELIMITER__DEFAULT assert "Hello'world!" == a.DEF_STRING_DELIMITER2__DEFAULT assert_raises(AttributeError, setattr, Strings, 'DEF_STRING__DEFAULT', 'bar') b = Various() assert [5, 23] == b.TWO_UINT16_VALUE__DEFAULT assert [5, 23] == b.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT assert '\x01' == b.CHAR_VALUE__DEFAULT assert '1' != b.CHAR_VALUE__DEFAULT assert b'\x01' == b.BYTE_VALUE__DEFAULT assert b'1' != b.BYTE_VALUE__DEFAULT
def test_arrays_of_bounded_strings(): a = Strings() 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 assert_raises( AssertionError, setattr, a, 'ub_string_static_array_value', array_too_long_strings) assert_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 assert_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 assert_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 assert_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
def test_arrays_of_bounded_strings(): a = Strings() 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 assert_raises(AssertionError, setattr, a, 'ub_string_static_array_value', array_too_long_strings) assert_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 assert_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 assert_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 assert_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
def test_strings(): a = Strings() assert '' == a.empty_string assert 'Hello world!' == a.def_string
def test_check_constraints(): a = Strings() a.empty_string = 'test' assert 'test' == a.empty_string with pytest.raises(AssertionError): setattr(a, 'empty_string', 1234) a.ub_string = 'a' * 22 assert 'a' * 22 == a.ub_string with pytest.raises(AssertionError): setattr(a, 'ub_string', 'a' * 23) b = Nested() primitives = Primitives() b.primitives = primitives assert b.primitives == primitives with pytest.raises(AssertionError): setattr(b, 'primitives', 'foo') list_of_primitives = [primitives, primitives] tuple_of_primitives = (primitives, primitives) b.two_primitives = list_of_primitives assert b.two_primitives == list_of_primitives assert type(b.two_primitives) == list b.two_primitives = tuple_of_primitives assert b.two_primitives == tuple_of_primitives assert type(b.two_primitives) == tuple with pytest.raises(AssertionError): setattr(b, 'two_primitives', Primitives()) with pytest.raises(AssertionError): setattr(b, 'two_primitives', [Primitives()]) with pytest.raises(AssertionError): setattr(b, 'two_primitives', [primitives, primitives, primitives]) b.up_to_three_primitives = [] assert [] == b.up_to_three_primitives b.up_to_three_primitives = [primitives] assert [primitives] == b.up_to_three_primitives b.up_to_three_primitives = [primitives, primitives] assert [primitives, primitives] == b.up_to_three_primitives b.up_to_three_primitives = [primitives, primitives, primitives] assert [primitives, primitives, primitives] == b.up_to_three_primitives with pytest.raises(AssertionError): setattr(b, 'up_to_three_primitives', [primitives, primitives, primitives, primitives]) b.unbounded_primitives = [primitives, primitives] assert [primitives, primitives] == b.unbounded_primitives c = Various() c.byte_value = b'a' assert b'a' == c.byte_value assert 'a' != c.byte_value with pytest.raises(AssertionError): setattr(c, 'byte_value', 'a') with pytest.raises(AssertionError): setattr(c, 'byte_value', b'abc') with pytest.raises(AssertionError): setattr(c, 'byte_value', 'abc') c.char_value = ord('a') assert ord('a') == c.char_value assert b'a' != c.char_value with pytest.raises(AssertionError): setattr(c, 'char_value', b'a') with pytest.raises(AssertionError): setattr(c, 'char_value', 'abc') with pytest.raises(AssertionError): setattr(c, 'char_value', b'abc') c.up_to_three_int32_values = [] assert [] == c.up_to_three_int32_values.tolist() c.up_to_three_int32_values = [12345, -12345] assert [12345, -12345] == c.up_to_three_int32_values.tolist() c.up_to_three_int32_values = [12345, -12345, 6789] assert [12345, -12345, 6789] == c.up_to_three_int32_values.tolist() c.up_to_three_int32_values = [12345, -12345, 6789] with pytest.raises(AssertionError): setattr(c, 'up_to_three_int32_values', [12345, -12345, 6789, -6789]) c.up_to_three_string_values = [] assert [] == c.up_to_three_string_values c.up_to_three_string_values = ['foo', 'bar'] assert ['foo', 'bar'] == c.up_to_three_string_values c.up_to_three_string_values = ['foo', 'bar', 'baz'] assert ['foo', 'bar', 'baz'] == c.up_to_three_string_values with pytest.raises(AssertionError): setattr(c, 'up_to_three_string_values', ['foo', 'bar', 'baz', 'hello'])
def test_invalid_attribute(): a = Strings() assert_raises(AttributeError, setattr, a, 'invalid_string1', 'foo') assert_raises(AttributeError, getattr, a, 'invalid_string2')
def test_constructor(): a = Strings(empty_string='foo') assert 'foo' == a.empty_string assert_raises(AssertionError, Strings, unknown_field='test')
def test_check_constraints(): a = Strings() a.empty_string = 'test' assert 'test' == a.empty_string assert_raises(AssertionError, setattr, a, 'empty_string', 1234) a.ub_string = 'a' * 22 assert 'a' * 22 == a.ub_string assert_raises(AssertionError, setattr, a, 'ub_string', 'a' * 23) b = Nested() primitives = Primitives() b.primitives = primitives assert b.primitives == primitives assert_raises(AssertionError, setattr, b, 'primitives', 'foo') list_of_primitives = [primitives, primitives] tuple_of_primitives = (primitives, primitives) b.two_primitives = list_of_primitives assert b.two_primitives == list_of_primitives assert type(b.two_primitives) == list b.two_primitives = tuple_of_primitives assert b.two_primitives == tuple_of_primitives assert type(b.two_primitives) == tuple assert_raises(AssertionError, setattr, b, 'two_primitives', Primitives()) assert_raises(AssertionError, setattr, b, 'two_primitives', [Primitives()]) assert_raises(AssertionError, setattr, b, 'two_primitives', [primitives, primitives, primitives]) b.up_to_three_primitives = [] assert [] == b.up_to_three_primitives b.up_to_three_primitives = [primitives] assert [primitives] == b.up_to_three_primitives b.up_to_three_primitives = [primitives, primitives] assert [primitives, primitives] == b.up_to_three_primitives b.up_to_three_primitives = [primitives, primitives, primitives] assert [primitives, primitives, primitives] == b.up_to_three_primitives assert_raises(AssertionError, setattr, b, 'up_to_three_primitives', [primitives, primitives, primitives, primitives]) b.unbounded_primitives = [primitives, primitives] assert [primitives, primitives] == b.unbounded_primitives c = Various() c.byte_value = b'a' assert b'a' == c.byte_value assert 'a' != c.byte_value assert_raises(AssertionError, setattr, c, 'byte_value', 'a') assert_raises(AssertionError, setattr, c, 'byte_value', b'abc') assert_raises(AssertionError, setattr, c, 'byte_value', 'abc') c.char_value = 'a' assert 'a' == c.char_value assert b'a' != c.char_value assert_raises(AssertionError, setattr, c, 'char_value', b'a') assert_raises(AssertionError, setattr, c, 'char_value', 'abc') assert_raises(AssertionError, setattr, c, 'char_value', b'abc') c.up_to_three_int32_values = [] assert [] == c.up_to_three_int32_values c.up_to_three_int32_values = [12345, -12345] assert [12345, -12345] == c.up_to_three_int32_values c.up_to_three_int32_values = [12345, -12345, 6789] assert [12345, -12345, 6789] == c.up_to_three_int32_values c.up_to_three_int32_values = [12345, -12345, 6789] assert_raises( AssertionError, setattr, c, 'up_to_three_int32_values', [12345, -12345, 6789, -6789]) c.up_to_three_string_values = [] assert [] == c.up_to_three_string_values c.up_to_three_string_values = ['foo', 'bar'] assert ['foo', 'bar'] == c.up_to_three_string_values c.up_to_three_string_values = ['foo', 'bar', 'baz'] assert ['foo', 'bar', 'baz'] == c.up_to_three_string_values assert_raises( AssertionError, setattr, c, 'up_to_three_string_values', ['foo', 'bar', 'baz', 'hello'])