| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Thibaut Oprinsen | ||
| 3 | Mail : thibaut.oprinsen@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #include "NanobindTraitCheckStream.h" | ||
| 8 | #include "type_utils.h" | ||
| 9 | |||
| 10 | ///Constructor of NanobindTraitCheckStream | ||
| 11 | 1 | NanobindTraitCheckStream::NanobindTraitCheckStream() | |
| 12 | 1 | : PAbstractNanobindTraitBackend() | |
| 13 | 1 | {} | |
| 14 | |||
| 15 | ///Destructor of NanobindTraitCheckStream | ||
| 16 | 2 | NanobindTraitCheckStream::~NanobindTraitCheckStream(){} | |
| 17 | |||
| 18 | ///Registration of static method | ||
| 19 | /// @param fs stream to write to | ||
| 20 | /// @param classConfig config of the class | ||
| 21 | /// @param mode generator mode | ||
| 22 | 2 | void NanobindTraitCheckStream::registerStaticMethod(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{ | |
| 23 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
|
2 | if(!mode.enableCheckStream){return;} |
| 24 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 2 (5→6) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 25 |
5/5✓ Branch 0 (6→7) taken 2 times.
✓ Branch 2 (7→8) taken 2 times.
✓ Branch 4 (8→9) taken 2 times.
✓ Branch 6 (9→10) taken 2 times.
✓ Branch 8 (10→11) taken 2 times.
|
2 | fs << std::endl << std::endl << "\t\t///Check stream method to compare two " << name << " objects"; |
| 26 |
6/6✓ Branch 0 (11→12) taken 2 times.
✓ Branch 2 (12→13) taken 2 times.
✓ Branch 4 (13→14) taken 2 times.
✓ Branch 6 (14→15) taken 2 times.
✓ Branch 8 (15→16) taken 2 times.
✓ Branch 10 (16→17) taken 2 times.
|
2 | fs << std::endl << "\t\t.def_static(\"check_stream\", [](const std::string & fieldDescription, const " << name << " & data, const " << name << " & reference) -> bool {"; |
| 27 |
2/2✓ Branch 0 (17→18) taken 2 times.
✓ Branch 2 (18→19) taken 2 times.
|
2 | fs << std::endl << "\t\t\tstd::stringstream out;"; |
| 28 |
4/4✓ Branch 0 (19→20) taken 2 times.
✓ Branch 2 (20→21) taken 2 times.
✓ Branch 4 (21→22) taken 2 times.
✓ Branch 6 (22→23) taken 2 times.
|
2 | fs << std::endl << "\t\t\tif(!CheckStream<" << name << ">::check_stream(fieldDescription, data, reference, out)){"; |
| 29 |
2/2✓ Branch 0 (23→24) taken 2 times.
✓ Branch 2 (24→25) taken 2 times.
|
2 | fs << std::endl << "\t\t\t\tthrow nb::value_error(out.str().c_str());"; |
| 30 |
2/2✓ Branch 0 (25→26) taken 2 times.
✓ Branch 2 (26→27) taken 2 times.
|
2 | fs << std::endl << "\t\t\t}"; |
| 31 |
2/2✓ Branch 0 (27→28) taken 2 times.
✓ Branch 2 (28→29) taken 2 times.
|
2 | fs << std::endl << "\t\t\treturn true;"; |
| 32 |
2/2✓ Branch 0 (29→30) taken 2 times.
✓ Branch 2 (30→31) taken 2 times.
|
2 | fs << std::endl << "\t\t}, "; |
| 33 |
2/2✓ Branch 0 (31→32) taken 2 times.
✓ Branch 2 (32→33) taken 2 times.
|
2 | fs << std::endl << "\t\tnb::arg(\"fieldDescription\"), nb::arg(\"data\"), nb::arg(\"reference\"),"; |
| 34 |
6/6✓ Branch 0 (33→34) taken 2 times.
✓ Branch 2 (34→35) taken 2 times.
✓ Branch 4 (35→36) taken 2 times.
✓ Branch 6 (36→37) taken 2 times.
✓ Branch 8 (37→38) taken 2 times.
✓ Branch 10 (38→39) taken 2 times.
|
2 | fs << std::endl << "\t\t\"Check stream for " << name << " (equivalent to CheckStream<" << name << ">::check_stream)\")"; |
| 35 | 2 | } | |
| 36 | |||
| 37 | ///Generate test function for check stream | ||
| 38 | /// @param fs stream to write to | ||
| 39 | /// @param classConfig config of the class | ||
| 40 | /// @param mode generator mode | ||
| 41 | /// @param baseFileName : base name of the configuration | ||
| 42 | 2 | void NanobindTraitCheckStream::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode, const PString & baseFileName) const{ | |
| 43 |
2/4✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 2 times.
|
2 | if(!mode.enableUnitTest || !mode.enableCheckStream){return;} |
| 44 |
2/2✓ Branch 0 (5→6) taken 2 times.
✓ Branch 2 (6→7) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 45 |
3/3✓ Branch 0 (7→8) taken 2 times.
✓ Branch 2 (8→9) taken 2 times.
✓ Branch 4 (9→10) taken 2 times.
|
2 | PString moduleName = baseFileName.toLower() + "_module"; |
| 46 | |||
| 47 |
2/2✓ Branch 0 (12→13) taken 2 times.
✓ Branch 2 (13→14) taken 2 times.
|
2 | fs << "def test_check_stream():" << std::endl; |
| 48 |
2/2✓ Branch 0 (14→15) taken 2 times.
✓ Branch 2 (15→16) taken 2 times.
|
2 | fs << "\t\"\"\"Test CheckStream functionality\"\"\"" << std::endl; |
| 49 |
1/1✓ Branch 0 (16→17) taken 2 times.
|
2 | fs << std::endl; |
| 50 |
3/3✓ Branch 0 (17→18) taken 2 times.
✓ Branch 2 (18→19) taken 2 times.
✓ Branch 4 (19→20) taken 2 times.
|
2 | fs << "\t# Create two identical " << name << std::endl; |
| 51 |
6/6✓ Branch 0 (20→21) taken 2 times.
✓ Branch 2 (21→22) taken 2 times.
✓ Branch 4 (22→23) taken 2 times.
✓ Branch 6 (23→24) taken 2 times.
✓ Branch 8 (24→25) taken 2 times.
✓ Branch 10 (25→26) taken 2 times.
|
2 | fs << "\ttest_value1 = " << moduleName << "." << name << "()" << std::endl; |
| 52 |
1/1✓ Branch 0 (26→27) taken 2 times.
|
2 | const PVecClassAttribute & listAttr(classConfig.getListAttribute()); |
| 53 |
2/2✓ Branch 0 (51→28) taken 5 times.
✓ Branch 1 (51→52) taken 2 times.
|
14 | for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ |
| 54 |
2/2✓ Branch 0 (30→31) taken 5 times.
✓ Branch 2 (31→32) taken 5 times.
|
5 | PString defaultValue = getTestDefaultValueTypeInPython(it->getType()); |
| 55 |
6/6✓ Branch 0 (32→33) taken 5 times.
✓ Branch 2 (35→36) taken 5 times.
✓ Branch 4 (36→37) taken 5 times.
✓ Branch 6 (37→38) taken 5 times.
✓ Branch 8 (38→39) taken 5 times.
✓ Branch 10 (39→40) taken 5 times.
|
10 | fs << "\ttest_value1." << it->getName() << " = " << defaultValue << std::endl; |
| 56 | 5 | } | |
| 57 |
1/1✓ Branch 0 (52→53) taken 2 times.
|
2 | fs << std::endl; |
| 58 |
6/6✓ Branch 0 (53→54) taken 2 times.
✓ Branch 2 (54→55) taken 2 times.
✓ Branch 4 (55→56) taken 2 times.
✓ Branch 6 (56→57) taken 2 times.
✓ Branch 8 (57→58) taken 2 times.
✓ Branch 10 (58→59) taken 2 times.
|
2 | fs << "\ttest_value2 = " << moduleName << "." << name << "()" << std::endl; |
| 59 |
2/2✓ Branch 0 (83→60) taken 5 times.
✓ Branch 1 (83→84) taken 2 times.
|
14 | for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ |
| 60 |
2/2✓ Branch 0 (62→63) taken 5 times.
✓ Branch 2 (63→64) taken 5 times.
|
5 | PString defaultValue = getTestDefaultValueTypeInPython(it->getType()); |
| 61 |
6/6✓ Branch 0 (64→65) taken 5 times.
✓ Branch 2 (67→68) taken 5 times.
✓ Branch 4 (68→69) taken 5 times.
✓ Branch 6 (69→70) taken 5 times.
✓ Branch 8 (70→71) taken 5 times.
✓ Branch 10 (71→72) taken 5 times.
|
10 | fs << "\ttest_value2." << it->getName() << " = " << defaultValue << std::endl; |
| 62 | 5 | } | |
| 63 |
1/1✓ Branch 0 (84→85) taken 2 times.
|
2 | fs << std::endl; |
| 64 |
4/4✓ Branch 0 (85→86) taken 2 times.
✓ Branch 2 (86→87) taken 2 times.
✓ Branch 4 (87→88) taken 2 times.
✓ Branch 6 (88→89) taken 2 times.
|
2 | fs << "\t# Test identical " << name << " - should return True" << std::endl; |
| 65 |
6/6✓ Branch 0 (89→90) taken 2 times.
✓ Branch 2 (90→91) taken 2 times.
✓ Branch 4 (91→92) taken 2 times.
✓ Branch 6 (92→93) taken 2 times.
✓ Branch 8 (93→94) taken 2 times.
✓ Branch 10 (94→95) taken 2 times.
|
2 | fs << "\tresult_identical = " << moduleName << "." << name << ".check_stream(\"Test identical\", test_value1, test_value2)" << std::endl; |
| 66 |
4/4✓ Branch 0 (95→96) taken 2 times.
✓ Branch 2 (96→97) taken 2 times.
✓ Branch 4 (97→98) taken 2 times.
✓ Branch 6 (98→99) taken 2 times.
|
2 | fs << "\tassert result_identical == True, \"CheckStream should return True for identical " << name << "\"" << std::endl; |
| 67 |
1/1✓ Branch 0 (99→100) taken 2 times.
|
2 | fs << std::endl; |
| 68 |
3/3✓ Branch 0 (100→101) taken 2 times.
✓ Branch 2 (101→102) taken 2 times.
✓ Branch 4 (102→103) taken 2 times.
|
2 | fs << "\t# Create a different " << name << std::endl; |
| 69 |
6/6✓ Branch 0 (103→104) taken 2 times.
✓ Branch 2 (104→105) taken 2 times.
✓ Branch 4 (105→106) taken 2 times.
✓ Branch 6 (106→107) taken 2 times.
✓ Branch 8 (107→108) taken 2 times.
✓ Branch 10 (108→109) taken 2 times.
|
2 | fs << "\ttest_value3 = " << moduleName << "." << name << "()" << std::endl; |
| 70 |
2/2✓ Branch 0 (214→110) taken 5 times.
✓ Branch 1 (214→215) taken 2 times.
|
14 | for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ |
| 71 |
2/2✓ Branch 0 (112→113) taken 5 times.
✓ Branch 2 (113→114) taken 5 times.
|
5 | PString defaultValue = getTestDefaultValueTypeInPython(it->getType()); |
| 72 |
1/1✓ Branch 0 (114→115) taken 5 times.
|
5 | PString differentValue = defaultValue; |
| 73 | |||
| 74 |
2/2✓ Branch 0 (122→123) taken 2 times.
✓ Branch 1 (122→194) taken 3 times.
|
10 | if(it == listAttr.begin()){ |
| 75 |
4/18✓ Branch 0 (125→126) taken 2 times.
✓ Branch 2 (126→127) taken 2 times.
✗ Branch 4 (127→128) not taken.
✓ Branch 5 (127→143) taken 2 times.
✗ Branch 6 (130→131) not taken.
✗ Branch 8 (131→132) not taken.
✗ Branch 10 (132→133) not taken.
✗ Branch 11 (132→143) not taken.
✗ Branch 12 (135→136) not taken.
✗ Branch 14 (136→137) not taken.
✗ Branch 16 (137→138) not taken.
✗ Branch 17 (137→143) not taken.
✗ Branch 18 (140→141) not taken.
✗ Branch 20 (141→142) not taken.
✗ Branch 22 (142→143) not taken.
✗ Branch 23 (142→144) not taken.
✓ Branch 24 (145→146) taken 2 times.
✗ Branch 25 (145→150) not taken.
|
2 | if(it->getType() == "int" || it->getType() == "short" || it->getType() == "long" || it->getType() == "size_t"){ |
| 76 |
2/2✓ Branch 0 (146→147) taken 2 times.
✓ Branch 2 (147→148) taken 2 times.
|
2 | 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 |
6/6✓ Branch 0 (194→195) taken 5 times.
✓ Branch 2 (197→198) taken 5 times.
✓ Branch 4 (198→199) taken 5 times.
✓ Branch 6 (199→200) taken 5 times.
✓ Branch 8 (200→201) taken 5 times.
✓ Branch 10 (201→202) taken 5 times.
|
10 | fs << "\ttest_value3." << it->getName() << " = " << differentValue << std::endl; |
| 89 | 5 | } | |
| 90 |
1/1✓ Branch 0 (215→216) taken 2 times.
|
2 | fs << std::endl; |
| 91 |
4/4✓ Branch 0 (216→217) taken 2 times.
✓ Branch 2 (217→218) taken 2 times.
✓ Branch 4 (218→219) taken 2 times.
✓ Branch 6 (219→220) taken 2 times.
|
2 | fs << "\t#test different " << name << " - should return False" << std::endl; |
| 92 |
2/2✓ Branch 0 (220→221) taken 2 times.
✓ Branch 2 (221→222) taken 2 times.
|
2 | fs << "\ttry:" << std::endl; |
| 93 |
6/6✓ Branch 0 (222→223) taken 2 times.
✓ Branch 2 (223→224) taken 2 times.
✓ Branch 4 (224→225) taken 2 times.
✓ Branch 6 (225→226) taken 2 times.
✓ Branch 8 (226→227) taken 2 times.
✓ Branch 10 (227→228) taken 2 times.
|
2 | fs << "\t\tresult_different = " << moduleName << "." << name << ".check_stream(\"Test different\", test_value1, test_value3)" << std::endl; |
| 94 |
5/5✓ Branch 0 (228→229) taken 2 times.
✓ Branch 2 (229→230) taken 2 times.
✓ Branch 4 (230→231) taken 2 times.
✓ Branch 6 (231→232) taken 2 times.
✓ Branch 8 (232→233) taken 2 times.
|
2 | fs << "\t\tassert False, \"CheckStream should raise an exception for different " << name << "\"" << std::endl << std::endl; |
| 95 |
2/2✓ Branch 0 (233→234) taken 2 times.
✓ Branch 2 (234→235) taken 2 times.
|
2 | fs << "\texcept:" << std::endl; |
| 96 |
2/2✓ Branch 0 (235→236) taken 2 times.
✓ Branch 2 (236→237) taken 2 times.
|
2 | fs << "\t\tassert True" << std::endl; |
| 97 | // fs << "\tassert result_different == False, \"CheckStream should return False for different " << name << "\"" << std::endl << std::endl; | ||
| 98 | 2 | } | |
| 99 | |||
| 100 | ///Generate test call to check stream function | ||
| 101 | /// @param fs stream to write to | ||
| 102 | /// @param classConfig config of the class | ||
| 103 | /// @param mode generator mode | ||
| 104 | ✗ | void 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 | ✗ | } | |
| 109 |