00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "judo.hpp"
00030 #include "judo_test.hpp"
00031 using namespace judo;
00032
00033 #include <iostream>
00034 using namespace std;
00035
00036 Test* ElementStreamTest::getTestSuite()
00037 {
00038 TestSuite* s = new TestSuite();
00039 s->addTest(new TestCaller<ElementStreamTest>("testing construction",
00040 &ElementStreamTest::construct));
00041 s->addTest(new TestCaller<ElementStreamTest>("push",
00042 &ElementStreamTest::push));
00043 s->addTest(new TestCaller<ElementStreamTest>("parseAtOnce",
00044 &ElementStreamTest::parseAtOnce));
00045 return s;
00046 }
00047
00048 list<Element*> G_results;
00049
00050 class ElementStreamTestImpl
00051 : public ElementStreamEventListener
00052 {
00053 public:
00054 ElementStreamTestImpl()
00055 : _stream(this)
00056 {}
00057
00058 void onDocumentStart(Element* t)
00059 {
00060 G_results.push_back(t);
00061 }
00062 void onElement(Element* t)
00063 {
00064 G_results.push_back(t);
00065 }
00066 void onDocumentEnd()
00067 {
00068 for_each(G_results.begin(), G_results.end(), _releaseElement);
00069 G_results.clear();
00070 }
00071 ElementStream _stream;
00072 private:
00073 static void _releaseElement(Element* t)
00074 {
00075 delete t;
00076 }
00077
00078 };
00079
00080 void ElementStreamTest::construct()
00081 {
00082 ElementStreamTestImpl es;
00083
00084 Assert(es._stream._document_started == false);
00085 Assert(es._stream._document_ended == false);
00086 }
00087
00088 void ElementStreamTest::push()
00089 {
00090 Assert(G_results.empty());
00091
00092 ElementStreamTestImpl es;
00093
00094 try
00095 {
00096
00097 es._stream.push("<root>", 6);
00098 Assert(es._stream._document_started == true);
00099 Assert(G_results.size() == 1);
00100 Assert(G_results.back()->toString() == "<root/>");
00101
00102
00103 es._stream.push("<message to='dizzy@j.org'><body>Hello", 37);
00104 Assert(es._stream._element_stack.size() == 2);
00105 Assert(G_results.size() == 1);
00106
00107
00108 es._stream.push("</body></message>", 17);
00109 Assert(es._stream._element_stack.size() == 0);
00110 Assert(G_results.size() == 2);
00111 Assert(G_results.back()->toString() == "<message to='dizzy@j.org'><body>Hello</body></message>");
00112
00113
00114 es._stream.push("<presence/>", 11);
00115 Assert(es._stream._element_stack.size() == 0);
00116 Assert(G_results.size() == 3);
00117 Assert(G_results.back()->toString() == "<presence/>");
00118
00119
00120 es._stream.push("</root>", 7);
00121 Assert(es._stream._document_ended == true);
00122 Assert(G_results.empty());
00123 } catch (ElementStream::exception::ParserError& e)
00124 {
00125 cerr << "Parser error: " << e.getMessage() << endl;
00126 throw;
00127 }
00128 }
00129
00130 void ElementStreamTest::parseAtOnce()
00131 {
00132
00133 const char* data1 = "<root>foobar<tag1/>somedata</root>";
00134 Element* e = ElementStream::parseAtOnce(data1);
00135 Assert(e->toString() == "<root>foobar<tag1/>somedata</root>");
00136 delete e;
00137
00138
00139 bool successful2 = false;
00140 const char* data2 = "<root><tag1>foadsf</tag1><tag2>";
00141 try
00142 {
00143 e = ElementStream::parseAtOnce(data2);
00144 } catch (ElementStream::exception::IncompleteParse& parse_ex) {
00145 successful2 = true;
00146 }
00147 Assert(successful2 == true);
00148
00149
00150 bool successful3 = false;
00151 const char* data3 = "<root></tag1></root>";
00152 try
00153 {
00154 e = ElementStream::parseAtOnce(data3);
00155 } catch (ElementStream::exception::ParserError& parse_ex) {
00156 successful3 = true;
00157 }
00158 Assert(successful3 == true);
00159
00160 }