Ejemplo n.º 1
0
def test_top_level_package_with_subpackages(pygen_output_dir):
    rootpkg = EPackage('rootpkg')
    subpkg = EPackage('subpkg')
    cls1 = EClass('A')
    cls2 = EClass('B')
    cls1.eStructuralFeatures.append(EReference('b', cls2))
    cls2.eStructuralFeatures.append(
        EReference('a', cls1, eOpposite=cls1.findEStructuralFeature('b')))
    rootpkg.eClassifiers.append(cls1)
    rootpkg.eSubpackages.append(subpkg)
    subpkg.eClassifiers.append(cls2)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    assert mm.name == rootpkg.name
    assert mm.eSubpackages[0].eSuperPackage.name == rootpkg.name

    generated_A = mm.getEClassifier('A')
    assert generated_A

    generated_subpkg = mm.eSubpackages[0]
    assert generated_subpkg
    assert generated_subpkg.name == 'subpkg'

    generated_B = generated_subpkg.getEClassifier('B')
    assert generated_B

    a = generated_A()
    b = generated_B()
    a.b = b

    assert b.a is a
Ejemplo n.º 2
0
def test__ecore_task__filtered_elements():
    # prepare test model:
    # -------------------
    # ThePackage
    #   Class1
    #   SubPackage
    #     Class2
    #     MyEnum
    package = EPackage('ThePackage')
    class1 = EClass('Class1')
    class2 = EClass('Class2')
    enum = EEnum('MyEnum', literals=['A', 'B'])

    subpackage = EPackage('SubPackage')
    package.eSubpackages.append(subpackage)

    package.eClassifiers.append(class1)
    subpackage.eClassifiers.extend([class2, enum])

    task = EcoreTask()

    task.element_type = EPackage
    assert set(task.filtered_elements(package)) == {package, subpackage}

    task.element_type = EClass
    assert set(task.filtered_elements(package)) == {class1, class2}

    task.element_type = EEnum
    assert set(task.filtered_elements(package)) == {enum}
Ejemplo n.º 3
0
def package_in_hierarchy():
    pkg1 = EPackage('pkg1')
    pkg2 = EPackage('pkg2')
    pkg3 = EPackage('pkg3')
    pkg1.eSubpackages.append(pkg2)
    pkg2.eSubpackages.append(pkg3)
    return pkg3
Ejemplo n.º 4
0
def test_user_module_derived_collection(pygen_output_dir):
    rootpkg = EPackage('derived_collection')
    c1 = EClass('MyClass')
    c1.eStructuralFeatures.append(EAttribute('any', EString, derived=True, upper=-1))
    rootpkg.eClassifiers.append(c1)
    c2 = EClass('MyOtherClass')
    c2.eStructuralFeatures.append(EReference('other', c1, derived=True, upper=-1))
    c2.eSuperTypes.append(c1)
    rootpkg.eClassifiers.append(c2)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    c = mm.MyOtherClass()
    assert isinstance(c, mm.MyOtherClass)
    assert isinstance(c, mm.MyClass)
    with pytest.raises(AttributeError):
        len(c.any)
    with pytest.raises(AttributeError):
        len(c.other)

    with pytest.raises(AttributeError):
        c.any.append('testValue')

    d = mm.MyClass()
    assert not isinstance(d, mm.MyOtherClass)
    assert isinstance(d, mm.MyClass)

    with pytest.raises(AttributeError):
        c.other.append(d)
Ejemplo n.º 5
0
def test_opposite_derived(pygen_output_dir):
    rootpkg = EPackage('opposite_derived')
    c1 = EClass('A')
    c2 = EClass('B')

    any_feature = EReference('any', c2, derived=True)
    c1.eStructuralFeatures.append(any_feature)
    rootpkg.eClassifiers.append(c1)

    other_feature = EReference('other',
                               c1,
                               derived=True,
                               eOpposite=any_feature)
    c2.eStructuralFeatures.append(other_feature)
    rootpkg.eClassifiers.append(c2)

    assert other_feature.eOpposite is any_feature
    assert any_feature.eOpposite is other_feature

    mm = generate_meta_model(rootpkg, pygen_output_dir)
    a = mm.A()
    b = mm.B()

    with pytest.raises(NotImplementedError):
        a.any = b

    with pytest.raises(NotImplementedError):
        b.other = a
Ejemplo n.º 6
0
def test_user_module_derived_from_mixin_(pygen_output_dir):
    rootpkg = EPackage('derived_from_mixin')
    c1 = EClass('MyClass')
    c1.eOperations.append(EOperation('do_it'))
    c1.eStructuralFeatures.append(EAttribute('any', EString, derived=True))
    rootpkg.eClassifiers.append(c1)
    c2 = EClass('MyOtherClass')
    c2.eStructuralFeatures.append(EAttribute('other', EString, derived=True))
    c2.eStructuralFeatures.append(EReference('toc', c1, derived=True))
    c2.eSuperTypes.append(c1)
    rootpkg.eClassifiers.append(c2)

    mm = generate_meta_model(rootpkg, pygen_output_dir, user_module='user_provided.module')

    c = mm.MyOtherClass(any='any', other='other')
    assert isinstance(c, MyClassMixin)
    assert isinstance(c, MyOtherClassMixin)
    assert isinstance(c, mm.MyClass)
    assert c.any == 'any'
    c.mock_other.assert_called_once_with('other')

    assert not c.do_it.called
    c.do_it()
    assert c.do_it.called

    assert isinstance(mm.MyOtherClass._toc, EReference)
    assert mm.MyOtherClass._toc.name == 'toc'
Ejemplo n.º 7
0
def test_operation(pygen_output_dir):
    rootpkg = EPackage('operation')
    class_ = EClass('MyClass')
    rootpkg.eClassifiers.append(class_)
    class_.eOperations.append(
        EOperation(
            'do_it',
            EInt,
            params=(EParameter('p1', EInt,
                               required=True), EParameter('p2', EInt)),
        ))

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    instance = mm.eClassifiers['MyClass']()

    with pytest.raises(NotImplementedError):
        instance.do_it(1, 2)

    # missing non-required argument
    with pytest.raises(NotImplementedError):
        instance.do_it(1)

    # missing non-required argument
    with pytest.raises(NotImplementedError):
        instance.do_it(p1=1)

    # missing required argument:
    with pytest.raises(TypeError):
        instance.do_it(p2=2)
Ejemplo n.º 8
0
def test_user_module_imported(pygen_output_dir):
    rootpkg = EPackage('user_module')
    c1 = EClass('MyClass')
    rootpkg.eClassifiers.append(c1)

    with pytest.raises(ImportError) as ex:
        mm = generate_meta_model(rootpkg, pygen_output_dir, user_module='some_module')
        assert 'some_module' in ex.message
Ejemplo n.º 9
0
def test_classifier_imports(pygen_output_dir):
    # super types and enums from another package have to be imported in using module:
    rootpkg = EPackage('import_test')
    ppkg = EPackage('provider')
    upkg = EPackage('user')
    rootpkg.eSubpackages.extend([ppkg, upkg])

    super_class = EClass('SuperClass')
    enum = EEnum('AnEnum', literals=('A', 'B'))
    ppkg.eClassifiers.extend((super_class, enum))
    derived_class = EClass('DerivedClass', superclass=super_class)
    derived_class.eStructuralFeatures.append(EAttribute('kind', enum))
    upkg.eClassifiers.append(derived_class)

    # simply importing successully shows the imports have made the types visible
    mm = generate_meta_model(rootpkg, pygen_output_dir)
    assert mm
Ejemplo n.º 10
0
def test_package_with_enum(pygen_output_dir):
    enumpkg = EPackage('enumpkg')
    enum = EEnum('MyEnum', literals=('X', 'Y', 'Z'))
    enumpkg.eClassifiers.append(enum)

    mm = generate_meta_model(enumpkg, pygen_output_dir)

    generated_enum = mm.eClassifiers['MyEnum']
    assert isinstance(generated_enum, EEnum)
    assert set(l.name for l in generated_enum.eLiterals) == {'X', 'Y', 'Z'}
Ejemplo n.º 11
0
def test_auto_registration_enabled(pygen_output_dir):
    rootpkg = EPackage('auto_registration', nsURI='http://autoregister')
    c1 = EClass('MyClass')
    rootpkg.eClassifiers.append(c1)

    mm = generate_meta_model(rootpkg, pygen_output_dir, auto_register_package=True)

    from pyecore.resources import global_registry
    assert mm.nsURI in global_registry
    assert global_registry[mm.nsURI] is mm.auto_registration
