Skip to content

Commit

Permalink
add SpecNode class and unit tests
Browse files Browse the repository at this point in the history
issue #28
  • Loading branch information
Valentin Noel committed Jul 19, 2013
1 parent 5be8cda commit ccbc9c4
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 0 deletions.
95 changes: 95 additions & 0 deletions libraries/specReader/src/SpecNode/SpecNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "SpecNode.hpp"
#include <boost/foreach.hpp>

SpecNode::SpecNode()
{
}

SpecNode::SpecNode( const bpt::ptree::value_type& node )
: _node( node.second )
{
}

SpecNode::~SpecNode()
{
}

void SpecNode::setNode( const bpt::ptree::value_type& node )
{
_node = node.second;
}



std::string SpecNode::getId()
{
return _node.get< std::string >( kId );
}

std::string SpecNode::getLabel()
{
return _node.get< std::string >( kLabel );
}

std::string SpecNode::getType()
{
return _node.get< std::string >( kType );
}

std::string SpecNode::getDisplayType()
{
return _node.get< std::string >( kDisplayType, "" );
}


std::string SpecNode::getCount()
{
return _node.get< std::string >( kCount, "" );
}

std::string SpecNode::getRequired()
{
return _node.get< std::string >( kRequired, "" );
}


std::vector< std::string > SpecNode::getValues()
{
std::vector< std::string > values;
if( boost::optional< bpt::ptree& > valuesNode = _node.get_child_optional( kValues ) )
{
std::string stringValue = _node.get< std::string >( kValues );
if( stringValue.empty() )
{
BOOST_FOREACH( bpt::ptree::value_type& value, valuesNode.get( ) )
values.push_back( value.second.get_value< std::string >() );
}
else
{
values.push_back( stringValue );
}
}
return values;
}



bool SpecNode::isBigEndian()
{
return ( _node.get<std::string>( kEndian, kEndianBig ) == kEndianBig );
}

bool SpecNode::isOptional()
{
return ( _node.get<std::string>( kOptional, kOptionalFalse ) == kOptionalTrue );
}

bool SpecNode::isOrdered()
{
return ( _node.get<std::string>( kOrdered, kOrderedTrue ) == kOrderedTrue );
}

bool SpecNode::hasGroup()
{
return _node.get_child_optional( kGroup );
}
83 changes: 83 additions & 0 deletions libraries/specReader/src/SpecNode/SpecNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef _SPECREADER_SPECNODE_HPP_
#define _SPECREADER_SPECNODE_HPP_

#include <common/global.hpp>

#include <boost/property_tree/ptree.hpp>

namespace bpt = boost::property_tree;

const std::string kId = "id";
const std::string kLabel = "label";

const std::string kAscii = "ascii";
const std::string kHexa = "hexa";
const std::string kType = "type";
const std::string kCount = "count";
const std::string kValues = "values";
const std::string kMap = "map";
const std::string kRange = "range";
const std::string kMin = "min";
const std::string kMax = "max";

const std::string kGroup = "group";
const std::string kGroupSize = "groupSize";

const std::string kRepetition = "repeated";
const std::string kRequired = "required";

const std::string kEndian = "endian";
const std::string kEndianBig = "big";
const std::string kEndianLittle = "little";

const std::string kOptional = "optional";
const std::string kOptionalTrue = "true";
const std::string kOptionalFalse = "false";

const std::string kOrdered = "ordered";
const std::string kOrderedTrue = "true";
const std::string kOrderedFalse = "false";

const std::string kData = "data";
const std::string kDisplayType = "displayType";

class SpecNode
{
public:
SpecNode();
SpecNode( const bpt::ptree::value_type& node );
~SpecNode();

void setNode( const bpt::ptree::value_type& node );

std::string getId();
std::string getLabel();
std::string getType();
std::string getDisplayType();

std::string getCount();
std::string getRequired();

std::vector< std::string > getValues();

// std::vector< std::pair< std::string > > getRange(); // @todo
// std::vector< std::pair< std::string > > getRepetition(); // @todo

// std::map< std::string, std::string > getMap(); // @todo

bool isBigEndian();
bool isOptional();
bool isOrdered();

bool hasGroup();
// std::string getGroupSize(); // @todo

private:
bpt::ptree _node;
};





#endif
98 changes: 98 additions & 0 deletions libraries/specReader/tests/specNodeTests.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <boost/foreach.hpp>

BOOST_AUTO_TEST_SUITE( spec_reader_test_specNode )

BOOST_AUTO_TEST_CASE( spec_reader_specNode )
{
LOG_INFO( "\n>>> spec_reader_specNode <<<" );
{
std::string jsonString = " { \"header\": [ ";
jsonString += " { \"id\": \"node\", \"label\": \"Node\", \"type\": \"type\" },";
jsonString += " { \"id\": \"node\", \"label\": \"Node\", \"type\": \"type\" }";
jsonString += " ] } ";

std::istringstream isstream( jsonString );
bpt::ptree tree;

bpt::read_json( isstream, tree );
size_t i = 0;
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) )
{
SpecNode node;
node.setNode( n );
BOOST_CHECK_EQUAL( node.getId(), "node" );
i++;
}
BOOST_CHECK_EQUAL( i, 2 );

i = 0;
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) )
{
SpecNode node( n );
BOOST_CHECK_EQUAL( node.getLabel(), "Node" );
BOOST_CHECK_EQUAL( node.getType(), "type" );
i++;
}
BOOST_CHECK_EQUAL( i, 2 );
}
{
std::string jsonString = " { \"header\": [ ";
jsonString += " { \"displayType\": \"display\", \"count\": \"123\", \"required\": true }";
jsonString += " ] } ";

std::istringstream isstream( jsonString );
bpt::ptree tree;

bpt::read_json( isstream, tree );
size_t i = 0;
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) )
{
SpecNode node( n );
BOOST_CHECK_EQUAL( node.getDisplayType(), "display" );
BOOST_CHECK_EQUAL( node.getCount(), "123" );
BOOST_CHECK_EQUAL( node.getRequired(), "true" );
i++;
}
BOOST_CHECK_EQUAL( i, 1 );
}
{
std::string jsonString = " { \"header\": [ ";
jsonString += " { \"values\": [ \"value1\", \"value2\", \"value3\" ] }";
jsonString += " ] } ";

std::istringstream isstream( jsonString );
bpt::ptree tree;

bpt::read_json( isstream, tree );
size_t i = 0;
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) )
{
SpecNode node( n );
BOOST_CHECK_EQUAL( node.getValues().at(0), "value1" );
BOOST_CHECK_EQUAL( node.getValues().at(1), "value2" );
BOOST_CHECK_EQUAL( node.getValues().at(2), "value3" );
i++;
}
BOOST_CHECK_EQUAL( i, 1 );
}
{
std::string jsonString = " { \"header\": [ ";
jsonString += " { \"values\": \"value\" }";
jsonString += " ] } ";

std::istringstream isstream( jsonString );
bpt::ptree tree;

bpt::read_json( isstream, tree );
size_t i = 0;
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) )
{
SpecNode node( n );
BOOST_CHECK_EQUAL( node.getValues().at(0), "value" );
i++;
}
BOOST_CHECK_EQUAL( i, 1 );
}
}

BOOST_AUTO_TEST_SUITE_END()
1 change: 1 addition & 0 deletions libraries/specReader/tests/specReaderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ BOOST_AUTO_TEST_SUITE_END()

#include "specificationTests.hpp"
#include "specListTests.hpp"
#include "specNodeTests.hpp"

0 comments on commit ccbc9c4

Please sign in to comment.