From c3b30de9c2698768a98f01fccbf49c7e6e205682 Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Wed, 4 Sep 2013 17:33:09 +0200 Subject: [PATCH] add modifications for count expressions support into Comparator class (and unit tests) issue #31 --- libraries/comparator/src/Comparator.hpp | 9 +- libraries/comparator/src/Comparator.tcc | 8 +- .../comparator/tests/comparatorTests.cpp | 83 +++++++++++++++++++ 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/libraries/comparator/src/Comparator.hpp b/libraries/comparator/src/Comparator.hpp index dfe974f..a4d345b 100644 --- a/libraries/comparator/src/Comparator.hpp +++ b/libraries/comparator/src/Comparator.hpp @@ -13,6 +13,8 @@ namespace filereader namespace comparator { +typedef std::map< std::string, std::shared_ptr< be::Element > > ElementMap; + class Comparator { public: @@ -32,9 +34,10 @@ class Comparator std::shared_ptr< be::Element > getElementFromNode( const spec_reader::SpecNode& node ); private: - filereader::FileReader* _file; - spec_reader::SpecList _specs; - report_generator::Report* _report; + filereader::FileReader* _file; + spec_reader::SpecList _specs; + report_generator::Report* _report; + ElementMap _elementList; }; } diff --git a/libraries/comparator/src/Comparator.tcc b/libraries/comparator/src/Comparator.tcc index f12ba94..c3164e0 100644 --- a/libraries/comparator/src/Comparator.tcc +++ b/libraries/comparator/src/Comparator.tcc @@ -65,6 +65,9 @@ std::shared_ptr< ElementType > Comparator::getElement( const sr::SpecNode& node element->setMap( node.getMap() ); element->checkData(); + + _elementList.insert( std::make_pair( node.getId(), element ) ); + return element; } @@ -81,10 +84,9 @@ std::shared_ptr< be::data_element::Data > Comparator::getElement< be::data_eleme element->setDisplayType( node.getDisplayType() ); size_t size = element->getSize(); - if( size == 0 && ! node.getCount().empty() ) // @todo: create a map of variables for expression parser! + if( size == 0 && ! node.getCount().empty() ) { - std::map < std::string, size_t > varMap; // @todelete: when variables map is got! - be::expression_parser::ExpressionParser sizeParser( varMap ); + be::expression_parser::ExpressionParser sizeParser( _elementList ); size = sizeParser.getExpressionResult< size_t >( node.getCount() ); } diff --git a/libraries/comparator/tests/comparatorTests.cpp b/libraries/comparator/tests/comparatorTests.cpp index e5972fd..6f165c6 100644 --- a/libraries/comparator/tests/comparatorTests.cpp +++ b/libraries/comparator/tests/comparatorTests.cpp @@ -1169,4 +1169,87 @@ BOOST_AUTO_TEST_CASE( comparator_comparator_validation_2 ) } +BOOST_AUTO_TEST_CASE( comparator_comparator_validation_3 ) +{ + LOG_WARNING( ">>> comparator_comparator_validation_3 <<<" ); + { + std::string jsonString = R"*( + { + "standard": + { + "id": "test", + "extension": [ + "ext1" + ] + }, + "header": [ + { + "id": "length", + "label": "Sixteen times length of FILE", + "type": "uint8" + }, + { + "id": "file", + "label": "FILE", + "type": "ascii", + "count": "length / 16" + }, + { + "id": "size", + "label": "Eleven times size HEY", + "type": "uint8" + }, + { + "id": "hey", + "label": "HEY", + "type": "ascii", + "count": "size / 11" + } + ] + } + )*"; + + sr::Specification spec; + sr::SpecList specList; + + spec.setFromString( jsonString ); + specList.addSpecification( spec ); + + std::stringbuf buffer; + buffer.str( "@FILE!HEY" ); + fr::FileReader file( &buffer ); + + Comparator comp( &file, specList ); + + rg::Report report; + comp.compare( "test", report ); + + rg::Transform tr( report ); + rg::Export exporter( tr.transformTree( rg::Transform::eReportTypeXml ) ); + + LOG_INFO( "\n==== REPORT ====" ); + // exporter.writeXmlFile( "test_validation_2.xml" ); + LOG_INFO( exporter.getXmlString() ); + + std::istringstream xmlStream( exporter.getXmlString() ); + std::istringstream jsonStream( jsonString ); + bpt::ptree xmlReport; + bpt::ptree jsonReport; + bpt::read_xml ( xmlStream, xmlReport ); + bpt::read_json( jsonStream, jsonReport ); + + BOOST_CHECK_EQUAL( xmlReport.size(), jsonReport.get_child( "header" ).size() ); + + std::vector< std::string > xmlIds; + std::vector< std::string > jsonIds; + + fillVectorXml( xmlReport, xmlIds, ".id" ); + fillVectorJson( jsonReport.get_child( "header" ), jsonIds, "id" ); + + BOOST_CHECK_EQUAL( xmlIds.size(), jsonIds.size() ); + for( size_t i = 0; i < xmlIds.size(); ++i ) + BOOST_CHECK_EQUAL( xmlIds.at(i), jsonIds.at(i) ); + } +} + BOOST_AUTO_TEST_SUITE_END()