aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-07-02 23:17:06 +0200
committerdec05eba <dec05eba@protonmail.com>2021-07-02 23:17:06 +0200
commit5f283adc8d1a29f420d466e85b216e9d6f4a9822 (patch)
tree1006965593e1f7c2571f0f74a64c3555fc03d506 /tests
parent48ceb8591e1e3c7695d091f02a6a43edb5f77936 (diff)
Remove dependency on html tidy
Diffstat (limited to 'tests')
-rw-r--r--tests/main.c46
1 files changed, 32 insertions, 14 deletions
diff --git a/tests/main.c b/tests/main.c
index 7888ea1..2a08ec7 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -1,34 +1,51 @@
-#include <stdio.h>
#include "../include/quickmedia/HtmlSearch.h"
-#include <assert.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
-static char* get_file_content(const char *filepath) {
+static char* get_file_content(const char *filepath, size_t *filesize) {
FILE *file = fopen(filepath, "rb");
assert(file);
fseek(file, 0, SEEK_END);
- size_t filesize = ftell(file);
+ *filesize = ftell(file);
fseek(file, 0, SEEK_SET);
- char *buffer = malloc(filesize + 1);
- buffer[filesize] = '\0';
- fread(buffer, 1, filesize, file);
+ char *buffer = malloc((*filesize) + 1);
+ buffer[*filesize] = '\0';
+ fread(buffer, 1, *filesize, file);
return buffer;
}
-static void result_callback(QuickMediaHtmlNode *node, void *userdata) {
- const char *href = quickmedia_html_node_get_attribute_value(node, "href");
- const char *text = quickmedia_html_node_get_text(node);
- printf("a href: %s, node value: %s\n", href, text);
+static int result_callback(QuickMediaMatchNode *node, void *userdata) {
+ QuickMediaStringView href = quickmedia_html_node_get_attribute_value(node, "href");
+ QuickMediaStringView text = quickmedia_html_node_get_text(node);
+ printf("a href: %.*s, node value: %.*s\n", (int)href.size, href.data, (int)text.size, text.data);
+ return 0;
+}
+
+static int result_callback_nested_text(QuickMediaMatchNode *node, void *userdata) {
+ QuickMediaStringView text = quickmedia_html_node_get_text(node);
+ printf("text: %.*s\n", (int)text.size, text.data);
+ return 0;
+}
+
+static void test_nested_nodes_get_text() {
+ const char *html_source = "<div class=\"item\">hello<h1>text</h1>world</div>";
+ QuickMediaHtmlSearch html_search;
+ quickmedia_html_search_init(&html_search, html_source, strlen(html_source));
+ quickmedia_html_find_nodes_xpath(&html_search, "//div[class='item']", result_callback_nested_text, NULL);
+ quickmedia_html_search_deinit(&html_search);
}
-int main(int argc, char **argv) {
- char *file_content = get_file_content("test_files/test.html");
+int main() {
+ size_t filesize = 0;
+ char *file_content = get_file_content("test_files/test.html", &filesize);
QuickMediaHtmlSearch html_search;
- int result = quickmedia_html_search_init(&html_search, file_content);
+ int result = quickmedia_html_search_init(&html_search, file_content, filesize);
if(result != 0)
goto cleanup;
result = quickmedia_html_find_nodes_xpath(&html_search, "//h3[class=\"story_name\"]//a", result_callback, NULL);
@@ -38,5 +55,6 @@ int main(int argc, char **argv) {
cleanup:
quickmedia_html_search_deinit(&html_search);
free(file_content);
+ test_nested_nodes_get_text();
return result;
}