Permalink
Browse files

Info.py: Validate value type

Validate value of the information as per the
value_type class variable.

Related to #101
  • Loading branch information...
satwikkansal committed Jun 14, 2017
1 parent b82cd81 commit 478aa440ef59799747184a21c062f0e43c4faaae
Showing with 28 additions and 8 deletions.
  1. +13 −1 coala_quickstart/info_extraction/Info.py
  2. +15 −7 tests/info_extraction/InfoTest.py
@@ -1,19 +1,31 @@
class Info:
description = 'Some information'
# tuple of allowed types for the information value.
value_type = (object,)
def __init__(self,
source,
value,
extractor=None):
extractor=None,
**kwargs):
"""
:param source: Source from where the information is discovered.
:param value: Value of the infomation to be represented.
:param extractor: ``InfoExtractor`` instance used to extract the
information.
"""
if not self.value_type:
self.value_type = (object,)
if not isinstance(value, self.value_type):
raise TypeError("The value {} does not match the allowed "
"value types {}".format(value, self.value_type))
self.source = source
self._value = value
self.extractor = extractor
for key, val in kwargs.items():
setattr(self, key, val)
@property
def value(self):
@@ -25,18 +25,18 @@ def find_information(self, fname, parsed_file):
class InfoA(Info):
description = 'Information A'
value_type = (str, int)
def __init__(self,
source,
value,
extra_param):
super().__init__(source, value)
self.extra_param = extra_param
class InfoB(Info):
description = "Info class without value_type"
self.info_a = InfoA(
'source_file',
'info_a_value',
'extra_param_value')
extra_param='extra_param_value')
self.InfoA = InfoA
self.InfoB = InfoB
def test_main(self):
self.assertEqual(self.base_info.name, 'Info')
@@ -51,3 +51,11 @@ def test_derived_instances(self):
self.assertEqual(self.info_a.source, 'source_file')
self.assertEqual(self.info_a.extra_param, 'extra_param_value')
self.assertEqual(self.info_a.description, 'Information A')
def test_value_type(self):
with self.assertRaisesRegexp(
TypeError,
'The value 5.5 does not match the allowed value types'):
self.InfoA("source_file", 5.5)
self.InfoB("source_file", 5.5)

0 comments on commit 478aa44

Please sign in to comment.