blob: 2b450303e9d466c9e1390a4e8c8fa035eb09dbcd [file] [log] [blame]
[comment]
Copyright (c) 2008, 2012 Obeo
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 1.0
Any license can be applied to the files generated with this template
and Acceleo.
author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
[/comment]
[module factory('http://www.eclipse.org/emf/2002/Ecore')/]
[import names/]
[template public packageToPythonFactoryFile(packaje : EPackage)]
[file (packaje.moduleFactory().trim(), false)]
[for (c : EClassifier | packaje.eClassifiers) ? (c.oclIsKindOf(EClass) and not c.oclAsType(EClass).abstract)]
from [packaje.moduleName()/] import [c.name/]
[/for]
[packaje.packageToPythonFactoryClass()/]
[packaje.packageToPythonFactorytests()/]
[/file]
[/template]
[template public packageToPythonFactoryClass(packaje : EPackage)]
class [packaje.factoryClassName()/]:
""" Factory to create elements from [packaje.name/]
"""
def create(self,name):
""" generic method to create elements.
This method is able to create:
[for (c : EClassifier | packaje.eClassifiers) ? (c.oclIsKindOf(EClass) and not c.oclAsType(EClass).abstract)]
[c.name/]
[/for]
"""
f = getattr(self,"create%s" % name)
return f()
[for (c : EClassifier | packaje.eClassifiers) ? (c.oclIsKindOf(EClass) and not c.oclAsType(EClass).abstract)]
def create[c.name/](self):
"""
Create an instance of [c.name/].
"""
instance = [c.name/]()
# type here special initialization code
return instance
[/for]
[/template]
[template public packageToPythonFactorytests(packaje : EPackage)]
import unittest
class Test_[packaje.moduleName()/]_factory(unittest.TestCase):
[for (c : EClassifier | packaje.eClassifiers) ? (c.oclIsKindOf(EClass) and not c.oclAsType(EClass).abstract)]
def test[c.name/]Creation(self):
"""
Test the direct instanciation of a [c.name/].
"""
d = [c.name/]()
self.assertTrue(d!=None)
def test[c.name/]CreationFactory(self):
"""
Test the instanciation of a [c.name/] through the factory.
"""
factory = [packaje.factoryClassName()/]()
d = factory.create("[c.name/]")
self.assertTrue(d != None)
self.assertTrue(isinstance(d, [c.name/]))
[/for]
if __name__ == '__main__':
unittest.main()
[/template]