PhoenixGenerator  2.2.0
Set of tools to generate code
Loading...
Searching...
No Matches
NanobindTraitCheckStream.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Thibaut Oprinsen
3 Mail : thibaut.oprinsen@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
8#include "type_utils.h"
9
14
17
22void NanobindTraitCheckStream::registerStaticMethod(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
23 if(!mode.enableCheckStream){return;}
24 PString name(classConfig.getName());
25 fs << std::endl << std::endl << "\t\t///Check stream method to compare two " << name << " objects";
26 fs << std::endl << "\t\t.def_static(\"check_stream\", [](const std::string & fieldDescription, const " << name << " & data, const " << name << " & reference) -> bool {";
27 fs << std::endl << "\t\t\tstd::stringstream out;";
28 fs << std::endl << "\t\t\tif(!CheckStream<" << name << ">::check_stream(fieldDescription, data, reference, out)){";
29 fs << std::endl << "\t\t\t\tthrow nb::value_error(out.str().c_str());";
30 fs << std::endl << "\t\t\t}";
31 fs << std::endl << "\t\t\treturn true;";
32 fs << std::endl << "\t\t}, ";
33 fs << std::endl << "\t\tnb::arg(\"fieldDescription\"), nb::arg(\"data\"), nb::arg(\"reference\"),";
34 fs << std::endl << "\t\t\"Check stream for " << name << " (equivalent to CheckStream<" << name << ">::check_stream)\")";
35}
36
42void NanobindTraitCheckStream::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode, const PString & baseFileName) const{
43 if(!mode.enableUnitTest || !mode.enableCheckStream){return;}
44 PString name(classConfig.getName());
45 PString moduleName = baseFileName.toLower() + "_module";
46
47 fs << "def test_check_stream():" << std::endl;
48 fs << "\t\"\"\"Test CheckStream functionality\"\"\"" << std::endl;
49 fs << std::endl;
50 fs << "\t# Create two identical " << name << std::endl;
51 fs << "\ttest_value1 = " << moduleName << "." << name << "()" << std::endl;
52 const PVecClassAttribute & listAttr(classConfig.getListAttribute());
53 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
54 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
55 fs << "\ttest_value1." << it->getName() << " = " << defaultValue << std::endl;
56 }
57 fs << std::endl;
58 fs << "\ttest_value2 = " << moduleName << "." << name << "()" << std::endl;
59 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
60 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
61 fs << "\ttest_value2." << it->getName() << " = " << defaultValue << std::endl;
62 }
63 fs << std::endl;
64 fs << "\t# Test identical " << name << " - should return True" << std::endl;
65 fs << "\tresult_identical = " << moduleName << "." << name << ".check_stream(\"Test identical\", test_value1, test_value2)" << std::endl;
66 fs << "\tassert result_identical == True, \"CheckStream should return True for identical " << name << "\"" << std::endl;
67 fs << std::endl;
68 fs << "\t# Create a different " << name << std::endl;
69 fs << "\ttest_value3 = " << moduleName << "." << name << "()" << std::endl;
70 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
71 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
72 PString differentValue = defaultValue;
73
74 if(it == listAttr.begin()){
75 if(it->getType() == "int" || it->getType() == "short" || it->getType() == "long" || it->getType() == "size_t"){
76 differentValue = defaultValue + " + 1"; // 42 + 1 = 43
77 }else if(it->getType() == "float" || it->getType() == "double"){
78 differentValue = defaultValue + " + 0.5"; // 1.0 + 0.5 = 1.5
79 }else if(it->getType() == "bool"){
80 differentValue = "not " + defaultValue; // not True = False
81 }else if(it->getType() == "std::string" || it->getType() == "PString"){
82 differentValue = defaultValue + " + \"_modified\""; // "Some string" + "_modified"
83 }else{
84 differentValue = "\"different_value\"";
85 }
86 }
87
88 fs << "\ttest_value3." << it->getName() << " = " << differentValue << std::endl;
89 }
90 fs << std::endl;
91 fs << "\t#test different " << name << " - should return False" << std::endl;
92 fs << "\ttry:" << std::endl;
93 fs << "\t\tresult_different = " << moduleName << "." << name << ".check_stream(\"Test different\", test_value1, test_value3)" << std::endl;
94 fs << "\t\tassert False, \"CheckStream should raise an exception for different " << name << "\"" << std::endl << std::endl;
95 fs << "\texcept:" << std::endl;
96 fs << "\t\tassert True" << std::endl;
97 // fs << "\tassert result_different == False, \"CheckStream should return False for different " << name << "\"" << std::endl << std::endl;
98}
99
104void NanobindTraitCheckStream::testCallFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
105 if(!mode.enableUnitTest || !mode.enableCheckStream){return;}
106 PString name(classConfig.getName());
107 fs << "test_check_stream()" << std::endl;
108}
std::vector< PClassAttribute > PVecClassAttribute
Definition PDataConfig.h:13
virtual ~NanobindTraitCheckStream()
Destructor of NanobindTraitCheckStream.
virtual void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
virtual void registerStaticMethod(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
NanobindTraitCheckStream()
Constructor of NanobindTraitCheckStream.
virtual void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode, const PString &baseFileName) const
Class to describe a basic class.
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PString & getName() const
Returns the class name.
All the genertor modes.
bool enableCheckStream
True to enable check stream interface generator.
bool enableUnitTest
True to enable the unit tests.
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.