def testNestedTemplateType(self): innerType1 = writer_code.stringType innerType2 = writer_code.intType innerType3 = writer_code.doubleType templ1 = writer_code.TemplateType('std::map', [innerType2, innerType3]) templ2 = writer_code.TemplateType('std::map', [innerType1, templ1]) templ2.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 1) self.assertEqual(lines[0], 'std::map< std::string, std::map< int, double > >')
def testTemplatedVariable(self): innerType1 = writer_code.stringType innerType2 = writer_code.intType innerType3 = writer_code.doubleType templ1 = writer_code.TemplateType('std::map', [innerType2, innerType3]) type = writer_code.TemplateType('std::map', [innerType1, templ1]) var = writer_code.Variable('pippo', type) var.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 0)
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 testSimpleTemplateType(self): innerType = writer_code.stringType templ = writer_code.TemplateType('std::vector', [innerType]) templ.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 1) self.assertEqual(lines[0], 'std::vector< std::string >')
def testDoubleTemplateType(self): innerType1 = writer_code.stringType innerType2 = writer_code.intType templ = writer_code.TemplateType('std::map', [innerType1, innerType2]) templ.writeDeclaration(self.writer) self.writer.flush() testFile = open('prova.cpp', 'r') lines = testFile.readlines() testFile.close() self.assertEqual(len(lines), 1) self.assertEqual(lines[0], 'std::map< std::string, int >')
def testDumpVariablesImpl(self): tempType = writer_code.TemplateType( 'std::map', [writer_code.intType, writer_code.stringType], 'map') tempVar = writer_code.Variable('pippo', tempType) 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), 4 + 15) self.assertEqual(lines[14], '#include <map>\n') self.assertEqual(lines[15], '#include <string>\n') self.assertEqual(lines[17], 'std::map< int, std::string > pippo;\n')
def testDumpVariablesHeader(self): tempType = writer_code.TemplateType( 'std::map', [writer_code.intType, writer_code.stringType], 'map') tempVar = writer_code.Variable('pippo', tempType) 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), 3 + 19) self.assertEqual(lines[16], '#include <map>\n') self.assertEqual(lines[17], '#include <string>\n') self.assertEqual(lines[18], '\n') self.assertEqual(lines[19], '\n') self.assertEqual(lines[20], '\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')