PhoenixGenerator  2.0.0
Set of tools to generate code
saveClassConfigTest.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 "header_generator.h"
8 #include "cmakelist_generator.h"
9 #include "saveClassConfig.h"
10 #include "saveClassConfigTest.h"
11 
13 
17  if(type == "PString"){
18  return "\"Some string\"";
19  }else if(type == "char"){
20  return "8";
21  }else if(type == "unsigned char"){
22  return "32";
23  }else if(type == "int" || type == "short" || type == "unsigned short"){
24  return "42";
25  }else if(type == "unsigned int"){
26  return "32u";
27  }else if(type == "long int" || type == "long" || type == "ssize_t"){
28  return "328l";
29  }else if(type == "size_t" || type == "long unsigned int"){
30  return "3423420lu";
31  }else if(type == "bool"){
32  return "true";
33  }else{
34  return "";
35  }
36 }
37 
39 
43 PString createCheckClassEquality(const PClassConfig & classConfig, bool isConst){
44  PString body(""), name(classConfig.getName()), strConst("");
45  if(isConst){
46  strConst = "const ";
47  }
48  body += "///Check the equality between two "+name+" classes\n";
49  body += "/**\t@param var : variable to be checked\n";
50  body += " * \t@param reference : reference\n";
51  body += " * \t@return true on success, false otherwise\n";
52  body += "*/\n";
53  body += "bool check"+name+"Equality";
54  if(isConst){
55  body += "Const";
56  }
57  body += "(" + strConst + name + " & var, " + strConst + name + " & reference){\n";
58  body += "\tbool b(true);\n";
59 
60  const std::vector<PClassAttribute> & listAttr = classConfig.getListAttribute();
61  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
62  PString attrName(it->getName().firstToUpper());
63  bool isSimpleType = getIsSimpleType(it->getType());
64  std::cout << "createCheckClassEquality : attrName = '"<<attrName<<"', isSimpleType = " << isSimpleType << ", it->getIsPointer() = " << it->getIsPointer() << ", it->getIsReference() = " << it->getIsReference() << std::endl;
65  if(isSimpleType || it->getIsPointer() || it->getType() == "PString"){
66  body += "\tb &= var.get"+attrName+"() == reference.get"+attrName+"();\n";
67  }
68  }
69 
70  body += "\treturn b;\n";
71  body += "}\n\n";
72  return body;
73 }
74 
76 
80 PString createTestClassCopy(const PClassConfig & classConfig, bool enableTypeStream){
81  PString body (""), name(classConfig.getName());
82 
83  body += "///Check copy of class "+name+"\n";
84  body += "bool check"+name+"Copy(){\n";
85 
86  body += "\t"+name+" reference;\n";
87  body += "\t//Let's use the setters\n";
88  const std::vector<PClassAttribute> & listAttr = classConfig.getListAttribute();
89  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
90  PString defaultValue(getTestDefaultValueTypeInCpp(it->getType())), attrName(it->getName().firstToUpper());;
91  if(defaultValue != ""){
92  body += "\treference.set"+attrName+"("+defaultValue+");\n";
93  }
94  }
95 
96  body += "\t"+name+" varCopy(reference);\n";
97  body += "\t"+name+" varEqual;\n";
98  body += "\tvarEqual = reference;\n";
99  body += "\tbool b(true);\n";
100  body += "\tb &= check"+name+"Equality(varCopy, reference);\n";
101  body += "\tb &= check"+name+"EqualityConst(varCopy, reference);\n";
102 
103  body += "\tb &= check"+name+"Equality(varEqual, reference);\n";
104  body += "\tb &= check"+name+"EqualityConst(varEqual, reference);\n";
105  body += "\tb &= phoenix_getTypeToStr<"+name+">() == \""+name+"\";\n";
106 
107  body += "\treturn b;\n";
108  body += "}\n\n";
109 
110  return body;
111 }
112 
113 
115 
122 bool saveClassTestMain(const PPath & outputCurrentTestDir, const PClassConfig & classConfig, const PPath & baseFileName, bool enableDataStream, bool enableTypeStream){
123  PString body(""), name(classConfig.getName());
124  body += licenceSaveStr() + "\n";
125 
126  body += "#include \"" + (baseFileName + ".h\"\n\n");
127 
128  body += createCheckClassEquality(classConfig, false);
129  body += createCheckClassEquality(classConfig, true);
130 
131  body += createTestClassCopy(classConfig, enableTypeStream);
132 
133  body += "int main(int argc, char ** argv){\n";
134  body += "\tbool b(check"+name+"Copy());\n";
135 
136  //Do other test if the enableDataStream is true
137 
138  body += "\treturn b - 1;\n";
139  body += "}\n";
140 
141  return PPath(outputCurrentTestDir + PString("/main.cpp")).saveFileContent(body);
142 }
143 
145 
152 bool saveClassTestCMakeLists(const PPath & outputCurrentTestDir, const PString & libName, const PClassConfig & classConfig, bool enableDataStream, bool enableTypeStream){
153  PString body(""), testName("test_class_" + classConfig.getName().toLowerUnderscore());
154  body += getCMakeListsHeader();
155  body += "add_executable("+testName+" main.cpp)\n";
156  PString extraLib;
157  extraLib += " " + libName;
158  if(enableTypeStream){
159  extraLib += " phoenix_type_stream";
160  }
161  if(enableDataStream){
162  extraLib += " phoenix_data_stream";
163  }
164  if(extraLib.size() != 0lu){
165  body += "target_link_libraries("+testName+extraLib+")\n";
166  }
167  body += "\n";
168  body += "add_test(NAME "+testName.firstToUpper()+"\n";
169  body += "\tCOMMAND ${CMAKE_CURRENT_BINARY_DIR}/"+testName+"\n";
170  body += "\tWORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}\n";
171  body += ")\n\n\n";
172 
173  return PPath(outputCurrentTestDir + PString("/CMakeLists.txt")).saveFileContent(body);
174 }
175 
177 
185 bool saveClassTest(const PPath & outputTestDir, const PString & libName, const PClassConfig & classConfig, const PPath & baseFileName, bool enableDataStream, bool enableTypeStream){
186  PString name(classConfig.getName());
187  PPath outputCurrentTestDir(outputTestDir + PString("/TEST_") + name.toUpper());
188  if(!outputCurrentTestDir.createDirectory()){std::cerr << "saveClassTest : cannot create TEST directory '"<<outputCurrentTestDir<<"'" << std::endl;return false;}
189 
190  bool b(true);
191  b &= saveClassTestCMakeLists(outputCurrentTestDir, libName, classConfig, enableDataStream, enableTypeStream);
192  b &= saveClassTestMain(outputCurrentTestDir, classConfig, baseFileName, enableDataStream, enableTypeStream);
193 
194  return b;
195 }
196 
197 
199 
207 bool saveClassTest(const PPath & outputTestDir, const PString & libName, const std::vector<PClassConfig> & classConfig, const PPath & baseFileName, bool enableDataStream, bool enableTypeStream){
208  bool b(true);
209  for(std::vector<PClassConfig>::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){
210  b &= saveClassTest(outputTestDir, libName, *it, baseFileName, enableDataStream, enableTypeStream);
211  }
212  return b;
213 }
214 
215 
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.
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 toLowerUnderscore() const
Convert std::string in lower case and space in '_'.
Definition: PString.cpp:618
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:639
PString firstToUpper() const
Convert first letter of the PString in upper case.
Definition: PString.cpp:673
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
PString licenceSaveStr()
Get the licence in string.
PString getTestDefaultValueTypeInCpp(const PString &type)
Get the default value of a type in C++.
PString createTestClassCopy(const PClassConfig &classConfig, bool enableTypeStream)
Create the class check.
PString createCheckClassEquality(const PClassConfig &classConfig, bool isConst)
Create the class check.
bool saveClassTestCMakeLists(const PPath &outputCurrentTestDir, const PString &libName, const PClassConfig &classConfig, bool enableDataStream, bool enableTypeStream)
Save the CMakeLists to be used to compile the unit test.
bool saveClassTestMain(const PPath &outputCurrentTestDir, const PClassConfig &classConfig, const PPath &baseFileName, bool enableDataStream, bool enableTypeStream)
Save the CMakeLists to be used to compile the unit test.
bool saveClassTest(const PPath &outputTestDir, const PString &libName, const PClassConfig &classConfig, const PPath &baseFileName, bool enableDataStream, bool enableTypeStream)
Save the unit test of the generated PClassConfig.
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.