def copy_attribute_properties(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 """ 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 ClassUtils.copy_inner_class(source, target, attr, clone_type) restrictions = source_attr.restrictions.clone() restrictions.merge(attr.restrictions) attr.restrictions = restrictions attr.help = attr.help or source_attr.help if source.nillable: restrictions.nillable = True
def test_copy_inner_class_with_missing_inner(self): source = ClassFactory.create() target = ClassFactory.create() attr = AttrFactory.create() attr_type = AttrTypeFactory.create(forward=True, qname=target.qname) with self.assertRaises(CodeGenerationError): ClassUtils.copy_inner_class(source, target, attr, attr_type)
def test_copy_inner_class_skip_non_forward_reference(self): source = ClassFactory.create() target = ClassFactory.create() attr = AttrFactory.create() attr_type = AttrTypeFactory.create() ClassUtils.copy_inner_class(source, target, attr, attr_type) self.assertFalse(attr_type.circular) self.assertEqual(0, len(target.inner))
def test_copy_inner_class_check_circular_reference(self): source = ClassFactory.create() target = ClassFactory.create() attr = AttrFactory.create() attr_type = AttrTypeFactory.create(forward=True, qname=target.qname) source.inner.append(target) ClassUtils.copy_inner_class(source, target, attr, attr_type) self.assertTrue(attr_type.circular) self.assertEqual(0, len(target.inner))
def test_copy_inner_class(self): source = ClassFactory.create() inner = ClassFactory.create(qname="a", module="b", package="c") target = ClassFactory.create() attr = AttrFactory.create() attr_type = AttrTypeFactory.create(forward=True, qname=inner.qname) source.inner.append(inner) ClassUtils.copy_inner_class(source, target, attr, attr_type) self.assertEqual(1, len(target.inner)) self.assertIsNot(inner, target.inner[0]) self.assertEqual(target.package, target.inner[0].package) self.assertEqual(target.module, target.inner[0].module) self.assertEqual(inner.qname, target.inner[0].qname)