Commit ecee5cb0 authored by Pavel Vainerman's avatar Pavel Vainerman

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

parent eb49d3c1
......@@ -95,7 +95,7 @@ class HourGlass
_state = st;
if( !_state )
{
int cur = t.getCurrent();
timeout_t cur = t.getCurrent();
if( cur > _size )
cur = _size;
......@@ -107,7 +107,7 @@ class HourGlass
}
else
{
int cur = t.getCurrent();
timeout_t cur = t.getCurrent();
if( cur > _size )
cur = _size;
......
......@@ -67,14 +67,31 @@ class UniXML_iterator:
bool canPrev();
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 ++();
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);
UniXML_iterator operator-=(int);
/*! Перейти на один уровень выше
\note Если перейти не удалось, итератор остаётся указывать на прежний узел
......@@ -93,17 +110,22 @@ class UniXML_iterator:
}
// Получить название текущего узла
const std::string getName() const
inline const std::string getName() const
{
if( curNode )
{
if( !curNode->name )
return "";
return (char*) curNode->name;
else
}
return "";
}
const std::string getContent() const;
operator xmlNode*() const
inline operator xmlNode*() const
{
//ulog.< "current\n";
return curNode;
......@@ -203,6 +225,9 @@ public:
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;
// ??
//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* 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 )
if( oind == NULL )
{
UniXML_iterator it = unixml.findNode(unixml.getFirstNode(),"ObjectsMap");
if( it == NULL )
if( !it )
{
ucrit << "(Configuration:init): not found <ObjectsMap> node in " << fileConfName << endl;
throw SystemError("(Configuration:init): not found <ObjectsMap> node in " + fileConfName );
......
......@@ -472,88 +472,58 @@ bool UniXML_iterator::find( const std::string& searchnode )
return false;
}
// -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator++()
UniXML_iterator UniXML_iterator::operator++(int)
{
if (!curNode->next)
{
curNode=curNode->next;
return *this;
}
for(;;)
{
curNode=curNode->next;
if (getName() == "text" || getName() == "comment")
continue;
else
break;
}
return *this;
return (*this)+1;
}
UniXML_iterator UniXML_iterator::operator++(int)
// -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator+=(int s)
{
UniXML_iterator it = *this;
if (!curNode->next)
{
curNode=curNode->next;
return it;
}
for(;;)
{
curNode=curNode->next;
if (getName() == "text" || getName() == "comment")
continue;
else
break;
}
return it;
return (*this)+s;
}
// -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator--()
UniXML_iterator UniXML_iterator::operator+(int step)
{
if (!curNode->prev)
int i = 0;
while( i<step && curNode )
{
curNode=curNode->prev;
return *this;
}
for(;;)
curNode = curNode->next;
if( curNode )
{
curNode=curNode->prev;
if (getName() == "text" || getName() == "comment")
if( getName() == "text" || getName() == "comment" )
continue;
else
break;
i++;
}
}
return *this;
}
// -------------------------------------------------------------------------
UniXML_iterator UniXML_iterator::operator--(int)
{
UniXML_iterator it = *this;
return (*this)-1;
}
UniXML_iterator UniXML_iterator::operator-=(int s)
{
return (*this)-s;
}
// -------------------------------------------------------------------------
if (!curNode->prev)
UniXML_iterator UniXML_iterator::operator-(int step)
{
int i=0;
while( i<step && curNode )
{
curNode=curNode->prev;
return it;
}
for(;;)
curNode = curNode->prev;
if( curNode )
{
curNode=curNode->prev;
if (getName() == "text" || getName() == "comment")
if( getName() == "text" || getName() == "comment" )
continue;
else
break;
i++;
}
}
return it;
return (*this);
}
// -------------------------------------------------------------------------------
......@@ -5,7 +5,7 @@
noinst_PROGRAMS = tests tests_with_conf
#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_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_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
#
# 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>
using namespace std;
......@@ -6,67 +8,95 @@ using namespace std;
#include "UniXML.h"
#include "UniSetTypes.h"
int main()
TEST_CASE("UniXML", "[UniXML]" )
{
/* try
SECTION( "Default constructor" )
{
*/
UniXML xml("test.xml");
UniXML xml;
CHECK_FALSE( xml.isOpen() );
}
xmlNode* cnode = xml.findNode(xml.getFirstNode(),"UniSet");
if( cnode == NULL )
SECTION( "Bad file" )
{
cerr << "<testnode> not found" << endl;
return 1;
REQUIRE_THROWS_AS( UniXML("unknown.xml"), UniSetTypes::NameNotFound );
REQUIRE_THROWS_AS( UniXML("tests_unixml_badfile.xml"), UniSetTypes::Exception );
}
UniXML_iterator it(cnode);
cout << "string id=" << it.getProp("id")
<< " int id=" << it.getIntProp("id")
<< endl;
UniXML uxml("tests_unixml.xml");
CHECK( uxml.isOpen() );
{
UniXML_iterator it = xml.findNode(xml.getFirstNode(),"LocalInfoServer", "InfoServer");
if( it == NULL )
{
cerr << "<testnode> not found" << endl;
return 1;
}
xmlNode* cnode = uxml.findNode(uxml.getFirstNode(),"UniSet");
CHECK( cnode != NULL );
// проверка поиска "вглубь"
CHECK( uxml.findNode(uxml.getFirstNode(),"Services") != NULL );
cout << "string id=" << it.getProp("dbrepeat")
<< " int id=" << it.getIntProp("dbrepeat")
<< endl;
}
// getProp
// getIntProp
// nextNode
// getPIntProp
// create
// remove
// copy
// nextNode
SECTION( "Iterator" );
{
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");
}
UniXML_iterator it(cnode);
CHECK( it.getCurrent() != 0 );
it = uxml.begin();
CHECK( it.find("UniSet") != 0 );
xml.save("test.out.xml");
/*
}
catch( UniSetTypes::Exception& ex )
{
cerr << ex << endl;
return 1;
}
catch( ... )
{
cerr << "catch ... " << endl;
return 1;
// поиск в глубину..
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 );
}
*/
return 0;
}
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