PhoenixGenerator  2.8.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 if(classConfig.getIsEnum()){
26 name = name + "::" + name;
27 }
28 fs << std::endl << std::endl << "\t\t///Check stream method to compare two " << name << " objects";
29 fs << std::endl << "\t\t.def_static(\"check_stream\", [](const std::string & fieldDescription, const " << name << " & data, const " << name << " & reference) -> bool {";
30 fs << std::endl << "\t\t\tstd::stringstream out;";
31 fs << std::endl << "\t\t\tif(!CheckStream<" << name << ">::check_stream(fieldDescription, data, reference, out)){";
32 fs << std::endl << "\t\t\t\tthrow nb::value_error(out.str().c_str());";
33 fs << std::endl << "\t\t\t}";
34 fs << std::endl << "\t\t\treturn true;";
35 fs << std::endl << "\t\t}, ";
36 fs << std::endl << "\t\tnb::arg(\"fieldDescription\"), nb::arg(\"data\"), nb::arg(\"reference\"),";
37 fs << std::endl << "\t\t\"Check stream for " << name << " (equivalent to CheckStream<" << name << ">::check_stream)\")";
38}
39
45void NanobindTraitCheckStream::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode, const PString & baseFileName) const{
46 if(!mode.enableUnitTest || !mode.enableCheckStream){return;}
47 PString name(classConfig.getName());
48 PString moduleName = baseFileName.toLower();
49
50 fs << "def test_check_stream():" << std::endl;
51 fs << "\t\"\"\"Test CheckStream functionality\"\"\"" << std::endl;
52 fs << std::endl;
53 fs << "\t# Create two identical " << name << std::endl;
54 const PVecClassAttribute & listAttr(classConfig.getListAttribute());
55 if(classConfig.getIsEnum()){
56 if(listAttr.size() != 0lu){
57 fs << "\ttest_value1 = " << moduleName << "." << name << "." << listAttr.front().getName() << std::endl;
58 fs << "\ttest_value2 = " << moduleName << "." << name << "." << listAttr.front().getName() << std::endl;
59 }
60 fs << "\t# Test identical " << name << " - should return True" << std::endl;
61 fs << "\tresult_identical = " << moduleName << "." << name << ".check_stream(\"Test identical\", test_value1, test_value2)" << std::endl;
62 fs << "\tassert result_identical == True, \"CheckStream should return True for identical " << name << "\"" << std::endl;
63 }else{
64 fs << "\ttest_value1 = " << moduleName << "." << name << "()" << std::endl;
65
66 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
67 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
68 if(defaultValue != ""){
69 fs << "\ttest_value1." << it->getName() << " = " << defaultValue << std::endl;
70 }
71 }
72 fs << std::endl;
73 fs << "\ttest_value2 = " << moduleName << "." << name << "()" << std::endl;
74 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
75 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
76 if(defaultValue != ""){
77 fs << "\ttest_value2." << it->getName() << " = " << defaultValue << std::endl;
78 }
79 }
80 fs << std::endl;
81 fs << "\t# Test identical " << name << " - should return True" << std::endl;
82 fs << "\tresult_identical = " << moduleName << "." << name << ".check_stream(\"Test identical\", test_value1, test_value2)" << std::endl;
83 fs << "\tassert result_identical == True, \"CheckStream should return True for identical " << name << "\"" << std::endl;
84 fs << std::endl;
85 }
86}
87
92void NanobindTraitCheckStream::testCallFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
93 if(!mode.enableUnitTest || !mode.enableCheckStream){return;}
94 PString name(classConfig.getName());
95 fs << "test_check_stream()" << std::endl;
96}
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.
bool getIsEnum() const
Say if the current PClassConfig is an enum.
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.