PhoenixGenerator  2.0.0
Set of tools to generate code
saveClassConfigTest.cpp File Reference
+ Include dependency graph for saveClassConfigTest.cpp:

Go to the source code of this file.

Functions

PString createCheckClassEquality (const PClassConfig &classConfig, bool isConst)
 Create the class check. More...
 
PString createTestClassCopy (const PClassConfig &classConfig, bool enableTypeStream)
 Create the class check. More...
 
PString getTestDefaultValueTypeInCpp (const PString &type)
 Get the default value of a type in C++. More...
 
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. More...
 
bool saveClassTest (const PPath &outputTestDir, const PString &libName, const std::vector< PClassConfig > &classConfig, const PPath &baseFileName, bool enableDataStream, bool enableTypeStream)
 Save the unit test of the generated PClassConfig. More...
 
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. More...
 
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. More...
 

Function Documentation

◆ createCheckClassEquality()

PString createCheckClassEquality ( const PClassConfig classConfig,
bool  isConst 
)

Create the class check.

Parameters
classConfig: class configuration to be used
isConst: true if the generated check has to use const classes
Returns
code of the generated function

Definition at line 43 of file saveClassConfigTest.cpp.

43  {
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 }
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PString & getName() const
Returns the class name.
Extends the std::string.
Definition: PString.h:16
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.

References getIsSimpleType(), PClassConfig::getListAttribute(), and PClassConfig::getName().

Referenced by saveClassTestMain().

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

◆ createTestClassCopy()

PString createTestClassCopy ( const PClassConfig classConfig,
bool  enableTypeStream 
)

Create the class check.

Parameters
classConfig: class configuration to be used
enableTypeStream: true to test phoenix_getTypeName function
Returns
code of the generated function

Definition at line 80 of file saveClassConfigTest.cpp.

80  {
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 }
PString getTestDefaultValueTypeInCpp(const PString &type)
Get the default value of a type in C++.

References PClassConfig::getListAttribute(), PClassConfig::getName(), and getTestDefaultValueTypeInCpp().

Referenced by saveClassTestMain().

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

◆ getTestDefaultValueTypeInCpp()

PString getTestDefaultValueTypeInCpp ( const PString type)

Get the default value of a type in C++.

Parameters
type: type to be converted
Returns
default value of the type in C++

Definition at line 16 of file saveClassConfigTest.cpp.

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

Referenced by createTestClassCopy().

+ Here is the caller graph for this function:

◆ saveClassTest() [1/2]

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.

Parameters
outputTestDir: output directory where to put the genreated test (typically ../TESTS if the program is called from ./src)
libName: name of the generated library to link with
classConfig: class configuration to be used
baseFileName: base of output file name of the generated sources
enableDataStream: true to enable the data stream test
enableTypeStream: true to test phoenix_getTypeName function
Returns
true on success, false oterhwise

Definition at line 185 of file saveClassConfigTest.cpp.

185  {
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 }
Path of a directory or a file.
Definition: PPath.h:17
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:639
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.

References PPath::createDirectory(), PClassConfig::getName(), saveClassTestCMakeLists(), saveClassTestMain(), and PString::toUpper().

Referenced by saveClassTest(), and saveParserClassConfig().

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

◆ saveClassTest() [2/2]

bool saveClassTest ( const PPath outputTestDir,
const PString libName,
const std::vector< PClassConfig > &  classConfig,
const PPath baseFileName,
bool  enableDataStream,
bool  enableTypeStream 
)

Save the unit test of the generated PClassConfig.

Parameters
outputTestDir: output directory where to put the genreated test (typically ../TESTS if the program is called from ./src)
libName: name of the generated library to link with
classConfig: class configuration to be used
baseFileName: base of output file name of the generated sources
enableDataStream: true to enable the data stream test
enableTypeStream: true to test phoenix_getTypeName function
Returns
true on success, false oterhwise

Definition at line 207 of file saveClassConfigTest.cpp.

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

References saveClassTest().

+ Here is the call graph for this function:

◆ saveClassTestCMakeLists()

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.

Parameters
outputCurrentTestDir: current test directoru in which to create the CMakeLists.txt
libName: name of the generated library to link with
classConfig: class configuration to be used
enableDataStream: true to enable the data stream test
enableTypeStream: true to test phoenix_getTypeName function
Returns
true on success, false oterhwise

Definition at line 152 of file saveClassConfigTest.cpp.

152  {
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 }
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
PString toLowerUnderscore() const
Convert std::string in lower case and space in '_'.
Definition: PString.cpp:618
PString firstToUpper() const
Convert first letter of the PString in upper case.
Definition: PString.cpp:673
PString getCMakeListsHeader()
Get the CMakeLists.txt header.

References PString::firstToUpper(), getCMakeListsHeader(), PClassConfig::getName(), PPath::saveFileContent(), and PString::toLowerUnderscore().

Referenced by saveClassTest().

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

◆ saveClassTestMain()

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.

Parameters
outputCurrentTestDir: current test directoru in which to create the CMakeLists.txt
classConfig: class configuration to be used
baseFileName: base of output file name of the generated sources
enableDataStream: true to enable the data stream test
enableTypeStream: true to test phoenix_getTypeName function
Returns
true on success, false oterhwise

Definition at line 122 of file saveClassConfigTest.cpp.

122  {
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 }
PString licenceSaveStr()
Get the licence in string.
PString createTestClassCopy(const PClassConfig &classConfig, bool enableTypeStream)
Create the class check.
PString createCheckClassEquality(const PClassConfig &classConfig, bool isConst)
Create the class check.

References createCheckClassEquality(), createTestClassCopy(), PClassConfig::getName(), licenceSaveStr(), and PPath::saveFileContent().

Referenced by saveClassTest().

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