Skip to content

Commit

Permalink
add modifications for count expressions support into Comparator class…
Browse files Browse the repository at this point in the history
… (and unit tests)

issue #31
  • Loading branch information
Valentin Noel committed Sep 4, 2013
1 parent e1d57d4 commit c3b30de
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
9 changes: 6 additions & 3 deletions libraries/comparator/src/Comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace filereader
namespace comparator
{

typedef std::map< std::string, std::shared_ptr< be::Element > > ElementMap;

class Comparator
{
public:
Expand All @@ -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;
};

}
Expand Down
8 changes: 5 additions & 3 deletions libraries/comparator/src/Comparator.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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() );
}

Expand Down
83 changes: 83 additions & 0 deletions libraries/comparator/tests/comparatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<xmlattr>.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()

0 comments on commit c3b30de

Please sign in to comment.