Commit 960003ca authored by jan.koester's avatar jan.koester
Browse files

tests

parent 9e83495c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -8,6 +8,11 @@ target_link_libraries(htmlcsstest htmlpp)

add_test(htmlcsstest htmlcsstest)

add_executable(htmlparenttest htmlparenttest.cpp)
target_link_libraries(htmlparenttest htmlpp)

add_test(htmlparenttest htmlparenttest)

add_executable(htmlpagetest htmlpagetest.cpp)
target_link_libraries(htmlpagetest htmlpp-static)

+171 −0
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2021, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <iostream>

#include "../src/html.h"

#define Red     "\033[0;31m"
#define Green   "\033[0;32m"
#define NOCOLOR "\033[0m"

static int testCount = 0;
static int passCount = 0;

static void check(bool cond, const char *desc) {
    ++testCount;
    if (cond) {
        ++passCount;
        std::cout << Green << "  PASS: " << desc << NOCOLOR << std::endl;
    } else {
        std::cout << Red   << "  FAIL: " << desc << NOCOLOR << std::endl;
    }
}

int main(){
    // --- parentElement() after parsing ---
    std::cout << "=== parentElement() after parse ===" << std::endl;
    {
        std::string html = "<div id=\"outer\"><p id=\"inner\">hi</p></div>";
        libhtmlpp::HtmlString htmlString(html);
        libhtmlpp::Element &root = htmlString.parse();

        libhtmlpp::HtmlElement *outer =
            static_cast<libhtmlpp::HtmlElement*>(&root)->getElementbyID("outer");
        libhtmlpp::HtmlElement *inner =
            static_cast<libhtmlpp::HtmlElement*>(&root)->getElementbyID("inner");
        check(outer != nullptr && inner != nullptr, "found both elements by id");
        if (outer && inner) {
            check(inner->parentElement() == outer, "inner element's parent is the outer div");
            check(outer->parentElement() != outer, "outer element's parent is not itself");
        }
    }

    // --- appendChild / insertChild set parentElement() ---
    std::cout << "=== appendChild / insertChild parent ===" << std::endl;
    {
        libhtmlpp::HtmlElement div("div");
        libhtmlpp::HtmlElement span("span");
        div.appendChild(&span);
        libhtmlpp::Element *child = div.firstChild();
        check(child != nullptr && child->parentElement() == &div,
              "appendChild's new node has the container as its parent");
    }
    {
        libhtmlpp::HtmlElement div("div");
        libhtmlpp::HtmlElement span("span");
        div.insertChild(&span);
        libhtmlpp::Element *child = div.firstChild();
        check(child != nullptr && child->parentElement() == &div,
              "insertChild's new node has the container as its parent");
    }

    // --- copy re-parents into the NEW tree, not the original ---
    std::cout << "=== copy re-parents subtree ===" << std::endl;
    {
        std::string html = "<div id=\"outer\"><p id=\"inner\">hi</p></div>";
        libhtmlpp::HtmlString htmlString(html);
        libhtmlpp::Element &root = htmlString.parse();
        libhtmlpp::HtmlElement *origOuter =
            static_cast<libhtmlpp::HtmlElement*>(&root)->getElementbyID("outer");

        libhtmlpp::HtmlElement copyRoot;
        copyRoot = *static_cast<libhtmlpp::HtmlElement*>(&root);

        libhtmlpp::HtmlElement *copiedInner = copyRoot.getElementbyID("inner");
        libhtmlpp::HtmlElement *copiedOuter = copyRoot.getElementbyID("outer");
        check(copiedInner != nullptr && copiedOuter != nullptr,
              "found both elements by id in the copied tree");
        if (copiedInner && copiedOuter && origOuter) {
            check(copiedInner->parentElement() == copiedOuter,
                  "copied inner element's parent is the COPIED outer div");
            check(copiedInner->parentElement() != origOuter,
                  "copied inner element's parent is NOT the original outer div");
        }
    }

    // --- remove(): first-child case (the original bug) ---
    std::cout << "=== remove() first child ===" << std::endl;
    {
        std::string html = "<ul><li id=\"a\">1</li><li id=\"b\">2</li><li id=\"c\">3</li></ul>";
        libhtmlpp::HtmlString htmlString(html);
        libhtmlpp::Element &root = htmlString.parse();
        libhtmlpp::HtmlElement *ul =
            static_cast<libhtmlpp::HtmlElement*>(&root)->getElementbyTag("ul");
        check(ul != nullptr, "found <ul>");
        if (ul) {
            libhtmlpp::Element *first = ul->firstChild();
            ul->remove(first);

            libhtmlpp::Element *newFirst = ul->firstChild();
            check(newFirst != nullptr, "ul still has a first child after removing the old one");
            if (newFirst) {
                libhtmlpp::HtmlElement *newFirstHel = static_cast<libhtmlpp::HtmlElement*>(newFirst);
                check(newFirstHel->getAtributte("id") == "b",
                      "ul's first child is now \"b\" (the old first child's next sibling)");
                libhtmlpp::Element *third = newFirst->nextElement();
                check(third != nullptr &&
                      static_cast<libhtmlpp::HtmlElement*>(third)->getAtributte("id") == "c",
                      "the rest of the sibling chain (\"c\") survived (the original bug lost it)");
            }
            check(first->parentElement() == nullptr && first->nextElement() == nullptr,
                  "the removed node's own parent/next links are cleared");
        }
    }

    // --- remove(): a node reached by normal sibling advancement (the crash case) ---
    std::cout << "=== remove() non-first child (previously crashed) ===" << std::endl;
    {
        std::string html = "<ul><li id=\"a\">1</li><li id=\"b\">2</li><li id=\"c\">3</li></ul>";
        libhtmlpp::HtmlString htmlString(html);
        libhtmlpp::Element &root = htmlString.parse();
        libhtmlpp::HtmlElement *ul =
            static_cast<libhtmlpp::HtmlElement*>(&root)->getElementbyTag("ul");
        libhtmlpp::HtmlElement *b = static_cast<libhtmlpp::HtmlElement*>(&root)->getElementbyID("b");
        check(ul != nullptr && b != nullptr, "found <ul> and \"b\"");
        if (ul && b) {
            libhtmlpp::Element *a = ul->firstChild();
            ul->remove(b); // previously: null-pointer dereference crash
            check(true, "removing a middle child (reached via sibling advancement) doesn't crash");

            check(a != nullptr && a->nextElement() != nullptr, "\"a\" still has a next sibling");
            if (a && a->nextElement()) {
                libhtmlpp::HtmlElement *c = static_cast<libhtmlpp::HtmlElement*>(a->nextElement());
                check(c->getAtributte("id") == "c", "\"a\"'s next sibling is now \"c\", skipping removed \"b\"");
                check(c->prevElement() == a,
                      "\"c\"'s prevElement correctly points to \"a\", not left dangling at removed \"b\"");
            }
            check(b->parentElement() == nullptr && b->nextElement() == nullptr && b->prevElement() == nullptr,
                  "the removed node's own parent/prev/next links are all cleared");
        }
    }

    // --- Summary ---
    std::cout << "\n=== " << passCount << "/" << testCount << " tests passed ===" << std::endl;

    return (passCount == testCount) ? 0 : -1;
}