Ejemplo n.º 12
0
def test_class_with_documentation(pygen_output_dir):
    rootpkg = EPackage('class_doc')
    class_ = EClass('MyClass')
    rootpkg.eClassifiers.append(class_)

    doc = EAnnotation('http://www.eclipse.org/emf/2002/GenModel')
    class_.eAnnotations.append(doc)
    doc.details['documentation'] = 'This is a documentation test'

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    generated_class = mm.eClassifiers['MyClass']
    assert generated_class.__doc__ == 'This is a documentation test'
Ejemplo n.º 13
0
def test_attribute_with_feature_id(pygen_output_dir):
    rootpkg = EPackage('id_attribute')
    c1 = EClass('MyClass')
    rootpkg.eClassifiers.append(c1)

    a1 = EAttribute('att', EString, iD=True)
    a2 = EAttribute('att2', EString)
    c1.eStructuralFeatures.extend([a1, a2])

    mm = generate_meta_model(rootpkg, pygen_output_dir)
    assert isinstance(mm.MyClass.att, EAttribute)
    assert isinstance(mm.MyClass.att2, EAttribute)
    assert mm.MyClass.att.iD is True
    assert mm.MyClass.att2.iD is False
Ejemplo n.º 14
0
def test_class_with_feature_many(pygen_output_dir):
    rootpkg = EPackage('manyfeatures')
    MyClass = EClass('MyClass')
    rootpkg.eClassifiers.append(MyClass)
    any_feature = EAttribute('any', EString, upper=-1)
    MyClass.eStructuralFeatures.append(any_feature)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    generated_class = mm.eClassifiers['MyClass']
    instance = mm.MyClass()

    assert generated_class is mm.MyClass
    assert isinstance(mm.MyClass.any, EAttribute)
    assert instance.any == set()
Ejemplo n.º 15
0
def test_class_with_derived_features(pygen_output_dir):
    rootpkg = EPackage('simpleClasses')
    MyClass = EClass('MyClass')
    rootpkg.eClassifiers.append(MyClass)
    any_feature = EAttribute('any', EString, derived=True)
    MyClass.eStructuralFeatures.append(any_feature)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    generated_class = mm.eClassifiers['MyClass']

    assert mm.MyClass is generated_class
    assert isinstance(mm.MyClass._any, EAttribute)
    assert mm.MyClass._any.derived is True
    assert mm.MyClass._any.name == 'any'
Ejemplo n.º 16
0
def test_class_with_features(pygen_output_dir):
    rootpkg = EPackage('class_features')
    class_ = EClass('MyClass')
    rootpkg.eClassifiers.append(class_)
    class_.eStructuralFeatures.append(EAttribute('number', EInt, changeable=False))
    class_.eStructuralFeatures.append(EReference('ref', class_))

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    generated_class = mm.eClassifiers['MyClass']
    instance = generated_class(number=7)
    assert instance.number == 7
    assert not instance.ref

    instance.ref = instance
    assert instance.ref is instance
Ejemplo n.º 17
0
def test_eoperation_with_documentation(pygen_output_dir):
    rootpkg = EPackage('eoperation_with_documentation')
    c1 = EClass('MyClass')
    rootpkg.eClassifiers.append(c1)

    operation = EOperation('do_it')
    doc = EAnnotation('http://www.eclipse.org/emf/2002/GenModel')
    operation.eAnnotations.append(doc)
    doc.details['documentation'] = 'This is a documentation test'
    c1.eOperations.append(operation)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    instance = mm.MyClass()
    with pytest.raises(NotImplementedError):
        instance.do_it()
Ejemplo n.º 18
0
def test_empty_package(pygen_output_dir):
    package = EPackage('empty')
    mm = generate_meta_model(package, pygen_output_dir)

    assert mm
    assert mm.name == 'empty'
    assert not mm.nsURI
    assert not mm.nsPrefix
    assert not mm.eClassifiers

    package.name = 'empty2'
    package.nsURI = 'http://xyz.org'
    package.nsPrefix = 'p'
    mm = generate_meta_model(package, pygen_output_dir)
    assert mm.nsURI == 'http://xyz.org'
    assert mm.nsPrefix == 'p'
Ejemplo n.º 19
0
def test_various_datatypes(pygen_output_dir):
    rootpkg = EPackage('datatypes')
    data1 = EDataType('Data1', instanceClassName='java.lang.Integer')
    data2 = EDataType('Data2', instanceClassName='Unknown')
    rootpkg.eClassifiers.extend([data1, data2])

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    gendata1 = mm.eClassifiers['Data1']
    gendata2 = mm.eClassifiers['Data2']

    assert gendata1 is mm.Data1
    assert mm.Data1.eType is int
    assert mm.Data1.default_value is 0
    assert gendata2 is mm.Data2
    assert mm.Data2.eType is object
    assert isinstance(mm.Data2.default_value, object)
