Add tests system

This commit is contained in:
orosmatthew 2023-10-04 13:02:36 -04:00
parent 4e0cc367e6
commit b537916c6b
7 changed files with 24 additions and 7 deletions

1
data/tests/p.html Normal file
View File

@ -0,0 +1 @@
<p>Hello World!</p>

1
data/tests/text.html Normal file
View File

@ -0,0 +1 @@
Hello World!

View File

@ -5,7 +5,7 @@ Defined Tokens
--- ---
<doc> ::= [<doctype>] <elem>* <doc> ::= [<doctype>] (<elem> | <inner>)*
<doctype> ::= "<" "!" "doctype" "html" ">" <doctype> ::= "<" "!" "doctype" "html" ">"
<elem> ::= <elem> ::=
| "<" <ident> <attr>* ">" (<elem> | <inner>)* "<" "/" <ident> ">" | "<" <ident> <attr>* ">" (<elem> | <inner>)* "<" "/" <ident> ">"

View File

@ -1,6 +1,8 @@
#include "fetch.hpp" #include "fetch.hpp"
#include <fstream>
#include <iostream> #include <iostream>
#include <sstream>
#include <string> #include <string>
#include <curl/curl.h> #include <curl/curl.h>
@ -13,8 +15,17 @@ size_t curl_write_func(void* ptr, size_t size, size_t num, std::string* str)
return size * num; return size * num;
} }
const std::string c_test_proto = "test://";
std::optional<std::string> fetch_url(const std::string& url) std::optional<std::string> 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; std::string curl_write_data;
curl_easy_setopt(g_curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(g_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(g_curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L); curl_easy_setopt(g_curl, CURLOPT_CA_CACHE_TIMEOUT, 604800L);

View File

@ -194,8 +194,13 @@ NodeDoc Parser::parse()
if (auto doc_type = parse_doc_type()) { if (auto doc_type = parse_doc_type()) {
doc.doc_type = doc_type.value(); doc.doc_type = doc_type.value();
} }
while (auto elem = parse_elem()) { while (peek().has_value()) {
doc.children.push_back(elem.value()); if (auto elem = parse_elem()) {
doc.children.push_back(elem.value());
}
else {
doc.children.push_back(consume().to_string());
}
} }
return doc; return doc;
} }

View File

@ -77,7 +77,7 @@ struct NodeElem {
struct NodeDoc { struct NodeDoc {
std::optional<NodeDocType> doc_type {}; std::optional<NodeDocType> doc_type {};
std::vector<NodeElem> children {}; std::vector<std::variant<NodeElem, std::string>> children {};
}; };
class Parser { class Parser {

View File

@ -1,6 +1,5 @@
#include "fetch.hpp" #include "fetch.hpp"
#include <codecvt>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
@ -14,7 +13,7 @@ int main()
{ {
init_curl(); init_curl();
std::optional<std::string> page_data = fetch_url("https://example.com"); std::optional<std::string> page_data = fetch_url("test://text.html");
SetConfigFlags(ConfigFlags::FLAG_WINDOW_RESIZABLE | ConfigFlags::FLAG_MSAA_4X_HINT | ConfigFlags ::FLAG_VSYNC_HINT); SetConfigFlags(ConfigFlags::FLAG_WINDOW_RESIZABLE | ConfigFlags::FLAG_MSAA_4X_HINT | ConfigFlags ::FLAG_VSYNC_HINT);
@ -27,7 +26,7 @@ int main()
SetTargetFPS(60); SetTargetFPS(60);
bool is_editing_url = false; bool is_editing_url = false;
std::string url_input = "https://example.com"; std::string url_input = "test://text.html";
url_input.reserve(1024); url_input.reserve(1024);
float scroll_pos = 0.0f; float scroll_pos = 0.0f;