Skip to content

Commit

Permalink
change children management and update next() method in Element (and u…
Browse files Browse the repository at this point in the history
…nit tests)

issue #27
  • Loading branch information
Valentin Noel committed Dec 23, 2013
1 parent 6f63041 commit ba95c52
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
23 changes: 16 additions & 7 deletions libraries/BasicElement/src/BasicElement/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ Element::Element( const ShPtrSpecNode node,
, _keepEndingChar ( node->keepEndingChar() )
, _checkedGroup ( false )
{
if( ! _parent.expired() )
_parent.lock()->_children.push_back( std::make_shared< Element >( *this ) );

LOG_TRACE( "[element] Create new Element " << _id << " :" );
if( ! _parent.expired() )
LOG_TRACE(" [element] - Parent : " << _parent.lock()->_id );
Expand All @@ -53,22 +50,25 @@ Element::Element( const ShPtrSpecNode node,

Element::ShPtrSpecNode Element::next( )
{
ShPtrElement parent;
ShPtrElement parent;
// if parent exists, copy it
if( _parent.use_count() != 0 )
parent = _parent.lock();

if( parent != nullptr && parent->_status == eStatusSkip )
return parent->next( );

if( _status == eStatusSkip )
{
LOG_TRACE( "[element] Next: next node of " << _id<< " ( optional / skip )" << std::endl);
if( _specNode->next() != nullptr )
LOG_TRACE( "[element] Next: next node of " << _id<< " ( optional / skip )" );
if( _specNode->next() != nullptr && ( parent == nullptr || parent->_status != eStatusSkip ) )
return _specNode->next();
if( parent != nullptr )
return parent->next( );
}

// Unordered Groups: if element valid and parent is unordered, go to the first child of the parent
if( _status == eStatusValid && _parent.use_count() != 0 && ( ! parent->_isOrdered ) && ( ! _isGroup || _checkedGroup ) )
if( ( _status == eStatusValid || _status == eStatusUnknown ) && _parent.use_count() != 0 && ( ! parent->_isOrdered ) && ( ! _isGroup || _checkedGroup ) )
{
LOG_TRACE( "[element] Next: " << _id << "'s parent first child ( unordered group )" );
return parent->_specNode->firstChild();
Expand Down Expand Up @@ -109,6 +109,15 @@ void Element::set( const std::vector< char >& data )
std::memcpy( &_data[0], &data[0], data.size() );
}

void Element::addChild( const ShPtrElement& element )
{
if( element->getParent()->_uId != _uId )
{
LOG_TRACE( "[element] wrong parent, cannot add child." );
return;
}
_children.push_back( element );
}

std::string Element::getPropertiesFlag()
{
Expand Down
7 changes: 6 additions & 1 deletion libraries/BasicElement/src/BasicElement/Element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Element
/**
* Set raw data from buffer.
* @param[in] data Data buffer to copy from.
* @param[in] size Buffer size (in bytes).
*/
void set( const std::vector< char >& data );

Expand All @@ -80,6 +79,12 @@ class Element
*/
ShPtrSpecNode getSpecNode() { return _specNode; }

/**
* Add a child to the current Element.
* @param[in] Child reference.
*/
void addChild( const ShPtrElement& element );

/**
* Get the current Element children.
* @return Vector of children references.
Expand Down
19 changes: 19 additions & 0 deletions libraries/BasicElement/test/elementTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ inline ShPtrElement checkElement( ShPtrElement& elem, ShPtrElement& previous, Sh
BOOST_CHECK_EQUAL( newElem->getPrevious()->_uId, elem->_uId );
BOOST_CHECK_EQUAL( node->getId(), nodeId );
newElem->_status = eStatusValid;
parent->addChild( newElem );
return newElem;
}

Expand Down Expand Up @@ -878,13 +879,31 @@ BOOST_AUTO_TEST_CASE( basic_element_get_children )
BOOST_CHECK( node->next() == nullptr );

BOOST_CHECK_EQUAL( elem1->getChildren().size(), 3 );
BOOST_CHECK_EQUAL( elem1->getChildren().at( 0 ), elem2 );
BOOST_CHECK_EQUAL( elem1->getChildren().at( 1 ), elem3 );
BOOST_CHECK_EQUAL( elem1->getChildren().at( 2 ), elem7 );

BOOST_CHECK_EQUAL( elem2->getChildren().size(), 0 );

BOOST_CHECK_EQUAL( elem3->getChildren().size(), 1 );
BOOST_CHECK_EQUAL( elem3->getChildren().at( 0 ), elem4 );

BOOST_CHECK_EQUAL( elem4->getChildren().size(), 2 );
BOOST_CHECK_EQUAL( elem4->getChildren().at( 0 ), elem5 );
BOOST_CHECK_EQUAL( elem4->getChildren().at( 1 ), elem6 );

BOOST_CHECK_EQUAL( elem5->getChildren().size(), 0 );

BOOST_CHECK_EQUAL( elem6->getChildren().size(), 0 );

BOOST_CHECK_EQUAL( elem7->getChildren().size(), 1 );
BOOST_CHECK_EQUAL( elem7->getChildren().at( 0 ), elem8 );

BOOST_CHECK_EQUAL( elem8->getChildren().size(), 0 );

elem8->_status = eStatusUnknown;
BOOST_CHECK_EQUAL( elem7->getChildren().at( 0 )->_status, eStatusUnknown );
BOOST_CHECK_EQUAL( elem1->getChildren().at( 2 )->getChildren().at( 0 )->_status, eStatusUnknown );
}
}

Expand Down

0 comments on commit ba95c52

Please sign in to comment.