Commit 55a60b73 authored by jan.koester's avatar jan.koester
Browse files

added parser test

parent b1ac31d8
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -32,3 +32,9 @@ add_executable(htmlostreamtest ostream.cpp)
target_link_libraries(htmlostreamtest htmlpp-static)

add_test(htmlostreamtest htmlostreamtest ${CMAKE_SOURCE_DIR}/test/htmlfiles/right.html)

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

add_test(htmlroundtriptest_html4 htmlroundtriptest ${CMAKE_SOURCE_DIR}/test/htmlfiles/html4.html)
add_test(htmlroundtriptest_right htmlroundtriptest ${CMAKE_SOURCE_DIR}/test/htmlfiles/right.html)
+103 −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.
*******************************************************************************/

// Round-trip / copy-fidelity test.
//
// parse(src) -> print -> pass1, then parse(pass1) -> print -> pass2.
// pass1 == pass2 is the invariant this test enforces: once a document has
// gone through the parser/printer once, it is already in this library's own
// canonical form, so a second parse+print cycle must be a no-op. Any diff
// between pass1 and pass2 is a genuine parser or printer bug, independent of
// how the *original* source document happened to be formatted (whitespace,
// quoting, void-element style, etc. are legitimately allowed to change on
// the first pass).
//
// Usage: htmlroundtriptest <input.html> [pass1-out] [pass2-out]
// Exit status is non-zero on parse failure or pass1/pass2 mismatch.

#include <fstream>
#include <iostream>
#include <sstream>

#include "html.h"
#include "exception.h"

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

static void writeIfRequested(const char *path, const std::string &data){
    if(!path)
        return;
    std::ofstream out(path, std::ios::binary);
    out.write(data.data(), (std::streamsize)data.size());
}

int main(int argc, char *argv[]){
    if(argc < 2){
        std::cout << Red << "usage: " << argv[0]
                   << " <input.html> [pass1-out] [pass2-out]" << NOCOLOR << std::endl;
        return -1;
    }

    const char *pass1Path = argc > 2 ? argv[2] : nullptr;
    const char *pass2Path = argc > 3 ? argv[3] : nullptr;

    try{
        libhtmlpp::HtmlPage page1;
        libhtmlpp::HtmlElement doc1;
        page1.loadFile(doc1, argv[1]);

        libhtmlpp::HtmlString pass1;
        libhtmlpp::print(doc1, pass1, false);

        libhtmlpp::HtmlPage page2;
        libhtmlpp::HtmlElement doc2;
        page2.loadString(doc2, pass1);

        libhtmlpp::HtmlString pass2;
        libhtmlpp::print(doc2, pass2, false);

        writeIfRequested(pass1Path, pass1.str());
        writeIfRequested(pass2Path, pass2.str());

        if(pass1.str() != pass2.str()){
            std::cout << Red << "pass1 != pass2 -- parser/printer is not idempotent"
                       << NOCOLOR << std::endl;
            std::cout << Red << "Test not Passed!" << NOCOLOR << std::endl;
            return -1;
        }

        std::cout << Green << "pass1 == pass2 (idempotent)" << NOCOLOR << std::endl;
        std::cout << Green << "Test Passed!" << NOCOLOR << std::endl;
    }catch(libhtmlpp::HTMLException &exp){
        std::cout << exp.what() << std::endl;
        std::cout << Red << "Test not Passed!" << NOCOLOR << std::endl;
        return -1;
    }
    return 0;
}