def _recalculate_templates( self, recalculate_cache: Dict["TemplateTree", "TemplateTree"], minimal_variables: bool, ) -> "TemplateTree": # Check if already recalculated if self in recalculate_cache: return recalculate_cache[self] # Map all children mapped_children = [ child._recalculate_templates(recalculate_cache, minimal_variables) for child in self._children ] new_template = ( Template.merge_all( [c.get_template() for c in mapped_children], minimal_variables, self.get_template(), ) if len(mapped_children) > 0 else self._template ) # Create new result = TemplateTree(new_template, mapped_children) # Cache recalculate_cache[self] = result return result
def test_merge_all(self): t1 = Template([self.slot_x, self.a, self.b]) t2 = Template([self.slot_x, self.a, self.slot_y]) t3 = Template([self.slot_x, self.c]) t12 = Template([self.slot_x, self.a, self.slot1]) self.assertEqual(t12, Template.merge_all([t1, t2])) self.assertEqual(t12, Template.merge_all([t1, t2, t12])) t123 = Template([self.slot_x, self.slot1]) self.assertEqual(t123, Template.merge_all([t12, t3])) self.assertEqual(t123, Template.merge_all([t12, t3, t123])) self.assertEqual(t123, Template.merge_all([t3, t12])) self.assertEqual(t123, Template.merge_all([t3, t12, t123])) self.assertEqual(t123, Template.merge_all([t1, t2, t3])) self.assertEqual(t123, Template.merge_all([t1, t2, t3, t123])) self.assertEqual(t123, Template.merge_all([t3, t2, t1])) self.assertEqual(t123, Template.merge_all([t3, t2, t1, t123]))