Commit ecee5cb0 authored by Pavel Vainerman's avatar Pavel Vainerman

(tests): добавил тесты для UniXML, исправил некоторые ошибки.

parent eb49d3c1
...@@ -95,7 +95,7 @@ class HourGlass ...@@ -95,7 +95,7 @@ class HourGlass
_state = st; _state = st;
if( !_state ) if( !_state )
{ {
int cur = t.getCurrent(); timeout_t cur = t.getCurrent();
if( cur > _size ) if( cur > _size )
cur = _size; cur = _size;
...@@ -107,7 +107,7 @@ class HourGlass ...@@ -107,7 +107,7 @@ class HourGlass
} }
else else
{ {
int cur = t.getCurrent(); timeout_t cur = t.getCurrent();
if( cur > _size ) if( cur > _size )
cur = _size; cur = _size;
...@@ -175,7 +175,7 @@ class HourGlass ...@@ -175,7 +175,7 @@ class HourGlass
return ret; return ret;
} }
protected: protected:
PassiveTimer t; /*!< таймер для отсчёта времени.. */ PassiveTimer t; /*!< таймер для отсчёта времени.. */
bool _state; /*!< текущее "положение часов", true - прямое, false - обратное (перевёрнутое) */ bool _state; /*!< текущее "положение часов", true - прямое, false - обратное (перевёрнутое) */
......
...@@ -67,14 +67,31 @@ class UniXML_iterator: ...@@ -67,14 +67,31 @@ class UniXML_iterator:
bool canPrev(); bool canPrev();
bool canNext(); bool canNext();
#if 0
friend inline bool operator==(const UniXML_iterator& lhs, const UniXML_iterator& rhs)
{
return ( lhs.curNode != 0 && rhs.curNode!=0 && lhs.curNode == rhs.curNode );
}
friend inline bool operator!=(const UniXML_iterator& lhs, const UniXML_iterator& rhs){return !(lhs == rhs);}
inline bool operator==(int a)
{
if( a == 0 )
return (curNode == NULL);
return false;
}
#endif
// Перейти к следующему узлу // Перейти к следующему узлу
UniXML_iterator operator ++(int); UniXML_iterator operator+(int);
UniXML_iterator operator ++(); UniXML_iterator operator++(int);
UniXML_iterator operator+=(int);
// Перейти к предыдущему узлу // Перейти к предыдущему узлу
UniXML_iterator operator --(int); UniXML_iterator operator-(int);
UniXML_iterator operator --(); UniXML_iterator operator--(int);
UniXML_iterator operator-=(int);
/*! Перейти на один уровень выше /*! Перейти на один уровень выше
\note Если перейти не удалось, итератор остаётся указывать на прежний узел \note Если перейти не удалось, итератор остаётся указывать на прежний узел
...@@ -93,17 +110,22 @@ class UniXML_iterator: ...@@ -93,17 +110,22 @@ class UniXML_iterator:
} }
// Получить название текущего узла // Получить название текущего узла
const std::string getName() const inline const std::string getName() const
{ {
if( curNode ) if( curNode )
{
if( !curNode->name )
return "";
return (char*) curNode->name; return (char*) curNode->name;
else }
return "";
return "";
} }
const std::string getContent() const; const std::string getContent() const;
operator xmlNode*() const inline operator xmlNode*() const
{ {
//ulog.< "current\n"; //ulog.< "current\n";
return curNode; return curNode;
...@@ -203,6 +225,9 @@ public: ...@@ -203,6 +225,9 @@ public:
xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const; xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
xmlNode* findNodeUtf8( xmlNode* node, const std::string& searchnode, const std::string& name = "") const; xmlNode* findNodeUtf8( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
// ??
//width means number of nodes of the same level as node in 1-st parameter (width number includes first node)
//depth means number of times we can go to the children, if 0 we can't go only to elements of the same level
xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top=true ) const; xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top=true ) const;
xmlNode* extFindNodeUtf8( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top=true ) const; xmlNode* extFindNodeUtf8( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top=true ) const;
......
...@@ -209,7 +209,7 @@ void Configuration::initConfiguration( int argc, const char* const* argv ) ...@@ -209,7 +209,7 @@ void Configuration::initConfiguration( int argc, const char* const* argv )
if( oind == NULL ) if( oind == NULL )
{ {
UniXML_iterator it = unixml.findNode(unixml.getFirstNode(),"ObjectsMap"); UniXML_iterator it = unixml.findNode(unixml.getFirstNode(),"ObjectsMap");
if( it == NULL ) if( !it )
{ {
ucrit << "(Configuration:init): not found <ObjectsMap> node in " << fileConfName << endl; ucrit << "(Configuration:init): not found <ObjectsMap> node in " << fileConfName << endl;
throw SystemError("(Configuration:init): not found <ObjectsMap> node in " + fileConfName ); throw SystemError("(Configuration:init): not found <ObjectsMap> node in " + fileConfName );
......
...@@ -424,7 +424,7 @@ void UniXML_iterator::setProp( const string& name, const string& text ) ...@@ -424,7 +424,7 @@ void UniXML_iterator::setProp( const string& name, const string& text )
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
bool UniXML_iterator::findName( const std::string& node, const std::string& searchname ) bool UniXML_iterator::findName( const std::string& node, const std::string& searchname )
{ {
while( this->find(node) ) while( this->find(node) )
{ {
if ( searchname == getProp("name") ) if ( searchname == getProp("name") )
...@@ -471,89 +471,59 @@ bool UniXML_iterator::find( const std::string& searchnode ) ...@@ -471,89 +471,59 @@ bool UniXML_iterator::find( const std::string& searchnode )
return false; return false;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator++()
{
if (!curNode->next)
{
curNode=curNode->next;
return *this;
}
for(;;)
{
curNode=curNode->next;
if (getName() == "text" || getName() == "comment")
continue;
else
break;
}
return *this;
}
UniXML_iterator UniXML_iterator::operator++(int) UniXML_iterator UniXML_iterator::operator++(int)
{ {
UniXML_iterator it = *this; return (*this)+1;
if (!curNode->next)
{
curNode=curNode->next;
return it;
}
for(;;)
{
curNode=curNode->next;
if (getName() == "text" || getName() == "comment")
continue;
else
break;
}
return it;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator--() UniXML_iterator UniXML_iterator::operator+=(int s)
{ {
if (!curNode->prev) return (*this)+s;
{ }
curNode=curNode->prev; // -------------------------------------------------------------------------
return *this; UniXML_iterator UniXML_iterator::operator+(int step)
} {
int i = 0;
for(;;) while( i<step && curNode )
{ {
curNode=curNode->prev; curNode = curNode->next;
if (getName() == "text" || getName() == "comment") if( curNode )
continue; {
else if( getName() == "text" || getName() == "comment" )
break; continue;
i++;
}
} }
return *this; return *this;
} }
// -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator--(int) UniXML_iterator UniXML_iterator::operator--(int)
{ {
UniXML_iterator it = *this; return (*this)-1;
}
if (!curNode->prev) UniXML_iterator UniXML_iterator::operator-=(int s)
{ {
curNode=curNode->prev; return (*this)-s;
return it; }
} // -------------------------------------------------------------------------
for(;;) UniXML_iterator UniXML_iterator::operator-(int step)
{
int i=0;
while( i<step && curNode )
{ {
curNode=curNode->prev; curNode = curNode->prev;
if (getName() == "text" || getName() == "comment") if( curNode )
continue; {
else if( getName() == "text" || getName() == "comment" )
break; continue;
i++;
}
} }
return it; return (*this);
} }
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
noinst_PROGRAMS = tests tests_with_conf noinst_PROGRAMS = tests tests_with_conf
#unixml ui umutex conftest iterator_test sscanf_hex calibration threadtst dlog #unixml ui umutex conftest iterator_test sscanf_hex calibration threadtst dlog
tests_SOURCES = tests.cc passivetimer.cc hourglass.cc delaytimer.cc tests_SOURCES = tests.cc passivetimer.cc hourglass.cc delaytimer.cc unixml.cc
tests_LDADD = $(top_builddir)/lib/libUniSet2.la tests_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch tests_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch
...@@ -29,10 +29,6 @@ tests_with_conf_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch ...@@ -29,10 +29,6 @@ tests_with_conf_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch
# umutex_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS) # umutex_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
# umutex_CPPFLAGS = -I$(top_builddir)/include $(COMCPP_CFLAGS) # umutex_CPPFLAGS = -I$(top_builddir)/include $(COMCPP_CFLAGS)
# #
# conftest_SOURCES = conftest.cc
# conftest_LDADD = $(top_builddir)/lib/libUniSet2.la
# conftest_CPPFLAGS = -I$(top_builddir)/include
#
# sscanf_hex_SOURCES = sscanf_hex.cc # sscanf_hex_SOURCES = sscanf_hex.cc
# #
# calibration_SOURCES = calibration.cc # calibration_SOURCES = calibration.cc
......
<?xml version="1.0" encoding="utf-8"?>
<UNISETPLC xmlns:xi="http://www.w3.org/2001/XInclude">
<UserData/>
<UniSet> <!-- не закрытый тег -->
</UNISETPLC>
#include <catch.hpp>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
...@@ -6,67 +8,95 @@ using namespace std; ...@@ -6,67 +8,95 @@ using namespace std;
#include "UniXML.h" #include "UniXML.h"
#include "UniSetTypes.h" #include "UniSetTypes.h"
int main() TEST_CASE("UniXML", "[UniXML]" )
{ {
/* try SECTION( "Default constructor" )
{
*/
UniXML xml("test.xml");
xmlNode* cnode = xml.findNode(xml.getFirstNode(),"UniSet");
if( cnode == NULL )
{
cerr << "<testnode> not found" << endl;
return 1;
}
UniXML_iterator it(cnode);
cout << "string id=" << it.getProp("id")
<< " int id=" << it.getIntProp("id")
<< endl;
{
UniXML_iterator it = xml.findNode(xml.getFirstNode(),"LocalInfoServer", "InfoServer");
if( it == NULL )
{
cerr << "<testnode> not found" << endl;
return 1;
}
cout << "string id=" << it.getProp("dbrepeat")
<< " int id=" << it.getIntProp("dbrepeat")
<< endl;
}
{
xmlNode* cnode = xml.findNode(xml.getFirstNode(),"ObjectsMap");
if( cnode == NULL )
{
cerr << "<testnode> not found" << endl;
return 1;
}
UniXML_iterator it = xml.findNode(xml.getFirstNode(),"item", "LocalhostNode");
if( it == NULL )
{
cerr << "<testnode> not found" << endl;
return 1;
}
cout << "textname=" << it.getProp("textname");
}
xml.save("test.out.xml");
/*
}
catch( UniSetTypes::Exception& ex )
{ {
cerr << ex << endl; UniXML xml;
return 1; CHECK_FALSE( xml.isOpen() );
} }
catch( ... )
SECTION( "Bad file" )
{ {
cerr << "catch ... " << endl; REQUIRE_THROWS_AS( UniXML("unknown.xml"), UniSetTypes::NameNotFound );
return 1; REQUIRE_THROWS_AS( UniXML("tests_unixml_badfile.xml"), UniSetTypes::Exception );
} }
*/
return 0; UniXML uxml("tests_unixml.xml");
CHECK( uxml.isOpen() );
xmlNode* cnode = uxml.findNode(uxml.getFirstNode(),"UniSet");
CHECK( cnode != NULL );
// проверка поиска "вглубь"
CHECK( uxml.findNode(uxml.getFirstNode(),"Services") != NULL );
// getProp
// getIntProp
// nextNode
// getPIntProp
// create
// remove
// copy
// nextNode
SECTION( "Iterator" );
{
UniXML_iterator it(cnode);
CHECK( it.getCurrent() != 0 );
it = uxml.begin();
CHECK( it.find("UniSet") != 0 );
// поиск в глубину..
it = uxml.begin();
CHECK( it.find("TestProc") != 0 );
it = uxml.begin();
CHECK( it.getName() == "UNISETPLC" );
it.goChildren();
CHECK( it.getName() == "UserData" );
it += 4;
CHECK( it.getName() == "settings" );
it -= 4;
CHECK( it.getName() == "UserData" );
it = it + 4;
CHECK( it.getName() == "settings" );
it = it - 4;
CHECK( it.getName() == "UserData" );
it++;
CHECK( it.getName() == "UniSet" );
it--;
CHECK( it.getName() == "UserData" );
it = uxml.begin();
CHECK( it.findName("TestNode","TestNode1") != 0 );
it = uxml.begin();
CHECK( it.findName("TestNode","TestNode2") != 0 );
it = uxml.begin();
it.goChildren();
CHECK( it.getName() == "UserData" );
it.goParent();
CHECK( it.getName() == "UNISETPLC" );
it = uxml.begin();
it.goChildren();
it.goEnd();
CHECK( it.getName() == "EndSection" );
it.goBegin();
CHECK( it.getName() == "UserData" );
it = uxml.begin();
CHECK( it.find("TestData") != 0 );
CHECK( it.getProp("text") == "text" );
CHECK( it.getIntProp("x") == 10 );
CHECK( it.getPIntProp("y",-20) == 100 );
CHECK( it.getPIntProp("unknown",20) == 20 );
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment