# Path: pyecore/utils.py
# class DynamicEPackage(EObserver):
#     """A DynamicEPackage gives the ability to directly handle metaclasses
#     from a metamodel as if it were a statically generated EPackage.
# 
#     Usage from an existing dynamic EPackage named 'myroot' that defines two
#     EClass: 'A' and 'B'
#     >>> from pyecore.utils import DynamicEPackage
#     >>> MyAPI = DynamicEPackage(myroot)
#     >>> MyAPI.A
#     <EClass name="A">
#     >>> a = MyAPI.A()
#     >>> a
#     <pyecore.ecore.A object at 0x7f118de363c8>
#     """
# 
#     def __init__(self, package):
#         if not isinstance(package, EPackage):
#             raise BadValueError(got=package, expected=EPackage)
#         if ismodule(package):
#             package = package.eClass
#         super().__init__(notifier=package)
# 
#         for eclass in package.eClassifiers:
#             setattr(self, eclass.name, eclass)
#         for subpackage in package.eSubpackages:
#             setattr(self, subpackage.name, DynamicEPackage(subpackage))
# 
#     def notifyChanged(self, notification):
#         kind = notification.kind
#         if notification.feature is EPackage.eClassifiers:
#             if kind == Kind.ADD:
#                 new = notification.new
#                 setattr(self, new.name, new)
#             elif kind == Kind.ADD_MANY:
#                 for new in notification.new:
#                     setattr(self, new.name, new)
#             elif kind == Kind.REMOVE and notification.old.eResource is None:
#                 delattr(self, notification.old.name)
#             # elif kind == Kind.REMOVE_MANY:
#             #     for element in notification.old:
#             #         if element.eResource is None:
#             #             delattr(self, element.name)
# 
# def alias(name, feature, eclass=None):
#     if eclass is None:
#         eclass = feature.eContainingClass
#     if isinstance(eclass, EClass):
#         setattr(eclass.python_class, name, feature)
#     else:
#         feature.name = name
#         setattr(eclass, name, feature)


import pytest
from pyecore.ecore import *
from pyecore.utils import DynamicEPackage, alias
    pack.eClassifiers.extend([Root, A, B])
    return pack


@pytest.fixture(scope='module')
def complexmm():
    A = EClass('A')
    B = EClass('B')
    Root = EClass('Root')
    pack = EPackage('pack', nsURI='http://pack/1.0', nsPrefix='pack')
    pack.eClassifiers.extend([Root, A, B])

    innerpackage = EPackage('inner', nsURI='http://inner', nsPrefix='inner')
    C = EClass('C')
    D = EClass('D')
    innerpackage.eClassifiers.extend([C, D])
    pack.eSubpackages.append(innerpackage)
    return pack


def test_dynamic_access_eclasses(simplemm):
    SimpleMM = DynamicEPackage(simplemm)
    assert SimpleMM.A
    assert SimpleMM.B


def test_only_epackages(simplemm):
    SimpleMM = DynamicEPackage(simplemm)
    assert SimpleMM.A
    with pytest.raises(BadValueError):

# Path: pyecore/utils.py
# class DynamicEPackage(EObserver):
#     """A DynamicEPackage gives the ability to directly handle metaclasses
#     from a metamodel as if it were a statically generated EPackage.
# 
#     Usage from an existing dynamic EPackage named 'myroot' that defines two
#     EClass: 'A' and 'B'
#     >>> from pyecore.utils import DynamicEPackage
#     >>> MyAPI = DynamicEPackage(myroot)
#     >>> MyAPI.A
#     <EClass name="A">
#     >>> a = MyAPI.A()
#     >>> a
#     <pyecore.ecore.A object at 0x7f118de363c8>
#     """
# 
#     def __init__(self, package):
#         if not isinstance(package, EPackage):
#             raise BadValueError(got=package, expected=EPackage)
#         if ismodule(package):
#             package = package.eClass
#         super().__init__(notifier=package)
# 
#         for eclass in package.eClassifiers:
#             setattr(self, eclass.name, eclass)
#         for subpackage in package.eSubpackages:
#             setattr(self, subpackage.name, DynamicEPackage(subpackage))
# 
#     def notifyChanged(self, notification):
#         kind = notification.kind
#         if notification.feature is EPackage.eClassifiers:
#             if kind == Kind.ADD:
#                 new = notification.new
#                 setattr(self, new.name, new)
#             elif kind == Kind.ADD_MANY:
#                 for new in notification.new:
#                     setattr(self, new.name, new)
#             elif kind == Kind.REMOVE and notification.old.eResource is None:
#                 delattr(self, notification.old.name)
#             # elif kind == Kind.REMOVE_MANY:
#             #     for element in notification.old:
#             #         if element.eResource is None:
#             #             delattr(self, element.name)
# 
# def alias(name, feature, eclass=None):
#     if eclass is None:
#         eclass = feature.eContainingClass
#     if isinstance(eclass, EClass):
#         setattr(eclass.python_class, name, feature)
#     else:
#         feature.name = name
#         setattr(eclass, name, feature)


# Path: tests/test_utils.py
import pytest
from pyecore.ecore import *
from pyecore.utils import DynamicEPackage, alias

    pack.eClassifiers.extend([Root, A, B])
    return pack


@pytest.fixture(scope='module')
def complexmm():
    A = EClass('A')
    B = EClass('B')
    Root = EClass('Root')
    pack = EPackage('pack', nsURI='http://pack/1.0', nsPrefix='pack')
    pack.eClassifiers.extend([Root, A, B])

    innerpackage = EPackage('inner', nsURI='http://inner', nsPrefix='inner')
    C = EClass('C')
    D = EClass('D')
    innerpackage.eClassifiers.extend([C, D])
    pack.eSubpackages.append(innerpackage)
    return pack


def test_dynamic_access_eclasses(simplemm):
    SimpleMM = DynamicEPackage(simplemm)
    assert SimpleMM.A
    assert SimpleMM.B


def test_only_epackages(simplemm):
    SimpleMM = DynamicEPackage(simplemm)
    assert SimpleMM.A
    with pytest.raises(BadValueError):

        DynamicEPackage(SimpleMM.A)