PhoenixGenerator  2.0.0
Set of tools to generate code
wrapper_test.cpp File Reference
#include "cmakelist_generator.h"
#include "type_utils.h"
#include "wrapper_test.h"
+ Include dependency graph for wrapper_test.cpp:

Go to the source code of this file.

Functions

bool project_wrapper_allClassTest (const PPath &testDirectory, const PString &moduleName, const std::vector< PClassConfig > &vecClassConfig)
 Generate the test for all class inside the module. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
PString pythonGetDefaultTestValue (PString &type)
 Get the default of a given type for a test. More...
 

Function Documentation

◆ project_wrapper_allClassTest()

bool project_wrapper_allClassTest ( const PPath testDirectory,
const PString moduleName,
const std::vector< PClassConfig > &  vecClassConfig 
)

Generate the test for all class inside the module.

Parameters
testDirectory: directory whre to write tests
moduleName: name of the python module
vecClassConfig: vector of class confoguration
Returns
true on success, false otherwise

Definition at line 149 of file wrapper_test.cpp.

149  {
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 }
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.

References project_wrapper_classTest().

Referenced by project_wrapper_moduleGeneratorTest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ project_wrapper_classTest()

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.

Parameters
testDirectory: directory whre to write tests
moduleName: name of the python module
classConfig: class confoguration
mapClass: map of all classes/types defined for this configuration
Returns
true on success, false otherwise

Definition at line 101 of file wrapper_test.cpp.

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 }
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
Extends the std::string.
Definition: PString.h:16
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
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.
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.

References getCMakeListsHeader(), PClassConfig::getIsEnum(), PClassConfig::getListAttribute(), PClassConfig::getName(), pythonAssertTestValue(), pythonDefaultTestValue(), and PPath::saveFileContent().

Referenced by project_wrapper_allClassTest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ project_wrapper_moduleGeneratorTest()

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.

Parameters
projectPath: path of the module
projectConfig: config of the project
moduleName: name of the module
vecClassConfig: vector of class confoguration
vecInclude: vector of includes
Returns
true on success, false otherwise

Definition at line 169 of file wrapper_test.cpp.

169  {
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 }
bool project_wrapper_allClassTest(const PPath &testDirectory, const PString &moduleName, const std::vector< PClassConfig > &vecClassConfig)
Generate the test for all class inside the module.

References PPath::createDirectory(), and project_wrapper_allClassTest().

Referenced by project_wrapper_generator().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pythonAssertTestValue()

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.

Parameters
varName: name of the variable of the class to be used
attr: attribute to be set
moduleName: name of the module to be used
indentation: indentation of the set
mapClass: map of all classes/types defined for this configuration
Returns
corresponding Python

Definition at line 74 of file wrapper_test.cpp.

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 }
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.
PString pythonGetDefaultTestValue(PString &type)
Get the default of a given type for a test.

References PClassAttribute::getIsEnum(), PClassAttribute::getName(), PClassAttribute::getType(), and pythonGetDefaultTestValue().

Referenced by project_wrapper_classTest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pythonDefaultTestValue()

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.

Parameters
varName: name of the variable of the class to be used
attr: attribute to be set
moduleName: name of the module to be used
indentation: indentation of the set
mapClass: map of all classes/types defined for this configuration
Returns
corresponding Python

Definition at line 45 of file wrapper_test.cpp.

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 }

References PClassAttribute::getIsEnum(), PClassAttribute::getName(), PClassAttribute::getType(), and pythonGetDefaultTestValue().

Referenced by project_wrapper_classTest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pythonGetDefaultTestValue()

PString pythonGetDefaultTestValue ( PString type)

Get the default of a given type for a test.

Parameters
type: type of the attribute to be set
Returns
corresponding Python

Definition at line 15 of file wrapper_test.cpp.

15  {
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 }
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

References generator_getListNestedType(), and generator_typeIsList().

Referenced by pythonAssertTestValue(), and pythonDefaultTestValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: