Example #1
0
    def process_simple_dependency(cls, source: Class, target: Class,
                                  attr: Attr, attr_type: AttrType):
        """
        Replace the given attribute type with the types of the single field
        source class.

        Ignore enumerations and gracefully handle dump types with no attributes.

        :raises: AnalyzerValueError if the source class has more than one attributes
        """
        if source.is_enumeration:
            return

        total = len(source.attrs)
        if total == 0:
            cls.reset_attribute_type(attr_type)
        elif total == 1:
            source_attr = source.attrs[0]
            index = attr.types.index(attr_type)
            attr.types.pop(index)

            for source_attr_type in source_attr.types:
                clone_type = source_attr_type.clone()
                attr.types.insert(index, clone_type)
                index += 1

            restrictions = source_attr.restrictions.clone()
            restrictions.merge(attr.restrictions)
            attr.restrictions = restrictions
            ClassUtils.copy_inner_classes(source, target)
        else:
            raise AnalyzerValueError(
                f"{source.type.__name__} with more than one attribute: `{source.name}`"
            )
Example #2
0
    def test_copy_inner_classes(self, mock_copy_inner_class):
        source = ClassFactory.create()
        target = ClassFactory.create()
        attr = AttrFactory.create(types=AttrTypeFactory.list(3))

        ClassUtils.copy_inner_classes(source, target, attr)

        mock_copy_inner_class.assert_has_calls(
            [
                mock.call(source, target, attr, attr.types[0]),
                mock.call(source, target, attr, attr.types[1]),
                mock.call(source, target, attr, attr.types[2]),
            ]
        )
Example #3
0
    def test_copy_inner_classes(self):
        source = ClassFactory.create(
            inner=ClassFactory.list(2, package="a", module="b"))
        target = ClassFactory.create()

        ClassUtils.copy_inner_classes(source, target)  # All good copy all
        self.assertEqual(2, len(target.inner))

        ClassUtils.copy_inner_classes(source,
                                      target)  # Inner classes exist skip
        self.assertEqual(2, len(target.inner))

        source.inner.append(target)

        attr = AttrFactory.create(types=[
            AttrTypeFactory.create(name=target.name, forward=True),
            AttrTypeFactory.create(name=target.name, forward=False),
            AttrTypeFactory.create(name="foobar"),
        ])
        target.attrs.append(attr)

        ClassUtils.copy_inner_classes(source,
                                      target)  # Inner class matches target
        self.assertEqual(2, len(target.inner))

        for inner in target.inner:
            self.assertEqual(target.package, inner.package)
            self.assertEqual(target.module, inner.module)

        self.assertTrue(attr.types[0].circular)
        self.assertFalse(attr.types[1].circular)
        self.assertFalse(attr.types[2].circular)