#!/usr/bin/env python

import coral

ECORE = coral.lang.ecore10

import os
os.chdir("../../../../qa/models2import")

#######################################################################
p1 = coral.loadDocument("SimpleECoreModel.xmi")

pkg = p1.contents()[0]

assert pkg
c1 = pkg.eClassifiers[0]
c2 = pkg.eClassifiers[1]

if c1.name == "foobar":
    pass
else:
    c1,c2 = c2,c1

assert len(c1.eOperations) == 1
op = c1.eOperations[0]
assert op.name == "myslotdefinition"
assert op.lowerBound == 5
assert op.upperBound == -1

assert len(c1.eStructuralFeatures) == 2
sf = c1.eStructuralFeatures[0]

#######################################################################

p2 = coral.loadDocument("simple_xpath.ecore")

pkg = p2.contents()[0]

umlclass = pkg.eClassifiers.select(lambda x: x.name == "UMLClass")[0]

classifier = pkg.eClassifiers.select(lambda x: x.name == "Classifier")[0]

attribute = pkg.eClassifiers.select(lambda x: x.name == "Attribute")[0]

owner = attribute.eReferences.select(lambda x: x.name == "owner")[0]

assert len(umlclass.eSuperTypes) == 1
assert umlclass.eSuperTypes[0] == classifier
assert owner
assert classifier
assert umlclass
assert attribute

_attr = umlclass.eReferences.select(lambda x: x.name == "attribute")[0]

assert _attr

assert _attr.eOpposite == owner
assert owner.eOpposite == _attr

#######################################################################

p3 = coral.loadDocument("complicated_xpath.ecore")

r = p3.contents()[0]
modelelement = r.eClassifiers.select(lambda x: x.name == "ModelElement")[0]
assert modelelement

ecore_model = coral.loadDocument("coral/lang/ecore10/def/ecore_model.xmi")

attr = modelelement.eAttributes[0]
assert attr.name == "name"

assert attr.eType.__getRootsProject__() == ecore_model

#######################################################################

p4 = coral.loadDocument("three_eclipse_models/useNoNs.ecore")

UseNoNs_ref = p4.contents()[0].eClassifiers[0].eStructuralFeatures[0]
UseNoNs_ref_type = UseNoNs_ref.eType

p5 = coral.loadDocument("three_eclipse_models/noNs.ecore")

assert UseNoNs_ref_type.__getRootsProject__() == p5
assert UseNoNs_ref_type.name == "ProviderLocation"

nons_ref = UseNoNs_ref_type.eStructuralFeatures[0]
assert nons_ref.name == "query"

p6 = coral.loadDocument("three_eclipse_models/query.ecore")

assert nons_ref.eType.__getRootsProject__() == p6

assert nons_ref.eType.name == "Query"

######################################################################
