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)\")";
44 PString name(classConfig.
getName());
45 PString moduleName = baseFileName.toLower() +
"_module";
47 fs <<
"def test_check_stream():" << std::endl;
48 fs <<
"\t\"\"\"Test CheckStream functionality\"\"\"" << std::endl;
50 fs <<
"\t# Create two identical " << name << std::endl;
51 fs <<
"\ttest_value1 = " << moduleName <<
"." << name <<
"()" << std::endl;
53 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
55 fs <<
"\ttest_value1." << it->getName() <<
" = " << defaultValue << std::endl;
58 fs <<
"\ttest_value2 = " << moduleName <<
"." << name <<
"()" << std::endl;
59 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
61 fs <<
"\ttest_value2." << it->getName() <<
" = " << defaultValue << 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;
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){
72 PString differentValue = defaultValue;
74 if(it == listAttr.begin()){
75 if(it->getType() ==
"int" || it->getType() ==
"short" || it->getType() ==
"long" || it->getType() ==
"size_t"){
76 differentValue = defaultValue +
" + 1";
77 }
else if(it->getType() ==
"float" || it->getType() ==
"double"){
78 differentValue = defaultValue +
" + 0.5";
79 }
else if(it->getType() ==
"bool"){
80 differentValue =
"not " + defaultValue;
81 }
else if(it->getType() ==
"std::string" || it->getType() ==
"PString"){
82 differentValue = defaultValue +
" + \"_modified\"";
84 differentValue =
"\"different_value\"";
88 fs <<
"\ttest_value3." << it->getName() <<
" = " << differentValue << 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;
106 PString name(classConfig.
getName());
107 fs <<
"test_check_stream()" << std::endl;
std::vector< PClassAttribute > PVecClassAttribute
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
PAbstractNanobindTraitBackend()
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 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.