00001 import unittest
00002 import win32com.client
00003 import os
00004 from GPyUnit.util import DispatchEx
00005
00006 def findInProj( project, obj_name = "", obj_kind = ""):
00007 """ Returns an object in project, satisfying the obj_name and obj_kind criteria, if speficied
00008 """
00009
00010
00011 filter = project.CreateFilter()
00012
00013
00014 filter.Name = obj_name
00015 filter.Kind = obj_kind
00016
00017 try:
00018 some_fcos = project.AllFCOs( filter )
00019 if some_fcos.Count > 0:
00020 return some_fcos.Item( 1 )
00021 else:
00022 print "findInProj >> Object not found : name = '" + obj_name + "' kind = '" + obj_kind + "'"
00023 assert 0
00024 except:
00025 print "findInProj >> Exception : name = '" + obj_name + "' kind = '" + obj_kind + "'"
00026 raise
00027
00028 pass
00029
00030 def folder( project, kind):
00031 """ Returns an IMgaMetaFolder pointer, based on the folder kind given """
00032 metaproj = project.RootMeta
00033 metaroot = metaproj.RootFolder
00034
00035
00036
00037 mfol = metaroot.LegalChildFolderByName(kind)
00038 return mfol
00039
00040 def kind(project, kind):
00041 """ Returns an IMgaMetaFCO pointer, based on the kind given """
00042 metaproj = project.RootMeta
00043 metaroot = metaproj.RootFolder
00044
00045
00046
00047 mfco = metaroot.DefinedFCOByName(kind, 1)
00048 return mfco
00049
00050 def role(project, model, role_str):
00051 """ Returns an IMgaMetaRole pointer, based on role_str and the container model (IMgaFCO) given """
00052 try:
00053 metaproj = project.RootMeta
00054 metaroot = metaproj.RootFolder
00055
00056 metacont = kind(project, model.MetaBase.Name)
00057 metarole = metacont.RoleByName(role_str)
00058
00059 except:
00060 print 'No such kind:', role_str, 'in', model
00061 raise
00062 return metarole
00063
00064
00065 def new(p, cont, role_str):
00066 """ Creates an fco, in role_str role in container model specified """
00067 return cont.CreateChildObject( role(p, cont, role_str))
00068
00069 def subtype(p, cont, base):
00070 """ Creates a derived fco from base, in the same role as base in container model specified """
00071 return cont.DeriveChildObject( base, base.MetaRole, False)
00072
00073 def instantiate(p, cont, type):
00074 """ Instantiates an fco from type, in the same role as type in container model specified """
00075 return cont.DeriveChildObject( type, type.MetaRole, True)
00076
00077 def newObjInFold(p, fold, kind_str):
00078 """ Creates an object in a folder/rootfolder, based on kind_str"""
00079 return fold.CreateRootObject( kind( p, kind_str))
00080
00081 def subtypeInFold(p, cont, base):
00082 """ Creates a derived fco from base, in the folder specified """
00083 return cont.DeriveRootObject( base, False)
00084
00085 def instantiateInFold(p, cont, type):
00086 """ Instantiates an fco from type, in the folder specified """
00087 return cont.DeriveRootObject( type, True)
00088
00089 def newFolder(p, parent, folder_kind_str):
00090 """ Creates a folder_kind_str folder in parent"""
00091 return parent.CreateFolder( folder( p, folder_kind_str))
00092
00093
00094 def connect(p, cont, s, d, role_str):
00095 """ Helper method connecting plain fcos/ports, when no references are involved
00096 """
00097 z0 = DispatchEx("Mga.MgaFCOs")
00098 return cont.CreateSimpleConn( role(p, cont, role_str), s, d, z0, z0)
00099
00100 def connectRefP(p, cont, s, d, r1, r2, role_str):
00101 """ Helper method connecting ports.
00102 s: source fco/port
00103 d: destination fco/port
00104 r1: modelreference, 'containing' s
00105 r2: modelreference, 'containing' d
00106 r1 or r2 might be 0, when that end of the connection is an fco or modelport (no reference involved)
00107 """
00108 z1 = DispatchEx("Mga.MgaFCOs")
00109 z2 = DispatchEx("Mga.MgaFCOs")
00110 if r1:
00111 z1.Append( r1)
00112 if r2:
00113 z2.Append( r2)
00114 return cont.CreateSimpleConn( role(p, cont, role_str), s, d, z1, z2)
00115
00116
00117 def creaP(mganame, parad):
00118 from GPyUnit import util
00119 util.register_xmp(parad)
00120 project = DispatchEx("Mga.MgaProject")
00121
00122
00123
00124
00125
00126 project.Create( "MGA=" + mganame, parad)
00127 project.BeginTransactionInNewTerr(0)
00128 return project
00129
00130 def saveP(project):
00131 try:
00132 project.CommitTransaction()
00133 except:
00134 project.AbortTransaction()
00135 project.Save()
00136 project.Close(0)
00137
00138 def populate(p):
00139 """Sample: The way the tester could build up a model needed for a test
00140 """
00141 folder1 = newFolder( p, p.RootFolder, 'SheetFolder')
00142 folder1.Name = 'SheetFolder1'
00143
00144 parsh1 = newObjInFold( p, folder1, 'ParadigmSheet')
00145 parsh1.Name = 'ParadigmSheet1'
00146
00147 at1 = new( p, parsh1, 'Atom')
00148 at1.Name = 'Atom1'
00149
00150 at2 = new( p, parsh1, 'Atom')
00151 at2.Name = 'Atom2'
00152
00153 ap1 = new( p, parsh1, 'AtomProxy')
00154 ap1.Name = 'AtProx1'
00155
00156 ap2 = new( p, parsh1, 'AtomProxy')
00157 ap2.Name = 'AtProx2'
00158
00159 ap1.Referred = at1
00160 ap2.Referred = at2