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 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')