def _create_matching_list( self, primary_list: List[interfaces.ParserNode], secondary_list: List[interfaces.ParserNode], ) -> List[Tuple[interfaces.ParserNode, interfaces.ParserNode]]: """ Matches the list of primary_list to a list of secondary_list and returns a list of tuples. This is used to create results for find_ methods. This helper function exists, because we cannot ensure that the list of search results returned by primary.find_* and secondary.find_* are ordered in a same way. The function pairs the same search results from both implementations to a list of tuples. """ matched = [] for p in primary_list: match = None for s in secondary_list: try: assertions.assertEqual(p, s) match = s break except AssertionError: continue if match: matched.append((p, match)) else: raise AssertionError("Could not find a matching node.") return matched
def __init__(self, **kwargs: Any): """ This initialization implementation allows ordinary initialization of CommentNode objects as well as creating a DualCommentNode object using precreated or fetched CommentNode objects if provided as optional arguments primary and secondary. Parameters other than the following are from interfaces.CommentNode: :param CommentNode primary: Primary pre-created CommentNode, mainly used when creating new DualParser nodes using add_* methods. :param CommentNode secondary: Secondary pre-created CommentNode """ kwargs.setdefault("primary", None) kwargs.setdefault("secondary", None) primary = kwargs.pop("primary") secondary = kwargs.pop("secondary") if primary or secondary: assert primary and secondary self.primary = primary self.secondary = secondary else: self.primary = augeasparser.AugeasCommentNode(**kwargs) self.secondary = apacheparser.ApacheCommentNode(**kwargs) assertions.assertEqual(self.primary, self.secondary)
def set_parameters(self, parameters): """ Sets parameters and asserts that both implementation successfully set the parameter sequence """ self.primary.set_parameters(parameters) self.secondary.set_parameters(parameters) assertions.assertEqual(self.primary, self.secondary)
def test_find_coms_second_passing(self): notpassing = [ augeasparser.AugeasCommentNode(comment="notpassing", ancestor=self.block, filepath="/path/to/whatever", metadata=self.metadata) ] passing = [ augeasparser.AugeasCommentNode(comment=assertions.PASS, ancestor=self.block, filepath=assertions.PASS, metadata=self.metadata) ] find_coms_primary = mock.MagicMock(return_value=notpassing) find_coms_secondary = mock.MagicMock(return_value=passing) self.block.primary.find_comments = find_coms_primary self.block.secondary.find_comments = find_coms_secondary comments = self.block.find_comments("something") for comment in comments: try: assertions.assertEqual(comment.primary, comment.secondary) except AssertionError: # pragma: no cover self.fail("Assertion should have passed") self.assertFalse(assertions.isPassComment(comment.primary)) self.assertTrue(assertions.isPassComment(comment.secondary))
def test_find_dirs_second_passing(self): notpassing = [ augeasparser.AugeasDirectiveNode(name="notpassing", ancestor=self.block, filepath="/path/to/whatever", metadata=self.metadata) ] passing = [ augeasparser.AugeasDirectiveNode(name=assertions.PASS, ancestor=self.block, filepath=assertions.PASS, metadata=self.metadata) ] find_dirs_primary = mock.MagicMock(return_value=notpassing) find_dirs_secondary = mock.MagicMock(return_value=passing) self.block.primary.find_directives = find_dirs_primary self.block.secondary.find_directives = find_dirs_secondary directives = self.block.find_directives("something") for directive in directives: try: assertions.assertEqual(directive.primary, directive.secondary) except AssertionError: # pragma: no cover self.fail("Assertion should have passed") self.assertFalse(assertions.isPassDirective(directive.primary)) self.assertTrue(assertions.isPassDirective(directive.secondary))
def test_find_blocks_second_passing(self): youshallnotpass = [ augeasparser.AugeasBlockNode(name="notpassing", ancestor=self.block, filepath="/path/to/whatever", metadata=self.metadata) ] youshallpass = [ augeasparser.AugeasBlockNode(name=assertions.PASS, ancestor=self.block, filepath=assertions.PASS, metadata=self.metadata) ] find_blocks_primary = mock.MagicMock(return_value=youshallnotpass) find_blocks_secondary = mock.MagicMock(return_value=youshallpass) self.block.primary.find_blocks = find_blocks_primary self.block.secondary.find_blocks = find_blocks_secondary blocks = self.block.find_blocks("something") for block in blocks: try: assertions.assertEqual(block.primary, block.secondary) except AssertionError: # pragma: no cover self.fail("Assertion should have passed") self.assertFalse(assertions.isPassDirective(block.primary)) self.assertTrue(assertions.isPassDirective(block.secondary))
def test_parsernode_filepath_assert(self): # disable assertion pass self.comment.primary.comment = "value" self.comment.secondary.comment = "value" self.comment.primary.filepath = "first" self.comment.secondary.filepath = "second" with self.assertRaises(AssertionError): assertions.assertEqual(self.comment.primary, self.comment.secondary)
def add_child_comment(self, comment="", position=None): """ Creates a new child CommentNode, asserts that both implementations did it in a similar way, and returns a newly created DualCommentNode object encapsulating both of the newly created objects """ primary_new = self.primary.add_child_comment(comment, position) secondary_new = self.secondary.add_child_comment(comment, position) assertions.assertEqual(primary_new, secondary_new) return DualCommentNode(primary=primary_new, secondary=secondary_new)
def add_child_directive(self, name, parameters=None, position=None): """ Creates a new child DirectiveNode, asserts that both implementations did it in a similar way, and returns a newly created DualDirectiveNode object encapsulating both of the newly created objects """ primary_new = self.primary.add_child_directive(name, parameters, position) secondary_new = self.secondary.add_child_directive(name, parameters, position) assertions.assertEqual(primary_new, secondary_new) return DualDirectiveNode(primary=primary_new, secondary=secondary_new)
def test_parsernode_dirty_assert(self): # disable assertion pass self.comment.primary.comment = "value" self.comment.secondary.comment = "value" self.comment.primary.filepath = "x" self.comment.secondary.filepath = "x" self.comment.primary.dirty = False self.comment.secondary.dirty = True with self.assertRaises(AssertionError): assertions.assertEqual(self.comment.primary, self.comment.secondary)
def add_child_block(self, name: str, parameters: Optional[str] = None, position: Optional[int] = None) -> "DualBlockNode": """ Creates a new child BlockNode, asserts that both implementations did it in a similar way, and returns a newly created DualBlockNode object encapsulating both of the newly created objects """ primary_new = self.primary.add_child_block(name, parameters, position) secondary_new = self.secondary.add_child_block(name, parameters, position) assertions.assertEqual(primary_new, secondary_new) return DualBlockNode(primary=primary_new, secondary=secondary_new)