From 52e0e71c4a968a1447395e615a1ee2078c1c7f7c Mon Sep 17 00:00:00 2001 From: Nimetu Date: Thu, 20 May 2021 13:27:36 +0300 Subject: [PATCH] Simple html/css test tool --- nel/tools/CMakeLists.txt | 5 + nel/tools/htmlcss_test/CMakeLists.txt | 15 ++ nel/tools/htmlcss_test/htmlcss_test.cpp | 183 ++++++++++++++++++ .../htmlcss_test/tests/01-syntax-tests.html | 28 +++ .../tests/css-tricks/01-var-toggle.html | 24 +++ .../tests/custom-properties/e01.basic.html | 18 ++ .../e02.case-sensitive-name.html | 23 +++ .../custom-properties/e03.value-syntax.html | 17 ++ .../custom-properties/e05.inherited.html | 17 ++ .../tests/custom-properties/e06.i18n.html | 15 ++ .../e07-safe-use-not-cyclic.html | 15 ++ .../tests/custom-properties/e08-cyclic.html | 14 ++ .../custom-properties/e09-not-cyclic.html | 15 ++ .../tests/custom-properties/e10.fallback.html | 24 +++ .../e11.1.invalid-property-name.html | 12 ++ .../e11.2.whitespace-after-var.html | 13 ++ .../e13.invalid-variables.html | 13 ++ 17 files changed, 451 insertions(+) create mode 100644 nel/tools/htmlcss_test/CMakeLists.txt create mode 100644 nel/tools/htmlcss_test/htmlcss_test.cpp create mode 100644 nel/tools/htmlcss_test/tests/01-syntax-tests.html create mode 100644 nel/tools/htmlcss_test/tests/css-tricks/01-var-toggle.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e01.basic.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e02.case-sensitive-name.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e03.value-syntax.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e05.inherited.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e06.i18n.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e07-safe-use-not-cyclic.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e08-cyclic.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e09-not-cyclic.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e10.fallback.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e11.1.invalid-property-name.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e11.2.whitespace-after-var.html create mode 100644 nel/tools/htmlcss_test/tests/custom-properties/e13.invalid-variables.html diff --git a/nel/tools/CMakeLists.txt b/nel/tools/CMakeLists.txt index 5297f34a4..079ded560 100644 --- a/nel/tools/CMakeLists.txt +++ b/nel/tools/CMakeLists.txt @@ -37,3 +37,8 @@ ENDIF() IF(WITH_NEL_TESTS) ADD_SUBDIRECTORY(nel_unit_test) ENDIF() + +IF(WITH_HTMLCSS_TEST) + ADD_SUBDIRECTORY(htmlcss_test) +ENDIF() + diff --git a/nel/tools/htmlcss_test/CMakeLists.txt b/nel/tools/htmlcss_test/CMakeLists.txt new file mode 100644 index 000000000..d37498f60 --- /dev/null +++ b/nel/tools/htmlcss_test/CMakeLists.txt @@ -0,0 +1,15 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.1) +CMAKE_POLICY(SET CMP0015 NEW) + +LINK_DIRECTORIES(${LINK_DIRECTORIES} ${CMAKE_LIBRARY_DIR}) +ADD_DEFINITIONS(${LIBXML2_DEFINITIONS}) + +FILE(GLOB SRC htmlcss_test.cpp) +ADD_EXECUTABLE(htmlcss_test ${SRC}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${LUA_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR} ) +TARGET_LINK_LIBRARIES(htmlcss_test nelmisc nelgui ${LUA_LIBRARIES} ${LIBXML2_LIBRARIES}) +NL_DEFAULT_PROPS(htmlcss_test "Ryzom, Tests: html/css parser tests") +NL_ADD_RUNTIME_FLAGS(htmlcss_test) + +INSTALL(TARGETS htmlcss_test RUNTIME DESTINATION bin COMPONENT tools) +# TODO: test fixtures diff --git a/nel/tools/htmlcss_test/htmlcss_test.cpp b/nel/tools/htmlcss_test/htmlcss_test.cpp new file mode 100644 index 000000000..4e5b79843 --- /dev/null +++ b/nel/tools/htmlcss_test/htmlcss_test.cpp @@ -0,0 +1,183 @@ +/* + * File: main.cpp + * Author: Karu + * + * Created on 2015-04-11 + */ + +typedef struct _xmlNode xmlNode; + +#include +#include + +#include "nel/misc/types_nl.h" +#include "nel/misc/file.h" +#include "nel/misc/path.h" + +#include "nel/gui/html_parser.h" +#include "nel/gui/css_parser.h" +#include "nel/gui/html_element.h" +#include "nel/gui/css_style.h" + +#include "nel/gui/libwww.h" + +using namespace std; +using namespace NLMISC; +using namespace NLGUI; + +sint indent { 0 }; + +// *************************************************************************** +void checkRuleset(CHtmlElement &elm, CCssStyle &style, TStyleVec testset, bool exists) +{ + bool verbose = false; + std::string existsMessage = exists ? "exist" : "unset"; + for (auto it : testset) + { + bool failed = (exists != style.hasStyle(it.first)); + if (failed) + { + bool failed2 = true; + if (it.first == "font-size") + { + printf("[%s]: font-size: %d; expected '%s'\n", existsMessage.c_str(), style.Current.FontSize, it.second.c_str()); + printf(" (%s)\n", elm.toString().c_str()); + failed2 = false; + } + else if (it.first == "background-color") + { + printf("[%s]: background-color: '%s'; expected '%s'\n", existsMessage.c_str(), style.Current.BackgroundColor.toString().c_str(), it.second.c_str()); + printf(" (%s)\n", elm.toString().c_str()); + failed2 = false; + } + + if (failed2) + { + printf("[%s] FAIL: '%s': '%s'\n", existsMessage.c_str(), it.first.c_str(), it.second.c_str()); + printf(" (%s)\n", elm.toString().c_str()); + for (auto it2 : style.Current.StyleRules) + printf("'%s': '%s'\n", it2.first.c_str(), it2.second.c_str()); + } + } + else if (exists && !style.checkStyle(it.first, it.second)) + { + printf("[%s] FAIL: expecting '%s': '%s', got '%s'\n", existsMessage.c_str(), it.first.c_str(), it.second.c_str(), style.getStyle(it.first).c_str()); + printf(" (%s)\n", elm.toString().c_str()); + } + else if (!failed) + { + if (verbose) + printf("[%s] PASS: '%s': '%s'\n", existsMessage.c_str(), it.first.c_str(), it.second.c_str()); + } + } +} + +// *************************************************************************** +void recursiveHtmlRender(CHtmlElement &elm, CCssStyle &style) +{ + bool verbose = false; + if (elm.Type == CHtmlElement::TEXT_NODE) + { + std::string val = trim(elm.Value); + if (verbose) + if (!val.empty()) + printf("[%d] '%s'\n", indent, val.c_str()); + } + else if (elm.Type == CHtmlElement::ELEMENT_NODE) + { + style.pushStyle(); + + if (verbose) + printf("========= '%s'\n", elm.toString().c_str()); + + style.getStyleFor(elm); + style.applyStyle(elm.Style); + if (elm.hasAttribute("data-ruleset")) + { + TStyleVec testset = CCssParser::parseDecls(elm.getAttribute("data-ruleset")); + checkRuleset(elm, style, testset, true); + } + + if (elm.hasAttribute("data-ruleunset")) + { + TStyleVec testset = CCssParser::parseDecls(elm.getAttribute("data-ruleunset")); + checkRuleset(elm, style, testset, false); + } + + if (elm.hasAttribute("data-ruleset-before")) + { + TStyleVec testset = CCssParser::parseDecls(elm.getAttribute("data-ruleset-before")); + + } + + for (auto it = elm.Children.begin(); it != elm.Children.end(); ++it) + { + recursiveHtmlRender(*it, style); + } + + style.popStyle(); + } +} + +// *************************************************************************** +void runTestOnFile(const std::string &filename) +{ + CHtmlElement dom; + + CHtmlParser htmlParser; + + std::vector links; + std::vector styles; + //, elm, styles, links + + ifstream f(filename); + if (!f.is_open()) + { + printf("!! failed to open file '%s'\n", filename.c_str()); + return; + } + + printf(": %s\n", filename.c_str()); + std::string htmlString; + std::string line; + while (getline(f, line)) + htmlString += line; + + htmlParser.getDOM(htmlString, dom, styles, links); + + CCssStyle style; + for (std::string s : styles) + { + if (!s.empty()) + style.parseStylesheet(s); + } + + for (auto it = dom.Children.begin(); it != dom.Children.end(); ++it) + recursiveHtmlRender(*it, style); +} + +// *************************************************************************** +int main(int argc, const char *argv[]) +{ + CApplicationContext *appContext = new CApplicationContext; + + // htmlcss_test file.html + if (argc == 2) + { + runTestOnFile(argv[1]); + } + else + { + std::vector result; + CPath::getPathContent("tests/", true /*recursive*/, false /*wantDir*/, true /*wantFile*/, result, NULL /*callback*/, true /*showEverything*/); + printf(":: got %ld files\n", result.size()); + for (const auto &fname : result) + { + if (endsWith(fname, ".html") && fname != "tests/XX-template.html") + runTestOnFile(fname); + } + } + + printf(">>> all done\n"); + return EXIT_SUCCESS; +} diff --git a/nel/tools/htmlcss_test/tests/01-syntax-tests.html b/nel/tools/htmlcss_test/tests/01-syntax-tests.html new file mode 100644 index 000000000..f9b2c9356 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/01-syntax-tests.html @@ -0,0 +1,28 @@ + + + + +
+ + link + link + + + diff --git a/nel/tools/htmlcss_test/tests/css-tricks/01-var-toggle.html b/nel/tools/htmlcss_test/tests/css-tricks/01-var-toggle.html new file mode 100644 index 000000000..72e49b854 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/css-tricks/01-var-toggle.html @@ -0,0 +1,24 @@ + + + + +

