From b537916c6b0367d2f8274711545c2c86f38a376d Mon Sep 17 00:00:00 2001 From: orosmatthew Date: Wed, 4 Oct 2023 13:02:36 -0400 Subject: [PATCH] Add tests system --- data/tests/p.html | 1 + data/tests/text.html | 1 + docs/html_grammar.txt | 2 +- src/fetch.cpp | 11 +++++++++++ src/html_parse.cpp | 9 +++++++-- src/html_parse.hpp | 2 +- src/main.cpp | 5 ++--- 7 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 data/tests/p.html create mode 100644 data/tests/text.html diff --git a/data/tests/p.html b/data/tests/p.html new file mode 100644 index 0000000..63c016c --- /dev/null +++ b/data/tests/p.html @@ -0,0 +1 @@ +

Hello World!

\ No newline at end of file diff --git a/data/tests/text.html b/data/tests/text.html new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/data/tests/text.html @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/docs/html_grammar.txt b/docs/html_grammar.txt index 388bfd3..774f37a 100644 --- a/docs/html_grammar.txt +++ b/docs/html_grammar.txt @@ -5,7 +5,7 @@ Defined Tokens --- - ::= [] * + ::= [] ( | )* ::= "<" "!" "doctype" "html" ">" ::= | "<" * ">" ( | )* "<" "/" ">" diff --git a/src/fetch.cpp b/src/fetch.cpp index b940ea0..fc6555b 100644 --- a/src/fetch.cpp +++ b/src/fetch.cpp @@ -1,6 +1,8 @@ #include "fetch.hpp" +#include #include +#include #include #include @@ -13,8 +15,17 @@ size_t curl_write_func(void* ptr, size_t size, size_t num, std::string* str) return size * num; } +const std::string c_test_proto = "test://"; + std::optional fetch_url(const std::string& url) { + if (url.rfind(c_test_proto, 0) == 0) { + std::fstream file("../data/tests/" + url.substr(c_test_proto.size()), std::ios::in); + std::stringstream ss; + ss << file.rdbuf(); + file.close(); + return ss.str(); + } std::string curl_write_data; curl_easy_setopt(g_curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(g_curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L); diff --git a/src/html_parse.cpp b/src/html_parse.cpp index a266084..ad1b4e4 100644 --- a/src/html_parse.cpp +++ b/src/html_parse.cpp @@ -194,8 +194,13 @@ NodeDoc Parser::parse() if (auto doc_type = parse_doc_type()) { doc.doc_type = doc_type.value(); } - while (auto elem = parse_elem()) { - doc.children.push_back(elem.value()); + while (peek().has_value()) { + if (auto elem = parse_elem()) { + doc.children.push_back(elem.value()); + } + else { + doc.children.push_back(consume().to_string()); + } } return doc; } diff --git a/src/html_parse.hpp b/src/html_parse.hpp index ae2cdca..eb271d1 100644 --- a/src/html_parse.hpp +++ b/src/html_parse.hpp @@ -77,7 +77,7 @@ struct NodeElem { struct NodeDoc { std::optional doc_type {}; - std::vector children {}; + std::vector> children {}; }; class Parser { diff --git a/src/main.cpp b/src/main.cpp index fd89fc3..88c31a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,5 @@ #include "fetch.hpp" -#include #include #include @@ -14,7 +13,7 @@ int main() { init_curl(); - std::optional page_data = fetch_url("https://example.com"); + std::optional page_data = fetch_url("test://text.html"); SetConfigFlags(ConfigFlags::FLAG_WINDOW_RESIZABLE | ConfigFlags::FLAG_MSAA_4X_HINT | ConfigFlags ::FLAG_VSYNC_HINT); @@ -27,7 +26,7 @@ int main() SetTargetFPS(60); bool is_editing_url = false; - std::string url_input = "https://example.com"; + std::string url_input = "test://text.html"; url_input.reserve(1024); float scroll_pos = 0.0f;