closes #1
This commit is contained in:
@@ -7,12 +7,15 @@ AM_CXXFLAGS += -I ${top_srcdir}/src
|
||||
AM_LDFLAGS = -L${top_builddir}/src
|
||||
LDADD = -lcppunit -lxml-cxx
|
||||
|
||||
check_PROGRAMS = xml_test serialization_test container_serialization_test
|
||||
check_PROGRAMS = xml_test serialization_test \
|
||||
container_serialization_test optional_serialization_test
|
||||
|
||||
TESTS=${check_PROGRAMS}
|
||||
|
||||
xml_test_SOURCES = xml_test.cxx
|
||||
serialization_test_SOURCES = serialization_test.cxx
|
||||
container_serialization_test_SOURCES = container_serialization_test.cxx
|
||||
optional_serialization_test_SOURCES = optional_serialization_test.cxx
|
||||
|
||||
CLEANFILES =
|
||||
MAINTAINERCLEANFILES = makefile.in
|
||||
|
118
test/optional_serialization_test.cxx
Normal file
118
test/optional_serialization_test.cxx
Normal file
@@ -0,0 +1,118 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id: serialization_test.cxx 31 2009-04-28 07:36:07Z $
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <xml-cxx/xml.hxx>
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/ui/text/TestRunner.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/extensions/TestFactoryRegistry.h>
|
||||
#include <cppunit/XmlOutputter.h>
|
||||
#include <fstream>
|
||||
|
||||
class A: public xml::Serialize {
|
||||
public:
|
||||
xml::Optional<int> _int;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("A");
|
||||
persist(_int, "int");
|
||||
}
|
||||
};
|
||||
|
||||
class B: public xml::Serialize {
|
||||
public:
|
||||
xml::Optional<long> _long;
|
||||
xml::Optional<short> _short;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("B");
|
||||
persist(_long, "long");
|
||||
persist(_short, "short");
|
||||
}
|
||||
};
|
||||
|
||||
class B1: public B {
|
||||
public:
|
||||
xml::Optional<A> _a1;
|
||||
xml::Optional<A> _a2;
|
||||
xml::Optional<A> _a3;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
B::initXmlMembers();
|
||||
className("B1");
|
||||
persist(_a1, "a1");
|
||||
persist(_a2, "a2");
|
||||
persist(_a3, "a3");
|
||||
}
|
||||
};
|
||||
|
||||
class OptionalSerializationTest: public CppUnit::TestFixture {
|
||||
public:
|
||||
void checkOptional() {
|
||||
std::string with("<A>\n"
|
||||
"\t<int>13</int>\n"
|
||||
"</A>");
|
||||
std::string without("<A/>");
|
||||
A a;
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("<A>\n"
|
||||
"\t<int/>\n"
|
||||
"</A>"), a.schema());
|
||||
std::stringstream ss1a(with);
|
||||
CPPUNIT_ASSERT_NO_THROW(a.loadXml(ss1a));
|
||||
CPPUNIT_ASSERT_EQUAL(true, (bool)a._int);
|
||||
CPPUNIT_ASSERT_EQUAL(13, *a._int);
|
||||
std::stringstream ss1b;
|
||||
CPPUNIT_ASSERT_NO_THROW(a.saveXml(ss1b));
|
||||
CPPUNIT_ASSERT_EQUAL(with, ss1b.str());
|
||||
std::stringstream ss2a(without);
|
||||
CPPUNIT_ASSERT_NO_THROW(a.loadXml(ss2a));
|
||||
CPPUNIT_ASSERT_EQUAL(false, (bool)a._int);
|
||||
std::stringstream ss2b;
|
||||
CPPUNIT_ASSERT_NO_THROW(a.saveXml(ss2b));
|
||||
CPPUNIT_ASSERT_EQUAL(without, ss2b.str());
|
||||
}
|
||||
void checkMoreOptional() {
|
||||
std::string first("<B1>\n"
|
||||
"\t<short>13</short>\n"
|
||||
"\t<a2/>\n"
|
||||
"\t<a3>\n"
|
||||
"\t\t<int>42</int>\n"
|
||||
"\t</a3>\n"
|
||||
"</B1>");
|
||||
B1 b;
|
||||
std::stringstream ifirst(first);
|
||||
CPPUNIT_ASSERT_NO_THROW(b.loadXml(ifirst));
|
||||
CPPUNIT_ASSERT_EQUAL(false, (bool)b._long);
|
||||
CPPUNIT_ASSERT_EQUAL(true, (bool)b._short);
|
||||
CPPUNIT_ASSERT_EQUAL((short)13, *b._short);
|
||||
CPPUNIT_ASSERT_EQUAL(false, (bool)b._a1);
|
||||
CPPUNIT_ASSERT_EQUAL(true, (bool)b._a2);
|
||||
CPPUNIT_ASSERT_EQUAL(false, (bool)b._a2->_int);
|
||||
CPPUNIT_ASSERT_EQUAL(true, (bool)b._a3);
|
||||
CPPUNIT_ASSERT_EQUAL(true, (bool)b._a3->_int);
|
||||
CPPUNIT_ASSERT_EQUAL(42, *b._a3->_int);
|
||||
std::stringstream ofirst;
|
||||
CPPUNIT_ASSERT_NO_THROW(b.saveXml(ofirst));
|
||||
CPPUNIT_ASSERT_EQUAL(first, ofirst.str());
|
||||
}
|
||||
CPPUNIT_TEST_SUITE(OptionalSerializationTest);
|
||||
CPPUNIT_TEST(checkOptional);
|
||||
CPPUNIT_TEST(checkMoreOptional);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
};
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(OptionalSerializationTest);
|
||||
|
||||
int main(int argc, char** argv) try {
|
||||
std::ofstream ofs((*argv+std::string(".xml")).c_str());
|
||||
CppUnit::TextUi::TestRunner runner;
|
||||
//runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), ofs));
|
||||
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
|
||||
return runner.run() ? 0 : 1;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr<<"***Exception: "<<e.what()<<std::endl;
|
||||
return 1;
|
||||
}
|
Reference in New Issue
Block a user