#!/usr/bin/env python
import coral
import random

DIFF = coral.lang.DIFF10

L = coral.lang.SMD01
UML14 = coral.lang.UML14

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

m1 = L.Language()
m1.name = "one"

m2 = coral.ModelCopy_One(m1)
m2.__setXMIid__(m1.__XMIid__())

m2.name = "two"

md = coral.ModelDifference_One(m1, m2)

o = coral.Set()
o.add(md)
print coral.saveDocument_str(o, "1.2")

ec = md.elementChanges
assert ec.size() == 1

pc = ec[0].propertyChanges
assert pc.size() == 1
assert pc[0].propertyName == "name"

op = pc[0].operations
assert op.size() == 1
assert op[0].oldValue == "one"
assert op[0].newValue == "two"


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

m2.name = "one"

md = coral.ModelDifference_One(m1, m2)

o = coral.Set()
o.add(md)
print coral.saveDocument_str(o, "1.2")

ec = md.elementChanges
assert ec.size() == 0


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

m2.version = 1
m2.version = 0

md = coral.ModelDifference_One(m1, m2)

o = coral.Set()
o.add(md)
print coral.saveDocument_str(o, "1.2")

ec = md.elementChanges
assert ec.size() == 0


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

m1 = UML14.Model()
m2 = UML14.Model()
m2.__setXMIid__(m1.__XMIid__())
c = UML14.Class()
m2.ownedElement.add(c)

md = coral.ModelDifference_One(m1, m2)

o = coral.Set()
o.add(md)
print coral.saveDocument_str(o, "1.2")

ec = md.elementChanges
mc = md.modelChanges
assert mc.size() == 1
assert mc[0].type.name == "Class"
assert mc[0].element == c.__XMIid__()
assert ec.size() == 2
# BUG this could be fleshed out a bit more


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

m1 = UML14.Method()
for i in range(10):
    p = UML14.Parameter()
    m1.parameter.insert(p)
m2 = coral.ModelCopy_One(m1, coral.MODELCOPY_DONT_CREATE_NEW_UUIDS)

n_removed = 0
for p in m2.parameter:
    if random.randrange(2) == 0:
        m2.parameter.remove(p)
        n_removed += 1

md = coral.ModelDifference_One(m1, m2)

o = coral.Set()
o.add(md)
print coral.saveDocument_str(o, "1.2")

ec = md.elementChanges
mc = md.modelChanges
assert mc.size() == n_removed * 2
for i in mc:
    assert i.type.name == "Parameter" or i.type.name == "Expression"