Ejemplo n.º 20
0
def test_pythonic_names(pygen_output_dir):
    rootpkg = EPackage('pythonic_names')

    c1 = EClass('MyClass')
    rootpkg.eClassifiers.append(c1)
    a1 = EAttribute('att', EString, upper=-1)
    c1.eStructuralFeatures.append(a1)

    c2 = EClass('pass')
    rootpkg.eClassifiers.append(c2)
    a2 = EAttribute('else', EString, upper=-1)
    c2.eStructuralFeatures.append(a2)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    assert mm.eClassifiers['MyClass'] is mm.MyClass
    assert mm.eClassifiers['pass_'] is mm.pass_
    assert isinstance(mm.pass_.else_, EAttribute)
    assert mm.pass_().else_ == set()
Ejemplo n.º 21
0
def test_eattribute_derived_not_changeable(pygen_output_dir):
    rootpkg = EPackage('changeable_attribute')
    c1 = EClass('MyClass')
    rootpkg.eClassifiers.append(c1)

    att1 = EAttribute('att1', EString, derived=True, changeable=True)
    att2 = EAttribute('att2', EString, derived=True, changeable=False)

    c1.eStructuralFeatures.extend([att2, att1])

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    instance = mm.MyClass()
    assert instance.att1 is None
    assert instance.att2 is None

    instance.att1 = "test_value"
    assert instance.att1 == "test_value"

    with pytest.raises(AttributeError):
        instance.att2 = "test_value2"
Ejemplo n.º 22
0
def test_user_module_default_values(pygen_output_dir):
    rootpkg = EPackage('with_default_values')
    e1 = EEnum('MyEnum', literals=('None_', 'A', 'B'))
    rootpkg.eClassifiers.append(e1)

    c1 = EClass('MyClass')
    a1 = EAttribute('a1', EString)
    a1.defaultValueLiteral = 'my_str'

    a2 = EAttribute('a2', EInt)
    a2.defaultValueLiteral = '7654321'

    a3 = EAttribute('a3', e1)
    a3.defaultValueLiteral = 'None'
    c1.eStructuralFeatures.extend([a1, a2, a3])
    rootpkg.eClassifiers.append(c1)

    mm = generate_meta_model(rootpkg, pygen_output_dir)

    assert mm.MyClass.a1.default_value == 'my_str'
    assert mm.MyClass.a2.default_value == 7654321
    assert mm.MyClass.a3.default_value == mm.MyEnum.None_
Ejemplo n.º 23
0
def makepackage(dom, idx):
    """Top-level constructor for ECore translation."""
    pkg = EPackage(PKGNAME, nsURI=PKGPREFIX, nsPrefix=PKGNAME)
    for clstag in dom.find_all(iseclass):
        makeeclass(clstag, idx, pkg)
    return pkg
Ejemplo n.º 24
0
"""Definition of meta model 'type' for XMLTypes."""
from functools import partial
import pyecore.ecore as Ecore
from pyecore.ecore import EPackage, EDataType, EObject, MetaEClass, \
                          EAttribute, EFeatureMapEntry, EReference

name = 'type'
nsURI = 'http://www.eclipse.org/emf/2003/XMLType'
nsPrefix = 'ecore.xml.type'

eClass = EPackage(name=name, nsURI=nsURI, nsPrefix=nsPrefix)

eClassifiers = {}
getEClassifier = partial(Ecore.getEClassifier, searchspace=eClassifiers)

AnySimpleType = EDataType('AnySimpleType',
                          instanceClassName='java.lang.Object')

AnyURI = EDataType('AnyURI', instanceClassName='java.lang.String')

Base64Binary = EDataType('Base64Binary', instanceClassName='byte[]')

Boolean = EDataType('Boolean',
                    instanceClassName='boolean',
                    to_string=lambda x: str(x).lower(),
                    from_string=lambda x: x in ['True', 'true'])

BooleanObject = EDataType('BooleanObject',
                          instanceClassName='java.lang.Boolean',
                          to_string=lambda x: str(x).lower(),
                          from_string=lambda x: x in ['True', 'true'])