PhoenixGenerator  2.0.0
Set of tools to generate code
wrapper_test.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include "cmakelist_generator.h"
8 #include "type_utils.h"
9 #include "wrapper_test.h"
10 
12 
16  if(type == "int" || type == "long" || type == "long int" || type == "size_t" || type == "unsigned int" || type == "unsigned short" || type == "char" || type == "unsigned char" || type == "long unsigned int"){
17  return "42";
18  }else if(type == "float" || type == "double"){
19  return "42.0";
20  }else if(type == "std::string"){
21  return "\"Shadoko\"";
22  }else if(type == "DataStreamMsg"){
23  return "bytearray(b\"Shadoko\")";
24  }else if(generator_typeIsList(type)){
25  PString nestedType(generator_getListNestedType(type));
26  PString nestedDefaultValue(pythonGetDefaultTestValue(nestedType));
27  if(nestedDefaultValue != ""){
28  return "["+nestedDefaultValue+"]";
29  }else{
30  return "[]";
31  }
32  }
33 
34  return "";
35 }
36 
38 
45 PString pythonDefaultTestValue(const PString & varName, const PClassAttribute & attr, const PString & moduleName, const PString & indentation,
46  const std::map<std::string, PClassConfig> & mapClass)
47 {
48  PString type(attr.getType());
49  PString body(indentation);
50 
51  PString initValue(pythonGetDefaultTestValue(type));
52  if(initValue != ""){
53  body += varName + "." + attr.getName() + " = "+initValue+"\n";
54  }else if(attr.getIsEnum()){
55  std::map<std::string, PClassConfig>::const_iterator it(mapClass.find(type));
56  if(it != mapClass.end()){
57  //Let's take the last value so we are sure this is not a 0 or a default value
58  body += varName + "." + attr.getName() + " = "+moduleName+"."+type+"."+it->second.getListAttribute().back().getName()+"\n";
59  }
60  }else{
61  return "";
62  }
63  return body;
64 }
65 
67 
74 PString pythonAssertTestValue(const PString & varName, const PClassAttribute & attr, const PString & moduleName, const PString & indentation,
75  const std::map<std::string, PClassConfig> & mapClass)
76 {
77  PString type(attr.getType());
78  PString body(indentation);
79 
80  PString initValue(pythonGetDefaultTestValue(type));
81  if(initValue != ""){
82  body += "assert " + varName + "." + attr.getName() + " == "+initValue+"\n";
83  }else if(attr.getIsEnum()){
84  std::map<std::string, PClassConfig>::const_iterator it(mapClass.find(type));
85  if(it != mapClass.end()){
86  body += "assert " + varName + "." + attr.getName() + ".value == "+moduleName+"."+type+"."+it->second.getListAttribute().back().getName()+".value\n";
87  }
88  }else{
89  return "";
90  }
91  return body;
92 }
93 
95 
101 bool project_wrapper_classTest(const PPath & testDirectory, const PString & moduleName, const PClassConfig & classConfig,
102  const std::map<std::string, PClassConfig> & mapClass)
103 {
104  PString body, className(classConfig.getName());
105  body += "\n";
106  body += getCMakeListsHeader();
107  body += "\n";
108  if(!classConfig.getIsEnum()){
109  body += "import "+moduleName+"\n";
110  }
111  body += "from "+moduleName+" import "+className+"\n";
112  body += "\n";
113  body += "#Unit Test of the " + className + "\n";
114  body += "def test_"+className+"():\n";
115  body += "\tassert "+className+".getTypeName() == \"" + className + "\"\n";
116  if(!classConfig.getIsEnum()){
117  body += "\t#Let's test the stream now\n";
118  body += "\tshadok = "+moduleName+"."+className+"()\n";
119  const std::vector<PClassAttribute> & vecAttr = classConfig.getListAttribute();
120  for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
121  body += pythonDefaultTestValue("shadok", *it, moduleName, "\t", mapClass);
122  }
123  body += "\tstream = shadok.toBytes()\n";
124  body += "\tassert len(stream) != 0\n";
125  body += "\t\n";
126  body += "\tother = "+moduleName+"."+className+"()\n";
127  body += "\tother.fromBytes(stream)\n";
128  for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
129  body += pythonAssertTestValue("other", *it, moduleName, "\t", mapClass);
130  }
131  body += "\t\n";
132  body += "\t\n";
133  body += "\t\n";
134  body += "\t\n";
135  body += "\t\n";
136  }
137 
138  PPath fileName(testDirectory / PPath("test_" + className + ".py"));
139  return fileName.saveFileContent(body);
140 }
141 
142 
144 
149 bool project_wrapper_allClassTest(const PPath & testDirectory, const PString & moduleName, const std::vector<PClassConfig> & vecClassConfig){
150  bool b(true);
151  std::map<std::string, PClassConfig> mapClass;
152  for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end() && b; ++it){
153  mapClass[it->getName()] = *it;
154  }
155  for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end() && b; ++it){
156  b &= project_wrapper_classTest(testDirectory, moduleName, *it, mapClass);
157  }
158  return b;
159 }
160 
162 
169 bool project_wrapper_moduleGeneratorTest(const PPath & projectPath, const ProjectConfig & projectConfig, const PString & moduleName, const std::vector<PClassConfig> & vecClassConfig, const PVecPath & vecInclude){
170  PPath testDirectory(projectPath / PPath("tests"));
171  if(!testDirectory.createDirectory()){
172  std::cerr << "project_wrapper_generator : cannot create unit tests directory of project '"<<testDirectory<<"'" << std::endl;
173  return false;
174  }
175  if(!project_wrapper_allClassTest(testDirectory, moduleName, vecClassConfig)){return false;}
176 
177 
178  return true;
179 }
180 
181 
std::vector< PPath > PVecPath
Definition: PPath.h:75
Describes a class attribute.
const PString & getType() const
Gets the type of the PClassAttribute.
bool getIsEnum() const
Gets the isEnum of the PClassAttribute.
const PString & getName() const
Gets the name of the PClassAttribute.
Class to describe a basic class.
Definition: PClassConfig.h:14
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PString & getName() const
Returns the class name.
bool getIsEnum() const
Say if the current PClassConfig is an enum.
Path of a directory or a file.
Definition: PPath.h:17
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
bool createDirectory(mode_t mode=0755) const
Create the current directory.
Definition: PPath.cpp:331
Extends the std::string.
Definition: PString.h:16
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
Configuration of the project.
PString generator_getListNestedType(const PString &type)
Get the nested type inside a std::list.
Definition: type_utils.cpp:22
bool generator_typeIsList(const PString &type)
Say if a given type is a std::list.
Definition: type_utils.cpp:14
bool project_wrapper_moduleGeneratorTest(const PPath &projectPath, const ProjectConfig &projectConfig, const PString &moduleName, const std::vector< PClassConfig > &vecClassConfig, const PVecPath &vecInclude)
Generate the unit tests of the project.
PString pythonAssertTestValue(const PString &varName, const PClassAttribute &attr, const PString &moduleName, const PString &indentation, const std::map< std::string, PClassConfig > &mapClass)
Get the default set of given attribute for a test.
bool project_wrapper_allClassTest(const PPath &testDirectory, const PString &moduleName, const std::vector< PClassConfig > &vecClassConfig)
Generate the test for all class inside the module.
bool project_wrapper_classTest(const PPath &testDirectory, const PString &moduleName, const PClassConfig &classConfig, const std::map< std::string, PClassConfig > &mapClass)
Generate the test for all class inside the module.
PString pythonDefaultTestValue(const PString &varName, const PClassAttribute &attr, const PString &moduleName, const PString &indentation, const std::map< std::string, PClassConfig > &mapClass)
Get the default set of given attribute for a test.
PString pythonGetDefaultTestValue(PString &type)
Get the default of a given type for a test.