Updating the ApplicationSpecificInformation attribute

This change adds some utility methods, including equality operators
and string methods, to the ApplicationSpecificInformation attribute.
Unit tests covering the changes are included.
This commit is contained in:
Peter Hamilton 2016-11-27 11:17:54 -05:00
parent 194412a17f
commit a55310368f
3 changed files with 204 additions and 1 deletions

View File

@ -757,6 +757,40 @@ class ApplicationSpecificInformation(Struct):
"""
self.__validate()
def __repr__(self):
application_namespace = "application_namespace={0}".format(
repr(self.application_namespace)
)
application_data = "application_data={0}".format(
repr(self.application_data)
)
return "ApplicationSpecificInformation({0}, {1})".format(
application_namespace,
application_data
)
def __str__(self):
return str({
"application_namespace": str(self.application_namespace),
"application_data": str(self.application_data)
})
def __eq__(self, other):
if isinstance(other, ApplicationSpecificInformation):
if self.application_namespace != other.application_namespace:
return False
if self.application_data != other.application_data:
return False
return True
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, ApplicationSpecificInformation):
return not self.__eq__(other)
else:
return NotImplemented
def __validate(self):
if not isinstance(self.application_namespace, ApplicationNamespace):
msg = "invalid application namespace"

View File

@ -839,7 +839,7 @@ class TextString(Base):
return "{0}(value={1})".format(type(self).__name__, repr(self.value))
def __str__(self):
return "{0}".format(repr(self.value))
return "{0}".format(str(self.value))
def __eq__(self, other):
if isinstance(other, TextString):

View File

@ -188,6 +188,175 @@ class TestApplicationSpecificInformation(TestCase):
self._test_write(self.encoding, application_namespace,
application_data)
def test_repr(self):
"""
Test that an ApplicationSpecificInformation object can be represented
using repr correctly.
"""
application_specific_info = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace("ssl"),
application_data=ApplicationData("www.example.com")
)
s = repr(application_specific_info)
self.assertEqual(
"ApplicationSpecificInformation("
"application_namespace=ApplicationNamespace(value='ssl'), "
"application_data=ApplicationData(value='www.example.com'))",
s
)
def test_str(self):
"""
Test that an ApplicationSpecificInformation object can be turned into
a string correctly.
"""
application_specific_info = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace("ssl"),
application_data=ApplicationData("www.example.com")
)
s = str(application_specific_info)
self.assertEqual(
str({'application_namespace': 'ssl',
'application_data': 'www.example.com'}
),
s
)
def test_equal_on_equal(self):
"""
Test that the equality operator returns True when comparing two
ApplicationSpecificInformation objects with the same data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
self.assertTrue(a == b)
self.assertTrue(b == a)
def test_equal_on_not_equal_namespace(self):
"""
Test that the equality operator returns False when comparing two
ApplicationSpecificInformation objects with different data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_1'),
application_data=ApplicationData('test_data')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_2'),
application_data=ApplicationData('test_data')
)
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_equal_on_not_equal_data(self):
"""
Test that the equality operator returns False when comparing two
ApplicationSpecificInformation objects with different data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_1')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_2')
)
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_equal_on_type_mismatch(self):
"""
Test that the equality operator returns False when comparing a
ApplicationSpecificInformation object to a
non-ApplicationSpecificInformation object.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
b = "invalid"
self.assertFalse(a == b)
self.assertFalse(b == a)
def test_not_equal_on_equal(self):
"""
Test that the inequality operator returns False when comparing
two ApplicationSpecificInformation objects with the same internal
data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
self.assertFalse(a != b)
self.assertFalse(b != a)
def test_not_equal_on_not_equal_namespace(self):
"""
Test that the inequality operator returns True when comparing two
ApplicationSpecificInformation objects with different data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_1'),
application_data=ApplicationData('test_data')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace_2'),
application_data=ApplicationData('test_data')
)
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_not_equal_on_not_equal_data(self):
"""
Test that the inequality operator returns True when comparing two
ApplicationSpecificInformation objects with different data.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_1')
)
b = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data_2')
)
self.assertTrue(a != b)
self.assertTrue(b != a)
def test_not_equal_on_type_mismatch(self):
"""
Test that the equality operator returns True when comparing a
ApplicationSpecificInformation object to a
non-ApplicationSpecificInformation object.
"""
a = ApplicationSpecificInformation(
application_namespace=ApplicationNamespace('test_namespace'),
application_data=ApplicationData('test_data')
)
b = "invalid"
self.assertTrue(a != b)
self.assertTrue(b != a)
def _test_create(self, application_namespace, application_data):
application_specific_info = ApplicationSpecificInformation.create(
application_namespace, application_data)