+

+

+

+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e01.basic.html b/nel/tools/htmlcss_test/tests/custom-properties/e01.basic.html new file mode 100644 index 000000000..c5085b8ef --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e01.basic.html @@ -0,0 +1,18 @@ + + + + +
+

Header

+
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e02.case-sensitive-name.html b/nel/tools/htmlcss_test/tests/custom-properties/e02.case-sensitive-name.html new file mode 100644 index 000000000..1d9dc7b88 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e02.case-sensitive-name.html @@ -0,0 +1,23 @@ + + + + +
+

Header1

+

Header2

+
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e03.value-syntax.html b/nel/tools/htmlcss_test/tests/custom-properties/e03.value-syntax.html new file mode 100644 index 000000000..28f0a9c69 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e03.value-syntax.html @@ -0,0 +1,17 @@ + + + + +
+

Header

+
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e05.inherited.html b/nel/tools/htmlcss_test/tests/custom-properties/e05.inherited.html new file mode 100644 index 000000000..bafbbf37f --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e05.inherited.html @@ -0,0 +1,17 @@ + + + + +

I inherited blue from the root element!

+
I got green set directly on me!
+
+ While I got red set directly on me! +

I’m red too, because of inheritance!

+
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e06.i18n.html b/nel/tools/htmlcss_test/tests/custom-properties/e06.i18n.html new file mode 100644 index 000000000..861e60412 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e06.i18n.html @@ -0,0 +1,15 @@ + + + + + + link + + link + + + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e07-safe-use-not-cyclic.html b/nel/tools/htmlcss_test/tests/custom-properties/e07-safe-use-not-cyclic.html new file mode 100644 index 000000000..39ed3a299 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e07-safe-use-not-cyclic.html @@ -0,0 +1,15 @@ + + + + +
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e08-cyclic.html b/nel/tools/htmlcss_test/tests/custom-properties/e08-cyclic.html new file mode 100644 index 000000000..4d1f5fdd8 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e08-cyclic.html @@ -0,0 +1,14 @@ + + + + +
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e09-not-cyclic.html b/nel/tools/htmlcss_test/tests/custom-properties/e09-not-cyclic.html new file mode 100644 index 000000000..f3e1788ee --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e09-not-cyclic.html @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e10.fallback.html b/nel/tools/htmlcss_test/tests/custom-properties/e10.fallback.html new file mode 100644 index 000000000..5d785bffa --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e10.fallback.html @@ -0,0 +1,24 @@ + + + + +
+

header

+

text

+
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e11.1.invalid-property-name.html b/nel/tools/htmlcss_test/tests/custom-properties/e11.1.invalid-property-name.html new file mode 100644 index 000000000..488e733a9 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e11.1.invalid-property-name.html @@ -0,0 +1,12 @@ + + + + +
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e11.2.whitespace-after-var.html b/nel/tools/htmlcss_test/tests/custom-properties/e11.2.whitespace-after-var.html new file mode 100644 index 000000000..a67b4ba1d --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e11.2.whitespace-after-var.html @@ -0,0 +1,13 @@ + + + + + +
+ + diff --git a/nel/tools/htmlcss_test/tests/custom-properties/e13.invalid-variables.html b/nel/tools/htmlcss_test/tests/custom-properties/e13.invalid-variables.html new file mode 100644 index 000000000..f60877d82 --- /dev/null +++ b/nel/tools/htmlcss_test/tests/custom-properties/e13.invalid-variables.html @@ -0,0 +1,13 @@ + + + + + + +
+ +