#!/usr/bin/env python
import sys
import unittest


#from umlcoresupport import *

import copy
import types
import coral
import coral ; theMetamodel=coral.lang.UML14 

class testMetaMM(unittest.TestCase):

## C++ Sets don't accept MetamodelElements, only ModelElements
##    def testMElements(self):
##        # This test collections
##        me1=[]
##        for c in vars(theMetamodel).values():
##            if type(c)==types.ClassType and isinstance(c, coral.MetamodelElement) and c.isSubclass(coral.MMClass):
##                me1.append(c)

##        me2=coral.Set(vars(theMetamodel).values()).select(
##            lambda c: type(c)==types.ClassType and c.isSubclass(coral.MMClass))

##        assert(me2.size()>0)
##        assert(me2.nonEmpty())
        
##        for e in me1:
##            assert(e in me2)

##        for e in me2:
##            assert(e in me1)

##        assert(me2.includesAll(me1))
##        assert(theMetamodel.Set(me1).includesAll(me2))
##        # each model element has a proper name
##        assert(me2.forAll(lambda e: len(e.getName())>0))
        
               
    def testReset(self):
        import coral ; metamodel=coral.lang.UML14
        model=metamodel.Model()
        m1=metamodel.Class()
        m2=metamodel.Class()
        g=metamodel.Generalization()
        model.ownedElement.append(m1)
        model.ownedElement.append(m2)
        model.ownedElement.append(g)
        g.child=m1
        g.parent=m2
        assert(g in m1.generalization)
        assert(g in m2.specialization)

        g.__resetAttrs__()

        assert(g.child is None)
        assert(g.parent is None)
        assert(not (g in m1.generalization))
        assert(not (g in m2.specialization))
        assert(not (g in model.ownedElement))

    def testGetMetamodelByName(self):
        m1=coral.Language.__getLanguage__("UML", 1, 3)
        assert m1
        print m1
        #assert(0) # should the below stuff be accepted? No, imho. --msa
        #m2=coral.Language.__getLanguage__("UML 1.3")
        #m3=coral.Language.__getLanguage__("smw.UML13")
        #assert(m1==m2 and m2==m3)
        m4=coral.Language.__getLanguage__("UML", 1, 4)
        assert m4
        assert(m1!=m4)
        print m4

    def testDeepCopy(self):
        import coral ; metamodel=coral.lang.UML14
        model=metamodel.Model(name="test")
        m1=metamodel.Class(name="name1")
        m2=metamodel.Class(name="name2")
        g=metamodel.Generalization(name="name3")
        model.ownedElement.append(m1)
        model.ownedElement.append(m2)
        model.ownedElement.append(g)
        g.child=m1
        g.parent=m2

        x=coral.ModelCopy_One(model)
        x.name="test2"
        assert(x.name!=model.name)
        assert(x.ownedElement!=model.ownedElement)
        for o in x.ownedElement:
            print o
            assert(o not in model.ownedElement)
            assert(model.ownedElement.exists(lambda e: e.name==o.name))

        x=coral.ModelCopy_One(model)
        x.name="test2"
        assert(x.name!=model.name)
        assert(x.ownedElement!=model.ownedElement)
        for o in x.ownedElement:
            print o
            assert(o not in model.ownedElement)
            assert(model.ownedElement.exists(lambda e: e.name==o.name))
        
if __name__ == '__main__':
    unittest.main()
