📄 nodetest.cpp
字号:
//// NodeTest.cpp//// $Id: //poco/1.2/XML/testsuite/src/NodeTest.cpp#1 $//// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.// and Contributors.//// Permission is hereby granted, free of charge, to any person or organization// obtaining a copy of the software and accompanying documentation covered by// this license (the "Software") to use, reproduce, display, distribute,// execute, and transmit the Software, and to prepare derivative works of the// Software, and to permit third-parties to whom the Software is furnished to// do so, all subject to the following:// // The copyright notices in the Software and this entire statement, including// the above license grant, this restriction and the following disclaimer,// must be included in all copies of the Software, in whole or in part, and// all derivative works of the Software, unless such copies or derivative// works are solely in the form of machine-executable object code generated by// a source language processor.// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER// DEALINGS IN THE SOFTWARE.//#include "NodeTest.h"#include "CppUnit/TestCaller.h"#include "CppUnit/TestSuite.h"#include "Poco/DOM/Document.h"#include "Poco/DOM/Element.h"#include "Poco/DOM/DocumentFragment.h"#include "Poco/DOM/AutoPtr.h"using Poco::XML::Element;using Poco::XML::Document;using Poco::XML::DocumentFragment;using Poco::XML::Node;using Poco::XML::AutoPtr;NodeTest::NodeTest(const std::string& name): CppUnit::TestCase(name){}NodeTest::~NodeTest(){}void NodeTest::testInsert(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pRoot = pDoc->createElement("root"); assert (!pRoot->hasChildNodes()); assert (pRoot->firstChild() == 0); assert (pRoot->lastChild() == 0); AutoPtr<Element> pChild1 = pDoc->createElement("child1"); pRoot->insertBefore(pChild1, 0); assert (pRoot->hasChildNodes()); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild1); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == 0); AutoPtr<Element> pChild3 = pDoc->createElement("child3"); pRoot->insertBefore(pChild3, 0); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild3); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild1); assert (pChild3->nextSibling() == 0); AutoPtr<Element> pChild0 = pDoc->createElement("child0"); pRoot->insertBefore(pChild0, pChild1); assert (pRoot->firstChild() == pChild0); assert (pRoot->lastChild() == pChild3); assert (pChild0->previousSibling() == 0); assert (pChild0->nextSibling() == pChild1); assert (pChild1->previousSibling() == pChild0); assert (pChild1->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild1); assert (pChild3->nextSibling() == 0); AutoPtr<Element> pChild2 = pDoc->createElement("child2"); pRoot->insertBefore(pChild2, pChild3); assert (pRoot->firstChild() == pChild0); assert (pRoot->lastChild() == pChild3); assert (pChild0->previousSibling() == 0); assert (pChild0->nextSibling() == pChild1); assert (pChild1->previousSibling() == pChild0); assert (pChild1->nextSibling() == pChild2); assert (pChild2->previousSibling() == pChild1); assert (pChild2->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild2); assert (pChild3->nextSibling() == 0);}void NodeTest::testAppend(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pRoot = pDoc->createElement("root"); AutoPtr<Element> pChild1 = pDoc->createElement("child1"); pRoot->appendChild(pChild1); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild1); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == 0); AutoPtr<Element> pChild2 = pDoc->createElement("child2"); pRoot->appendChild(pChild2); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild2); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == pChild2); assert (pChild2->previousSibling() == pChild1); assert (pChild2->nextSibling() == 0); AutoPtr<Element> pChild3 = pDoc->createElement("child3"); pRoot->appendChild(pChild3); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild3); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == pChild2); assert (pChild2->previousSibling() == pChild1); assert (pChild2->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild2); assert (pChild3->nextSibling() == 0);}void NodeTest::testRemove(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pRoot = pDoc->createElement("root"); AutoPtr<Element> pChild1 = pDoc->createElement("child1"); pRoot->appendChild(pChild1); AutoPtr<Element> pChild2 = pDoc->createElement("child2"); pRoot->appendChild(pChild2); AutoPtr<Element> pChild3 = pDoc->createElement("child3"); pRoot->appendChild(pChild3); AutoPtr<Element> pChild4 = pDoc->createElement("child4"); pRoot->appendChild(pChild4); pRoot->removeChild(pChild2); assert (pChild2->previousSibling() == 0); assert (pChild2->nextSibling() == 0); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild1); assert (pChild3->nextSibling() == pChild4); assert (pChild4->previousSibling() == pChild3); assert (pChild4->nextSibling() == 0); pRoot->removeChild(pChild4); assert (pChild4->previousSibling() == 0); assert (pChild4->nextSibling() == 0); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild1); assert (pChild3->nextSibling() == 0); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild3); pRoot->removeChild(pChild1); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == 0); assert (pRoot->firstChild() == pChild3); assert (pRoot->lastChild() == pChild3); pRoot->removeChild(pChild3); assert (pChild3->previousSibling() == 0); assert (pChild3->nextSibling() == 0); assert (pRoot->firstChild() == 0); assert (pRoot->lastChild() == 0);}void NodeTest::testReplace(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pRoot = pDoc->createElement("root"); AutoPtr<Element> pChild1 = pDoc->createElement("child1"); pRoot->appendChild(pChild1); AutoPtr<Element> pChild2 = pDoc->createElement("child2"); pRoot->appendChild(pChild2); AutoPtr<Element> pChild3 = pDoc->createElement("child3"); pRoot->appendChild(pChild3); AutoPtr<Element> pChild4 = pDoc->createElement("child4"); pRoot->appendChild(pChild4); AutoPtr<Element> pChild11 = pDoc->createElement("child11"); pRoot->replaceChild(pChild11, pChild1); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == 0); assert (pRoot->firstChild() == pChild11); assert (pRoot->lastChild() == pChild4); assert (pChild11->previousSibling() == 0); assert (pChild11->nextSibling() == pChild2); assert (pChild2->previousSibling() == pChild11); assert (pChild2->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild2); assert (pChild3->nextSibling() == pChild4); assert (pChild4->previousSibling() == pChild3); assert (pChild4->nextSibling() == 0); AutoPtr<Element> pChild22 = pDoc->createElement("child22"); pRoot->replaceChild(pChild22, pChild2); assert (pChild2->previousSibling() == 0); assert (pChild2->nextSibling() == 0); assert (pRoot->firstChild() == pChild11); assert (pRoot->lastChild() == pChild4); assert (pChild11->previousSibling() == 0); assert (pChild11->nextSibling() == pChild22); assert (pChild22->previousSibling() == pChild11); assert (pChild22->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild22); assert (pChild3->nextSibling() == pChild4); assert (pChild4->previousSibling() == pChild3); assert (pChild4->nextSibling() == 0); AutoPtr<Element> pChild33 = pDoc->createElement("child33"); pRoot->replaceChild(pChild33, pChild3); assert (pChild3->previousSibling() == 0); assert (pChild3->nextSibling() == 0); assert (pRoot->firstChild() == pChild11); assert (pRoot->lastChild() == pChild4); assert (pChild11->previousSibling() == 0); assert (pChild11->nextSibling() == pChild22); assert (pChild22->previousSibling() == pChild11); assert (pChild22->nextSibling() == pChild33); assert (pChild33->previousSibling() == pChild22); assert (pChild33->nextSibling() == pChild4); assert (pChild4->previousSibling() == pChild33); assert (pChild4->nextSibling() == 0); AutoPtr<Element> pChild44 = pDoc->createElement("child44"); pRoot->replaceChild(pChild44, pChild4); assert (pChild4->previousSibling() == 0); assert (pChild4->nextSibling() == 0); assert (pRoot->firstChild() == pChild11); assert (pRoot->lastChild() == pChild44); assert (pChild11->previousSibling() == 0); assert (pChild11->nextSibling() == pChild22); assert (pChild22->previousSibling() == pChild11); assert (pChild22->nextSibling() == pChild33); assert (pChild33->previousSibling() == pChild22); assert (pChild33->nextSibling() == pChild44); assert (pChild44->previousSibling() == pChild33); assert (pChild44->nextSibling() == 0);}void NodeTest::testInsertFragment1(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pRoot = pDoc->createElement("root"); AutoPtr<DocumentFragment> pFrag = pDoc->createDocumentFragment(); assert (!pRoot->hasChildNodes()); assert (pRoot->firstChild() == 0); assert (pRoot->lastChild() == 0); AutoPtr<Element> pChild1 = pDoc->createElement("child1"); pFrag->appendChild(pChild1); pRoot->insertBefore(pFrag, 0); assert (pFrag->firstChild() == 0); assert (pFrag->lastChild() == 0); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild1); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == 0); AutoPtr<Element> pChild3 = pDoc->createElement("child3"); pFrag->appendChild(pChild3); pRoot->insertBefore(pFrag, 0); assert (pRoot->firstChild() == pChild1); assert (pRoot->lastChild() == pChild3); assert (pChild1->previousSibling() == 0); assert (pChild1->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild1); assert (pChild3->nextSibling() == 0); AutoPtr<Element> pChild0 = pDoc->createElement("child0"); pFrag->appendChild(pChild0); pRoot->insertBefore(pFrag, pChild1); assert (pRoot->firstChild() == pChild0); assert (pRoot->lastChild() == pChild3); assert (pChild0->previousSibling() == 0); assert (pChild0->nextSibling() == pChild1); assert (pChild1->previousSibling() == pChild0); assert (pChild1->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild1); assert (pChild3->nextSibling() == 0); AutoPtr<Element> pChild2 = pDoc->createElement("child2"); pFrag->appendChild(pChild2); pRoot->insertBefore(pFrag, pChild3); assert (pRoot->firstChild() == pChild0); assert (pRoot->lastChild() == pChild3); assert (pChild0->previousSibling() == 0); assert (pChild0->nextSibling() == pChild1); assert (pChild1->previousSibling() == pChild0); assert (pChild1->nextSibling() == pChild2); assert (pChild2->previousSibling() == pChild1); assert (pChild2->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild2); assert (pChild3->nextSibling() == 0);}void NodeTest::testInsertFragment2(){ AutoPtr<Document> pDoc = new Document; AutoPtr<Element> pRoot = pDoc->createElement("root"); AutoPtr<DocumentFragment> pFrag = pDoc->createDocumentFragment(); assert (!pRoot->hasChildNodes()); assert (pRoot->firstChild() == 0); assert (pRoot->lastChild() == 0); AutoPtr<Element> pChild2 = pDoc->createElement("child2"); AutoPtr<Element> pChild3 = pDoc->createElement("child3"); pFrag->appendChild(pChild2); pFrag->appendChild(pChild3); pRoot->insertBefore(pFrag, 0); assert (pFrag->firstChild() == 0); assert (pFrag->lastChild() == 0); assert (pRoot->firstChild() == pChild2); assert (pRoot->lastChild() == pChild3); assert (pChild2->previousSibling() == 0); assert (pChild2->nextSibling() == pChild3); assert (pChild3->previousSibling() == pChild2); assert (pChild3->nextSibling() == 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -