def testDumpTemplateClassHeader(self): intDecl = writer_code.intType stringDecl = writer_code.stringType privateVar = writer_code.Attribute('pippo', intDecl, 'pri') emptyBody = writer_code.Code('') publicConstr = writer_code.Constructor(emptyBody, 'pu', [], ['std::string()']) classDecl = writer_code.ClassDeclaration('MyClass', [privateVar], [stringDecl], ['T']) classDecl.addConstructor(publicConstr) dumper = writer_code.FileDumper('prova.cpp', True) dumper.addMember(classDecl) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 12 + 20) self.assertEqual( lines[18], 'template < typename T > class MyClass : public std::string{\n') self.assertEqual(lines[19], ' private:\n') self.assertEqual(lines[20], ' int pippo;\n') self.assertEqual(lines[21], '\n') self.assertEqual(lines[22], ' public:\n') self.assertEqual(lines[23], ' MyClass() : std::string(){\n') self.assertEqual(lines[24], '\n') self.assertEqual(lines[25], ' }\n') self.assertEqual(lines[26], '\n') self.assertEqual(lines[27], '};\n')
def testTemplateFunctionsHeader(self): tempType = writer_code.TemplateType( 'std::map', [writer_code.intType, writer_code.stringType], 'map') tempVar = writer_code.Function( 'pippo', writer_code.Code( 'std::map<int, std::string> myMap;\nmyMap[5] = \"ccc\";\nreturn myMap;' ), tempType, [], False, False, ['T']) dumper = writer_code.FileDumper('prova.cpp', True) dumper.addMember(tempVar) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 8 + 19) self.assertEqual(lines[16], '#include <map>\n') self.assertEqual(lines[17], '#include <string>\n') self.assertEqual( lines[19], 'template < typename T > std::map< int, std::string > pippo(){\n') self.assertEqual(lines[20], ' std::map<int, std::string> myMap;\n') self.assertEqual(lines[21], ' myMap[5] = \"ccc\";\n') self.assertEqual(lines[22], ' return myMap;\n') self.assertEqual(lines[23], '}\n')
def testSimpleFunction(self): code = writer_code.Code('printf(\"Wow\");') function = writer_code.Function('dummyFun', code) function.writeImplementation(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 3) self.assertEqual(lines[0], 'void dummyFun(){\n') self.assertEqual(lines[1], ' printf(\"Wow\");\n') self.assertEqual(lines[2], '}\n')
def testFunctionDoc(self): intType = writer_code.intType code = writer_code.Code('') parameters = [writer_code.Parameter('param1', intType)] function = writer_code.Function('dummyFun', code, intType, parameters) function.addDoc('Documentation test\nanother line\n') function.writeImplementation(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 5) self.assertEqual(lines[0], '/// Documentation test\n') self.assertEqual(lines[1], '/// another line\n') self.assertEqual(lines[2], 'int dummyFun( int param1 ){\n') self.assertEqual(lines[3], '\n') self.assertEqual(lines[4], '}\n')
def testDumpTemplateClassImpl(self): intDecl = writer_code.intType stringDecl = writer_code.stringType privateVar = writer_code.Attribute('pippo', intDecl, 'pri') emptyBody = writer_code.Code('') publicConstr = writer_code.Constructor(emptyBody, 'pu', [], ['std::string()']) classDecl = writer_code.ClassDeclaration('MyClass', [privateVar], [stringDecl], ['T']) classDecl.addConstructor(publicConstr) dumper = writer_code.FileDumper('prova.cpp', False) dumper.addMember(classDecl) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 1 + 16)
def testDumpClassImpl(self): intDecl = writer_code.intType privateVar = writer_code.Attribute('pippo', intDecl, 'pri') emptyBody = writer_code.Code('') publicConstr = writer_code.Constructor(emptyBody, 'pu') classDecl = writer_code.ClassDeclaration('MyClass', [privateVar]) classDecl.addConstructor(publicConstr) dumper = writer_code.FileDumper('prova.cpp', False) dumper.addMember(classDecl) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 4 + 15) self.assertEqual(lines[14], 'MyClass::MyClass(){\n') self.assertEqual(lines[15], '\n') self.assertEqual(lines[16], '}\n')
def testTemplateFunctionsImpl(self): tempType = writer_code.TemplateType( 'std::map', [writer_code.intType, writer_code.stringType], 'map') tempVar = writer_code.Function( 'pippo', writer_code.Code( 'std::map<int, std::string> myMap;\nmyMap[5] = \"ccc\";\nreturn myMap;' ), tempType, [], False, ['T']) dumper = writer_code.FileDumper('prova.cpp', False) dumper.addMember(tempVar) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 3 + 15) self.assertEqual(lines[14], '#include <map>\n') self.assertEqual(lines[15], '#include <string>\n')
def testReturnFunction(self): code = writer_code.Code('if(works){\nprintf(\"hummm\\n\");\nreturn 1;\n}\nelse{\nreturn 0;\n}') retType = writer_code.intType function = writer_code.Function('dummyFun', code, retType) function.writeImplementation(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 9) self.assertEqual(lines[0], 'int dummyFun(){\n') self.assertEqual(lines[1], ' if(works){\n') self.assertEqual(lines[2], ' printf(\"hummm\\n\");\n') self.assertEqual(lines[3], ' return 1;\n') self.assertEqual(lines[4], ' }\n') self.assertEqual(lines[5], ' else{\n') self.assertEqual(lines[6], ' return 0;\n') self.assertEqual(lines[7], ' }\n') self.assertEqual(lines[8], '}\n')
def testInlineFunction(self): code = writer_code.Code('if(works){\nprintf(\"hummm\\n\");\nreturn 1;\n}\nelse{\nreturn 0;\n}') intType = writer_code.intType parameters = [writer_code.Parameter('param1', intType)] function = writer_code.Function('dummyFun', code, intType, parameters, inline = True) function.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 9) self.assertEqual(lines[0], 'inline int dummyFun( int param1 ){\n') self.assertEqual(lines[1], ' if(works){\n') self.assertEqual(lines[2], ' printf(\"hummm\\n\");\n') self.assertEqual(lines[3], ' return 1;\n') self.assertEqual(lines[4], ' }\n') self.assertEqual(lines[5], ' else{\n') self.assertEqual(lines[6], ' return 0;\n') self.assertEqual(lines[7], ' }\n') self.assertEqual(lines[8], '}\n')
def testDumpClassHeader(self): intDecl = writer_code.intType privateVar = writer_code.Attribute('pippo', intDecl, 'pri') emptyBody = writer_code.Code('') publicConstr = writer_code.Constructor(emptyBody, 'pu') classDecl = writer_code.ClassDeclaration('MyClass', [privateVar]) classDecl.addConstructor(publicConstr) dumper = writer_code.FileDumper('prova.cpp', True) dumper.addMember(classDecl) dumper.write() testFile = open('prova.cpp', 'rt') lines = testFile.readlines() testFile.close() os.remove('prova.cpp') self.assertEqual(len(lines), 8 + 19) self.assertEqual(lines[16], 'class MyClass{\n') self.assertEqual(lines[17], ' private:\n') self.assertEqual(lines[18], ' int pippo;\n') self.assertEqual(lines[19], '\n') self.assertEqual(lines[20], ' public:\n') self.assertEqual(lines[21], ' MyClass();\n') self.assertEqual(lines[22], '};\n')
def testDumpAll(self): folder = writer_code.Folder('temp') intDecl = writer_code.intType privateVar = writer_code.Attribute('pippo', intDecl, 'pri') emptyBody = writer_code.Code('') publicConstr = writer_code.Constructor(emptyBody, 'pu') classDecl = writer_code.ClassDeclaration('MyClass', [privateVar]) classDecl.addConstructor(publicConstr) implFile = writer_code.FileDumper('prova.cpp', False) implFile.addMember(classDecl) headFile = writer_code.FileDumper('prova.hpp', True) headFile.addMember(classDecl) folder.addHeader(headFile) folder.addCode(implFile) folder.create() testImplFile = open('temp/prova.cpp', 'rt') lines = testImplFile.readlines() testImplFile.close() os.remove('temp/prova.cpp') self.assertEqual(len(lines), 19) self.assertEqual(lines[14], 'MyClass::MyClass(){\n') self.assertEqual(lines[15], '\n') self.assertEqual(lines[16], '}\n') testHeadFile = open('temp/prova.hpp', 'rt') lines = testHeadFile.readlines() testHeadFile.close() os.remove('temp/prova.hpp') self.assertEqual(len(lines), 27) self.assertEqual(lines[16], 'class MyClass{\n') self.assertEqual(lines[17], ' private:\n') self.assertEqual(lines[18], ' int pippo;\n') self.assertEqual(lines[19], '\n') self.assertEqual(lines[20], ' public:\n') self.assertEqual(lines[21], ' MyClass();\n') self.assertEqual(lines[22], '};\n') testWscriptFile = open('temp/wscript', 'rt') lines = testWscriptFile.readlines() testWscriptFile.close() os.remove('temp/wscript') self.assertEqual(len(lines), 16) self.assertEqual(lines[0], '#!/usr/bin/env python\n') self.assertEqual(lines[1], '# -*- coding: iso-8859-1 -*-\n') self.assertEqual(lines[2], '\n') self.assertEqual(lines[3], 'import os\n') self.assertEqual(lines[4], '\n') self.assertEqual(lines[5], '\n') self.assertEqual(lines[6], 'def build(bld):\n') self.assertEqual(lines[7], ' sources = \"\"\"\n') self.assertEqual(lines[8], ' prova.cpp\n') self.assertEqual(lines[9], ' \"\"\"\n') self.assertEqual( lines[10], ' uselib = \'BOOST BOOST_THREAD BOOST_FILESYSTEM BOOST_SYSTEM SYSTEMC TLM TRAP\'\n' ) self.assertEqual(lines[11], ' objects = \'\'\n') self.assertEqual(lines[12], ' includes = \'.\'\n') self.assertEqual(lines[13], ' target = \'temp\'\n') self.assertEqual( lines[15], ' bld.program(source = sources, target = target, use = uselib + \' \' + objects, includes = includes)\n' ) import shutil shutil.rmtree('temp', True)