#!/usr/bin/env python

import coral
import coral.modeler
import coral.modeler.modeler
import coral.addins.constraint.script.evaluateconstraints as S

L = coral.lang.CONSTRAINT10
try:
    S.main(["-i", "testScript.xml", "-t", "testTarget.xml", "-q", "-o", "tmp/testScriptOutput.xml"])
except SystemExit,e:
    assert str(e) == "2"
prj = coral.loadDocument("tmp/testScriptOutput.xml")
for part in prj.contents():
    if part.oclIsKindOf(L.Package):
        for CM in part.__getAllParts__().select(lambda p: p.oclIsKindOf(L.ConstraintModel)):
            if CM.name == "testModelCM":
                assert len(CM.result) == 10
            if CM.name == "testModel2CM":
                assert len(CM.result) == 2,str(len(CM.result))


prj.invalidate()
try:
    S.main(["-i", "testScript.xml", "-t", "testTarget.xml", "-q", "-o", "tmp/testScriptOutput.xml", "testPackage"])
except SystemExit,e:
    assert str(e) == "2"
prj = coral.loadDocument("tmp/testScriptOutput.xml")
for part in prj.contents():
    if part.oclIsKindOf(L.Package):
        for CM in part.__getAllParts__().select(lambda p: p.oclIsKindOf(L.ConstraintModel)):
            if CM.name == "testModelCM":
                assert len(CM.result) == 2
            if CM.name == "testModel2CM":
                assert len(CM.result) == 1

prj.invalidate()
try:
    S.main(["-i", "testScript.xml", "-t", "testTarget.xml", "-q", "-o", "tmp/testScriptOutput.xml", "testPackage2"])
except SystemExit,e:
    assert str(e) == "2"
prj = coral.loadDocument("tmp/testScriptOutput.xml")
for part in prj.contents():
    if part.oclIsKindOf(L.Package):
        for CM in part.__getAllParts__().select(lambda p: p.oclIsKindOf(L.ConstraintModel)):
            if CM.name == "testModelCM":
                assert len(CM.result) == 8
            if CM.name == "testModel2CM":
                assert len(CM.result) == 1

prj.invalidate()
try:
    S.main(["-i", "testScript.xml", "-t", "testTarget.xml", "-q", "-o", "tmp/testScriptOutput.xml", "testPackage", "testPackage2"])
except SystemExit,e:
    assert str(e) == "2"    
prj = coral.loadDocument("tmp/testScriptOutput.xml")
for part in prj.contents():
    if part.oclIsKindOf(L.Package):
        for CM in part.__getAllParts__().select(lambda p: p.oclIsKindOf(L.ConstraintModel)):
            if CM.name == "testModelCM":
                assert len(CM.result) == 10
            if CM.name == "testModel2CM":
                assert len(CM.result) == 2

prj.invalidate()
try:
    S.main(["-i", "testPath/testScript.xml", "-t", "testPath/testTarget.xml", "-q", "-o", "tmp/testPathScriptOutput.xml"])
except SystemExit,e:
    assert str(e) == "2"    
prj = coral.loadDocument("tmp/testScriptOutput.xml")
for part in prj.contents():
    if part.oclIsKindOf(L.Package):
        for CM in part.__getAllParts__().select(lambda p: p.oclIsKindOf(L.ConstraintModel)):
            if CM.name == "testModelCM":
                assert len(CM.result) == 10
            if CM.name == "testModel2CM":
                assert len(CM.result) == 2

#import sys
#sys.exit(0)
