Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

ElementTest.cpp

00001 //============================================================================
00002 // Project:       Jabber Universal Document Objects (Judo)
00003 // Filename:      ElementTest.cpp
00004 // Description:   judo::Element unit tests
00005 // Created at:    Tue Jul  3 13:06:11 2001
00006 // Modified at:   Thu Aug 16 18:01:23 2001
00007 //
00008 //   License:
00009 // 
00010 // The contents of this file are subject to the Jabber Open Source License
00011 // Version 1.0 (the "License").  You may not copy or use this file, in either
00012 // source code or executable form, except in compliance with the License.  You
00013 // may obtain a copy of the License at http://www.jabber.com/license/ or at
00014 // http://www.opensource.org/.  
00015 //
00016 // Software distributed under the License is distributed on an "AS IS" basis,
00017 // WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
00018 // for the specific language governing rights and limitations under the
00019 // License.
00020 //
00021 //   Copyrights
00022 //
00023 // Portions created by or assigned to Jabber.com, Inc. are 
00024 // Copyright (c) 1999-2001 Jabber.com, Inc.  All Rights Reserved.  
00025 // 
00026 // $Id: ElementTest.cpp,v 1.2 2002/07/13 19:30:22 temas Exp $
00027 //============================================================================
00028 
00029 #include "judo.hpp"
00030 #include "judo_test.hpp"
00031 using namespace judo;
00032 using namespace std;
00033 
00034 Test* ElementTest::getTestSuite()
00035 {
00036     TestSuite* s = new TestSuite();
00037     s->addTest(new TestCaller<ElementTest>("testing construction",
00038                                        &ElementTest::construct));
00039     s->addTest(new TestCaller<ElementTest>("testing addElement",
00040                                        &ElementTest::addElement));
00041     s->addTest(new TestCaller<ElementTest>("testing addCDATA",
00042                                        &ElementTest::addCDATA));
00043     s->addTest(new TestCaller<ElementTest>("testing putAttrib",
00044                                        &ElementTest::putAttrib));
00045     s->addTest(new TestCaller<ElementTest>("testing getAttrib",
00046                                        &ElementTest::getAttrib));
00047     s->addTest(new TestCaller<ElementTest>("testing delAttrib",
00048                                        &ElementTest::delAttrib));
00049     s->addTest(new TestCaller<ElementTest>("testing cmpAttrib",
00050                                        &ElementTest::cmpAttrib));
00051     s->addTest(new TestCaller<ElementTest>("testing toString",
00052                                        &ElementTest::ElementtoString));
00053     s->addTest(new TestCaller<ElementTest>("testing toStringEx",
00054                                        &ElementTest::ElementtoStringEx));
00055     s->addTest(new TestCaller<ElementTest>("testing getCDATA",
00056                                        &ElementTest::getCDATA));
00057     s->addTest(new TestCaller<ElementTest>("testing findElement",
00058                                        &ElementTest::findElement));
00059     s->addTest(new TestCaller<ElementTest>("testing detachChild",
00060                                        &ElementTest::detachChild));
00061     return s;
00062 }
00063 
00064 void ElementTest::construct()
00065 {
00066     Element e("message");
00067 
00068     Assert(e.getName() == "message");
00069     Assert(e.getType() == Node::ntElement);
00070 
00071     {
00072         // Test copy constructor
00073         Element e1("message");
00074         e.putAttrib("to", "dizzyd");
00075         e.addElement("body", "Hello, dizzyd");
00076         e.addElement("subject", "Yo buddy");
00077 
00078         Element e2(e1);
00079         Assert(e1.toString() == e2.toString());
00080     }
00081 }
00082 
00083 void ElementTest::addElement()
00084 {
00085     {
00086         Element e("message");
00087         
00088         Element* e1 = e.addElement("body");
00089         
00090         Assert(e._children.size() == 1);
00091         Assert(e._children.front() == (Element*)e1);
00092         Assert(e1->getType() == Node::ntElement);
00093         Assert(e1->getName() == "body");
00094     }
00095     // Test overloaded version (formerly addTaggedCDATA)
00096     {
00097         Element e("message");
00098         
00099         Element* e1 = e.addElement("body", "hello, world!");
00100         
00101         Assert(e._children.size() == 1);
00102         
00103         Assert(e._children.front() == (Element*)e1);
00104         
00105         Assert(e1->getType() == Node::ntElement);
00106         Assert(e1->getName() == "body");
00107         
00108         Assert(e1->_children.size() == 1);
00109         Assert(e1->_children.front()->getType() == Node::ntCDATA);
00110         Assert(e1->_children.front()->getName() == "#CDATA");
00111     }
00112 }
00113 
00114 void ElementTest::addCDATA()
00115 {
00116     Element e("message");
00117 
00118     CDATA* c = e.addCDATA("hello, world!", strlen("hello, world!"));
00119 
00120     Assert(e._children.size() == 1);
00121 
00122     Assert(e._children.front() == (Element*)c);
00123     Assert(c->getType() == Node::ntCDATA);
00124     Assert(c->getName() == "#CDATA");
00125 }
00126 
00127 void ElementTest::putAttrib()
00128 {
00129     Element e("message");
00130     e.putAttrib("name", "value");
00131 
00132     Assert(e._attribs.size() == 1);
00133 
00134     map<string,string>::iterator it = e._attribs.find("name");
00135     Assert(it != e._attribs.end());
00136     Assert(it->first  == "name");
00137     Assert(it->second == "value");
00138 }
00139 
00140 void ElementTest::getAttrib()
00141 {
00142     Element e("message");
00143 
00144     e._attribs["name1"] = "value1";
00145     e._attribs["name2"] = "value2";
00146     e._attribs["name3"] = "value3";
00147 
00148     string result = e.getAttrib("name2");
00149     Assert(result == "value2");
00150 }
00151 
00152 void ElementTest::delAttrib()
00153 {
00154     Element e("message");
00155 
00156     e._attribs["name1"] = "value1";
00157     e._attribs["name2"] = "value2";
00158     e._attribs["name3"] = "value3";
00159 
00160     e.delAttrib("name2");
00161 
00162     Assert(e._attribs.find("name2") == e._attribs.end());
00163 }
00164 
00165 void ElementTest::cmpAttrib()
00166 {
00167     Element e("message");
00168 
00169     e._attribs["name1"] = "value1";
00170     e._attribs["name2"] = "value2";
00171     e._attribs["name3"] = "value3";
00172 
00173     Assert(e.cmpAttrib("name3", "value3"));
00174 }
00175 void ElementTest::ElementtoString()
00176 {
00177     Element e("message");
00178     e.putAttrib("name1", "value1");
00179     e.putAttrib("name3", "value3");
00180     e.addElement("body", "Hullo, world!");
00181 
00182     Assert(e.toString() == "<message name1='value1' name3='value3'><body>Hullo, world!</body></message>");
00183 }
00184 
00185 void ElementTest::ElementtoStringEx()
00186 {
00187     Element e("message");
00188     e.putAttrib("name1", "value1");
00189     e.putAttrib("name3", "value&&3");
00190     e.addElement("body", "Hullo, world!");
00191 
00192     Assert(e.toStringEx() == "<message name1='value1' name3='value&amp;&amp;3'>");
00193 }
00194 
00195 void ElementTest::getCDATA()
00196 {
00197     Element e("message");
00198     Element* e1 = e.addElement("body", "Hullo, world");
00199 
00200     Assert(e1->getCDATA() == "Hullo, world");
00201 }
00202 
00203 void ElementTest::findElement()
00204 {
00205     Element e("message");
00206     e.putAttrib("name1", "value1");
00207     Element *f = e.addElement("body", "this is a test");
00208     Element *g = e.addElement("subject", "greetings");
00209     e.addElement("body", "howdy");
00210     Assert (e.findElement("body") == f);
00211     Assert (e.findElement("subject") == g);
00212     Assert (e.findElement("topic") == NULL); 
00213 };
00214 
00215 void ElementTest::detachChild()
00216 {
00217    Element* e = ElementStream::parseAtOnce("<foo><bar>baz</bar>fiz</foo>");
00218    Element::iterator it = e->begin();
00219    Node* bar = e->detachChild(it);
00220    Assert (bar);
00221    Assert (bar->getName() == "bar");
00222    Assert (bar->getType() == Node::ntElement);
00223    Assert (((Element*)bar)->getCDATA() == "baz");
00224    it = e->begin();
00225    Assert ((*it)->getType() == Node::ntCDATA);
00226    Assert (((CDATA*)*it)->getText() == "fiz");
00227    delete bar;
00228    delete e;
00229 }

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3