Updates for issue #450:

Adding support for open_paren_new_line option.

  1)  Added tests
  2)  Added code
This commit is contained in:
Jeremiah Leary 2021-01-31 13:43:35 -06:00
parent 0434eca997
commit 0d228d00a8
3 changed files with 65 additions and 0 deletions

View File

@ -71,6 +71,7 @@ class rule_012(rule.Rule):
_check_first_paren_new_line(self, oToi)
_check_last_paren_new_line(self, oToi)
_check_open_paren_new_line(self, oToi)
# for oNewToi in lToi:
@ -245,6 +246,31 @@ def _check_last_paren_new_line(self, oToi):
break
def _check_open_paren_new_line(self, oToi):
if self.open_paren_new_line == 'Ignore':
return
iLine, lTokens = utils.get_toi_parameters(oToi)
for iToken, oToken in enumerate(lTokens):
iLine = utils.increment_line_number(iLine, oToken)
if isinstance(oToken, token.constant_declaration.assignment_operator):
bSearch = True
if isinstance(oToken, parser.open_parenthesis) and bSearch:
if utils.is_token_at_end_of_line(iToken, lTokens):
if not self.open_paren_new_line:
iEnd = utils.find_carriage_return(lTokens, iToken)
sSolution = 'Remove carriage return after open parenthesis.'
oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iEnd), sSolution)
self.add_violation(oViolation)
else:
if self.open_paren_new_line:
sSolution = 'Add carriage return after open parenthesis.'
oViolation = violation.New(iLine, oToi.extract_tokens(iToken, iToken), sSolution)
self.add_violation(oViolation)
def _is_open_paren_after_assignment(oToi):

View File

@ -25,6 +25,7 @@ class test_constant_rule(unittest.TestCase):
oRule = constant.rule_012()
oRule.first_paren_new_line = True
oRule.last_paren_new_line = 'Ignore'
oRule.open_paren_new_line = 'Ignore'
self.assertTrue(oRule)
self.assertEqual(oRule.name, 'constant')
self.assertEqual(oRule.identifier, '012')
@ -38,6 +39,7 @@ class test_constant_rule(unittest.TestCase):
oRule = constant.rule_012()
oRule.first_paren_new_line = False
oRule.last_paren_new_line = 'Ignore'
oRule.open_paren_new_line = 'Ignore'
lExpected = [14, 17, 21, 27, 41, 57]
@ -48,6 +50,7 @@ class test_constant_rule(unittest.TestCase):
oRule = constant.rule_012()
oRule.first_paren_new_line = 'Ignore'
oRule.last_paren_new_line = True
oRule.open_paren_new_line = 'Ignore'
lExpected = [11, 14]
@ -58,12 +61,35 @@ class test_constant_rule(unittest.TestCase):
oRule = constant.rule_012()
oRule.first_paren_new_line = 'Ignore'
oRule.last_paren_new_line = False
oRule.open_paren_new_line = 'Ignore'
lExpected = [18, 24, 38, 54, 72]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_rule_012_open_paren_new_line_true(self):
oRule = constant.rule_012()
oRule.first_paren_new_line = 'Ignore'
oRule.last_paren_new_line = 'Ignore'
oRule.open_paren_new_line = True
lExpected = [11, 11, 14, 14, 14, 17, 17, 17, 22, 23, 28, 33]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
def test_rule_012_open_paren_new_line_false(self):
oRule = constant.rule_012()
oRule.first_paren_new_line = 'Ignore'
oRule.last_paren_new_line = 'Ignore'
oRule.open_paren_new_line = False
lExpected = [10, 21, 27, 41, 42, 48, 57, 58, 65]
oRule.analyze(self.oFile)
self.assertEqual(lExpected, utils.extract_violation_lines_from_violation_object(oRule.violations))
# def test_fix_rule_012(self):
# oRule = constant.rule_012()
#

View File

@ -638,3 +638,16 @@ def read_vhdlfile(sFileName):
return lLines, None
except OSError as e:
return [], e
def is_token_at_end_of_line(iToken, lTokens):
iMyToken = iToken + 1
if are_next_consecutive_token_types([parser.carriage_return], iMyToken, lTokens):
return True
if are_next_consecutive_token_types([parser.whitespace, parser.carriage_return], iMyToken, lTokens):
return True
if are_next_consecutive_token_types([parser.comment, parser.carriage_return], iMyToken, lTokens):
return True
if are_next_consecutive_token_types([parser.whitespace, parser.comment, parser.carriage_return], iMyToken, lTokens):
return True
return False