def test_clone_attribute(self): attr = AttrFactory.create( restrictions=Restrictions(length=1), types=[ AttrTypeFactory.create(qname="x"), AttrTypeFactory.create(qname="y"), AttrTypeFactory.xs_int(), ], ) restrictions = Restrictions(length=2) clone = ClassUtils.clone_attribute(attr, restrictions) self.assertEqual(2, clone.restrictions.length) self.assertIsNot(attr, clone)
def test_clone_attribute(self): attr = AttrFactory.create( restrictions=RestrictionsFactory.create(length=1), types=[ AttrTypeFactory.create(name="foo:x"), AttrTypeFactory.create(name="y"), AttrTypeFactory.xs_int(), ], ) restrictions = RestrictionsFactory.create(length=2) prefix = "foo" clone = ClassUtils.clone_attribute(attr, restrictions, prefix) self.assertEqual(["foo:x", "foo:y", "integer"], [x.name for x in clone.types]) self.assertEqual(2, clone.restrictions.length) self.assertIsNot(attr, clone)
def process_attribute(self, target: Class, attr: Attr): """ Check if the given attribute matches any substitution class in order to clone it's attributes to the target class. The cloned attributes are placed below the attribute the are supposed to substitute. """ index = target.attrs.index(attr) assert self.substitutions is not None for attr_type in attr.types: for substitution in self.substitutions.get(attr_type.qname, []): clone = ClassUtils.clone_attribute(substitution, attr.restrictions) pos = collections.find(target.attrs, clone) index = pos + 1 if pos > -1 else index target.attrs.insert(index, clone) self.process_attribute(target, clone)